summaryrefslogtreecommitdiff
path: root/ir.h
diff options
context:
space:
mode:
authorWormHeamer2025-10-27 23:26:44 -0400
committerWormHeamer2025-10-27 23:26:44 -0400
commitdd62801133cddca25d94c9c59f8ca7d0748850c6 (patch)
treedb5210bd773d0c57f1cfe53b303d1a6debdde0b7 /ir.h
parenta2c5243af5bd8482385aeb3395e073ff17f57a0d (diff)
small size optimization on <= 1-node NodeList
Diffstat (limited to 'ir.h')
-rw-r--r--ir.h21
1 files changed, 18 insertions, 3 deletions
diff --git a/ir.h b/ir.h
index 8b963f7..b5992c8 100644
--- a/ir.h
+++ b/ir.h
@@ -100,7 +100,13 @@ typedef enum {
const char *node_type_name(NodeType t);
-typedef DYNARR(struct Node *) NodeList;
+typedef struct {
+ unsigned len, cap;
+ union {
+ struct Node *sbo;
+ struct Node **data;
+ };
+} NodeList;
typedef struct Node {
int id, refs;
@@ -122,8 +128,9 @@ typedef struct Node {
/* convenience macros (lisp-inspired lol) */
-#define IN(n, i) ((n)->in.data[i])
-#define OUT(n, i) ((n)->out.data[i])
+#define Ni(nl, i) (*nodelist_nth(&(nl), i))
+#define IN(n, i) Ni(n->in, i)
+#define OUT(n, i) Ni(n->out, i)
#define CTRL(n) IN(n, 0)
#define CAR(n) IN(n, 1)
@@ -187,4 +194,12 @@ int node_maybe_uninit(Node *n);
#define node_new(...) node_newv(__VA_ARGS__, NULL)
+static inline Node **nodelist_data(NodeList *l) {
+ return l->cap ? l->data : &l->sbo;
+}
+
+static inline Node **nodelist_nth(NodeList *l, uint32_t i) {
+ return nodelist_data(l) + i;
+}
+
#endif