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

@@ -377,7 +377,7 @@
377377 if (!nd) return;
378378 size_t klen = strlen(key), vlen = strlen(val);
379379 for (int j = 0; j < nd->ns; j++)
380- if (strcmp(nd->s[j].key, key) == 0) {
380+ if (sp_streq(nd->s[j].key, key)) {
381381 free(nd->s[j].val); nd->s[j].val = dup_n(val, vlen); nd->s[j].val_len = vlen; return;
382382 }
383383 node_add_str(nd, key, klen, dup_n(val, vlen), vlen);
@@ -481,7 +481,7 @@
481481 const SpNode *nd = node_at(nt, id);
482482 if (!nd) return NULL;
483483 for (int j = 0; j < nd->ns; j++)
484- if (strcmp(nd->s[j].key, key) == 0) return nd->s[j].val;
484+ if (sp_streq(nd->s[j].key, key)) return nd->s[j].val;
485485 return NULL;
486486 }
487487
@@ -490,7 +490,7 @@
490490 if (!nd) return 0;
491491 size_t vlen = strlen(val);
492492 for (int j = 0; j < nd->ns; j++) {
493- if (strcmp(nd->s[j].key, key) == 0) {
493+ if (sp_streq(nd->s[j].key, key)) {
494494 free(nd->s[j].val);
495495 nd->s[j].val = malloc(vlen + 1);
496496 memcpy(nd->s[j].val, val, vlen + 1);
@@ -505,7 +505,7 @@
505505 const SpNode *nd = node_at(nt, id);
506506 if (!nd) return 0;
507507 for (int j = 0; j < nd->ns; j++)
508- if (strcmp(nd->s[j].key, key) == 0) return nd->s[j].val_len;
508+ if (sp_streq(nd->s[j].key, key)) return nd->s[j].val_len;
509509 return 0;
510510 }
511511
@@ -513,7 +513,7 @@
513513 const SpNode *nd = node_at(nt, id);
514514 if (!nd) return dflt;
515515 for (int j = 0; j < nd->ni; j++)
516- if (strcmp(nd->i[j].key, key) == 0) return nd->i[j].val;
516+ if (sp_streq(nd->i[j].key, key)) return nd->i[j].val;
517517 return dflt;
518518 }
519519
@@ -521,7 +521,7 @@
521521 const SpNode *nd = node_at(nt, id);
522522 if (!nd) return -1;
523523 for (int j = 0; j < nd->nr; j++)
524- if (strcmp(nd->r[j].key, key) == 0) return nd->r[j].ref;
524+ if (sp_streq(nd->r[j].key, key)) return nd->r[j].ref;
525525 return -1;
526526 }
527527
@@ -530,7 +530,7 @@
530530 const SpNode *nd = node_at(nt, id);
531531 if (!nd) return NULL;
532532 for (int j = 0; j < nd->na; j++)
533- if (strcmp(nd->a[j].key, key) == 0) { *out_n = nd->a[j].n; return nd->a[j].ids; }
533+ if (sp_streq(nd->a[j].key, key)) { *out_n = nd->a[j].n; return nd->a[j].ids; }
534534 return NULL;
535535 }
536536