Skip to content

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

Open
ludi317 wants to merge 2 commits intosvent:masterfrom
ludi317:master
Open

Bug fix + 16x speedup for -w when WORD starts with string literal#123
ludi317 wants to merge 2 commits intosvent:masterfrom
ludi317:master

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

Comments