Skip to content

Fix regexp performance regression for patterns starting with s/k#174

Open
nobu wants to merge 1 commit intok-takata:masterfrom
nobu:fix-performance-regression
Open

Fix regexp performance regression for patterns starting with s/k#174
nobu wants to merge 1 commit intok-takata:masterfrom
nobu:fix-performance-regression

Conversation

@nobu
Copy link
Contributor

@nobu nobu commented Feb 2, 2026

Merge from ruby/ruby#15848.

ruby/ruby@981ee02c7c ("Fix performance problem with /k/i and /s/i") was
merged for Ruby 4.0 to enable partial Boyer-Moore optimization for
patterns containing 's' or 'k' by using the prefix before those
characters.

However, when 's' or 'k' appears at the start of a pattern (no usable
prefix), set_bm_skip() returns 0 and the code returned early without
setting any optimization mode, leaving reg->optimize at
ONIG_OPTIMIZE_NONE. This caused up to 30x slowdown for patterns like
/slackware/i when matched against strings with non-ASCII characters.

This patch keeps the improvement from 981ee02c7c for patterns with
3+ char prefix, while fixing the regression by falling back to
ONIG_OPTIMIZE_EXACT_IC with the full pattern when the usable prefix
is less than 3 characters.

Before: /\bslackware\b/i with non-ASCII string: 2.24 us/op
After:  /\bslackware\b/i with non-ASCII string: 0.70 us/op (3.2x faster)

[Bug #21824](https://bugs.ruby-lang.org/issues/21824)
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