Skip to content

Optimize strspn() #12431

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

Closed
wants to merge 1 commit into from
Closed

Optimize strspn() #12431

wants to merge 1 commit into from

Conversation

nielsdos
Copy link
Member

The current implementation uses a nested loop (for + goto), which has complexity O(|s1| * |s2|). If we instead use a lookup table, the complexity drops to O(|s1| + |s2|).
This is conceptually the same strategy that common C library implementations such as glibc and musl use.
The variation with a bitvector instead of a table also gives a speed-up, but the table variation (i.e. this one) was about 1.34x faster.

On microbenchmarks this easily gave a 5x speedup.

This can bring a 1.4-1.5% performance improvement in the Symfony benchmark.

The current implementation uses a nested loop (for + goto), which has
complexity O(|s1| * |s2|). If we instead use a lookup table, the
complexity drops to O(|s1| + |s2|).
This is conceptually the same strategy that common C library
implementations such as glibc and musl use.
The variation with a bitvector instead of a table also gives a speed-up,
but the table variation was about 1.34x faster.

On microbenchmarks this easily gave a 5x speedup.

This can bring a 1.4-1.5% performance improvement in the Symfony
benchmark.
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, although the names s1 and s2 are kinda crap. Maybe improve those while at it?

@nielsdos nielsdos closed this in d0b29d8 Oct 14, 2023
@nielsdos
Copy link
Member Author

Maybe improve those while at it?

Agreed, fixed in the merge, thx

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

Successfully merging this pull request may close these issues.

2 participants