From dd62801133cddca25d94c9c59f8ca7d0748850c6 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Mon, 27 Oct 2025 23:26:44 -0400 Subject: small size optimization on <= 1-node NodeList --- ir.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'ir.h') 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 -- cgit v1.2.3