Commit d65f6c70
d65f6c70249aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
node_table: replace strcmp with an inline byte compare in the field accessors
A profiled optcarrot compile spends ~47% of self time in __strcmp_avx2. The field-key lookups in nt_str / nt_ref / nt_int / nt_arr / nt_str_len / nt_set_str linearly scan a node's small key array comparing short keys ("name", "value", "receiver", ...); glibc resolves each to __strcmp_avx2, whose per-call AVX setup dominates for strings this short and is paid in full even when the first byte already differs (the common case while scanning for a non-matching key).
Add a static inline int sp_streq(a, b) byte loop in node_table.h and use it in
the accessors. Pure equality, so behavior is identical: emitted C is
byte-for-byte unchanged on optcarrot and the synthetic reproducers.
Compile time (analyze+codegen only, -c, best of 5):
optcarrot: 4.34 s -> 4.07 s (-6%) N=F=90 ivar reproducer: 4.05 s -> 3.47 s (-14%)
The remaining strcmp self time is in the type-string and method-name dispatch ladders in the analysis passes (separate follow-up).
Gates: 950 tests pass, optcarrot checksum 59662, clang clean.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- d65f6c70249a
- Parent
- 9b13d2e8dc15
src/node_table.cmodifiedView file
if (!nd) return; size_t klen = strlen(key), vlen = strlen(val); for (int j = 0; j < nd->ns; j++) if (strcmp(nd->s[j].key, key) == 0) { if (sp_streq(nd->s[j].key, key)) { free(nd->s[j].val); nd->s[j].val = dup_n(val, vlen); nd->s[j].val_len = vlen; return; } node_add_str(nd, key, klen, dup_n(val, vlen), vlen); const SpNode *nd = node_at(nt, id); if (!nd) return NULL; for (int j = 0; j < nd->ns; j++) if (strcmp(nd->s[j].key, key) == 0) return nd->s[j].val; if (sp_streq(nd->s[j].key, key)) return nd->s[j].val; return NULL;} if (!nd) return 0; size_t vlen = strlen(val); for (int j = 0; j < nd->ns; j++) { if (strcmp(nd->s[j].key, key) == 0) { if (sp_streq(nd->s[j].key, key)) { free(nd->s[j].val); nd->s[j].val = malloc(vlen + 1); memcpy(nd->s[j].val, val, vlen + 1); const SpNode *nd = node_at(nt, id); if (!nd) return 0; for (int j = 0; j < nd->ns; j++) if (strcmp(nd->s[j].key, key) == 0) return nd->s[j].val_len; if (sp_streq(nd->s[j].key, key)) return nd->s[j].val_len; return 0;} const SpNode *nd = node_at(nt, id); if (!nd) return dflt; for (int j = 0; j < nd->ni; j++) if (strcmp(nd->i[j].key, key) == 0) return nd->i[j].val; if (sp_streq(nd->i[j].key, key)) return nd->i[j].val; return dflt;} const SpNode *nd = node_at(nt, id); if (!nd) return -1; for (int j = 0; j < nd->nr; j++) if (strcmp(nd->r[j].key, key) == 0) return nd->r[j].ref; if (sp_streq(nd->r[j].key, key)) return nd->r[j].ref; return -1;} const SpNode *nd = node_at(nt, id); if (!nd) return NULL; for (int j = 0; j < nd->na; j++) if (strcmp(nd->a[j].key, key) == 0) { *out_n = nd->a[j].n; return nd->a[j].ids; } if (sp_streq(nd->a[j].key, key)) { *out_n = nd->a[j].n; return nd->a[j].ids; } return NULL;}src/node_table.hmodifiedView file
Loading diff…