8000 Speed-up contains by using `memchr` on every iteration by Mytherin · Pull Request #16484 · duckdb/duckdb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Speed-up contains by using memchr on every iteration #16484

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

Merged
merged 3 commits into from
Mar 3, 2025

Conversation

Mytherin
Copy link
Collaborator
@Mytherin Mytherin commented Mar 3, 2025

This PR reworks the implementation for contains (which also gets generated as part of LIKE '%str%' by the optimizer).

Previously we had three different implementations - for small aligned contains (2/4/8 bytes), small unaligned contains (3/5/6/7 bytes) and a generic fallback for needles > 8 bytes. These implementations were pretty distinct and not entirely optimal - they did a lot of work for every byte even if an early-out could be used to speed this up in many cases.

This PR unifies these three implementations and leans on memchr to find the beginning of possible matches - followed by doing an actual matching comparison. The way this works is that we first do the comparison with the largest unsigned integer that fits in the needle (so either 2/4/8 bytes) - followed by a memcmp for the remaining bytes (if any).

Running this query on hits we can see this improves performance in all cases - but especially for small unaligned searches and large searches.

SELECT COUNT(*) FROM hits WHERE URL LIKE '%goog%';
LIKE Search Size v1.2.0 New
%goog% 4 0.35s 0.31s
%google% 6 0.41s 0.33s
%google.com% 10 0.43s 0.32s

@Mytherin Mytherin merged commit 5ca240b into duckdb:main Mar 3, 2025
47 checks passed
@Mytherin Mytherin deleted the fastercontains branch April 2, 2025 09:24
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 15, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 15, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 16, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 17, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 18, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 18, 2025
Speed-up contains by using `memchr` on every iteration (duckdb/duckdb#16484)
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
0