Skip to content

perf: Add non-regex fastpath for KeyMatch2/KeyMatch3 - #279

Closed
cbaecker wants to merge 1 commit into
apache:masterfrom
cbaecker:perf/no-regex-fastpath-for-keymatch2-keymatch3
Closed

cbaecker wants to merge 1 commit into
apache:masterfrom
cbaecker:perf/no-regex-fastpath-for-keymatch2-keymatch3

Conversation

@cbaecker

Copy link
Copy Markdown
Contributor

Follow up on:

Adds a fastpath for KeyMatch2()/KeyMatch3(), checking if regex-evaluation is actually required.
My test with 1000 random (failing) requests resulted in a ~6 times faster performance.
Real-world scenario on embedded was about 2 times faster for initial (non-cached) request.

@hsluoyz

hsluoyz commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thanks @cbaecker, the fast path idea is good and the numbers made the case. I've pushed a reworked version directly to master in 2e99457, so I'm closing this PR in favor of it:

  • The fast path is a single find_first_of over the regex special characters (plus : for keyMatch2). With no special character it's plain equality; with only a trailing /* it's a prefix check. Everything else still goes to the regex.
  • The trailing /* case also checks that the rest of key1 has no \r or \n, because . in /.* doesn't match line terminators. This keeps it exactly equivalent to the regex.
  • keyMatch4 uses the same fast path, and keyMatch5 gets it through keyMatch3.
  • Compiled regexes are cached per thread (thread_local, capped at 1024 patterns), so patterns with placeholders and regexMatch no longer recompile on every call either.

I checked it against the old regex-only keyMatch2/keyMatch3 on ~520k random key/pattern pairs with no differences, and added a few test cases. On a small policy set of plain, /* and :id paths it's about 10x faster.

@hsluoyz hsluoyz closed this Oct 31, 2026
@cbaecker
cbaecker deleted the perf/no-regex-fastpath-for-keymatch2-keymatch3 branch October 31, 2026 07:11
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.

3 participants