Skip to content

Add ASCII fast path to gototab to avoid bsearch#264

Open
yhirose wants to merge 1 commit intoonetrueawk:stagingfrom
yhirose:gototab-ascii-fastpath
Open

Add ASCII fast path to gototab to avoid bsearch#264
yhirose wants to merge 1 commit intoonetrueawk:stagingfrom
yhirose:gototab-ascii-fastpath

Conversation

@yhirose
Copy link
Copy Markdown

@yhirose yhirose commented Apr 29, 2026

Add a per-state direct-indexed array for ASCII code points to gototab,
so get_gototab and set_gototab return in a single load on the common
path. Non-ASCII still falls through to the existing sorted (codepoint,
state) entries via bsearch, so behavior is unchanged. Same dense-table
technique gnulib's dfa.c uses, narrowed to ASCII.

Performance on testdir/Compare.tt (Apple M1, default -O2 -g):

Test Old New Ratio
tt.10 (/.$/) 0.21s 0.09s 0.43 (2.3× faster)
tt.15 (fmt) 1.60s 1.15s 0.72
tt.big (mixed) 1.99s 1.73s 0.87
All 21 tt. tests* 7.07s 6.11s 0.86 (≈ 14%)

Same ratio under -O3 -DNDEBUG -flto -mcpu=apple-m1.

Per-state struct grows by GOTO_DIRECT * sizeof(int) = 512 bytes,
about 5% of the existing per-state entries calloc.

Each state's gototab gains a direct-indexed array for ASCII code
points (0..GOTO_DIRECT-1), letting get_gototab and set_gototab
return in a single load on the common path.  Non-ASCII still goes
through the existing sorted (codepoint, state) entries via bsearch,
so behavior is unchanged.

On the testdir/Compare.tt suite this cuts total time by ~14%;
pure regex matches like /.$/ run about 2.3x faster.
@plan9
Copy link
Copy Markdown
Collaborator

plan9 commented May 1, 2026

thanks, I'll think about this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants