Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix + 16x speedup for -w when WORD starts with string literal #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ludi317
Copy link

@ludi317 ludi317 commented Mar 1, 2022

No description provided.

…h \b\W or ending with \W\b

Old code:
$ sift "\b\W\W\W\b" $m/mist-ap/cloud | wc -l
       0

New code:
$ sift "\b\W\W\W\b" $m/mist-ap/cloud | wc -l
    3509

Ripgrep agrees with bug fix:
$ rg "\b\W\W\W\b" $m/mist-ap/cloud | wc -l
    3509
…tches for

WORD start with a string literal.

1. Make a first pass over the haystack by starting the search with the string
literal part of the needle: WORD\b It's a more selective filter (in most cases)
and delays entering the slower regex engine. [1]

2. Do a second pass with the now much smaller haystack on the complete needle:
\bWORD\b

[1] https://github.com/golang/go/blob/a064a4f29a97a4fc7398d1ac9d7c53c5ba0bc646/src/regexp/backtrack.go#L341

Observed 16x speedup for the following pattern and directory:
sift -q "\bWaitForConnect\b" $m/mist-ap  2.60s user 0.09s system 549% cpu 0.488 total
sift -w -q "WaitForConnect" $m/mist-ap 0.08s user 0.10s system 570% cpu 0.031 total
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.

1 participant