Skip to content

⚡ perf: Optimize simhash bit generation and assembly#83

Closed
SuarezPM wants to merge 4 commits into
mainfrom
perf-simhash-vectorize-18412311951160534470
Closed

⚡ perf: Optimize simhash bit generation and assembly#83
SuarezPM wants to merge 4 commits into
mainfrom
perf-simhash-vectorize-18412311951160534470

Conversation

@SuarezPM

@SuarezPM SuarezPM commented Jun 4, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced the highly nested Python loops inside _simhash_impl with vectorized NumPy operations and fast bit-packing using np.packbits and int.from_bytes().
🎯 Why: The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences.
📊 Measured Improvement: Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching.


PR created automatically by Jules for task 18412311951160534470 started by @SuarezPM

Replaced inefficient nested Python loops for token bit generation and extraction in `_simhash_impl` with vectorized NumPy operations and `np.packbits`. Benchmarks indicate an ~12.5x speedup locally (3.14s -> 0.25s). Correctness is preserved by casting bit counts to `np.int32` before summing to properly handle negatives during the boolean vote.

Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules Bot and others added 3 commits June 4, 2026 03:46
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`.
🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences.
📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching.

Signed-off-by: Jules <jules@example.com>

Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`.
🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences.
📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching.

Signed-off-by: Jules <jules@example.com>

Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`.
🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences.
📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching.

Signed-off-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>

Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
SuarezPM added a commit that referenced this pull request Jun 4, 2026
Applies Jules PR #83 (uv.lock/log churn dropped).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Pablo <suarezpm@csnat.unt.edu.ar>
@SuarezPM

SuarezPM commented Jun 4, 2026

Copy link
Copy Markdown
Owner Author

Applied to main in b0c873d (vectorize simhash with numpy packbits). Thanks!

@SuarezPM SuarezPM closed this Jun 4, 2026
@google-labs-jules

Copy link
Copy Markdown
Contributor

Applied to main in b0c873d (vectorize simhash with numpy packbits). Thanks!

Acknowledged.

SuarezPM added a commit that referenced this pull request Jun 4, 2026
PR #83 vectorized _simhash_impl in embeddings/__init__.py, but that module's
EmbeddingEngine is a dead duplicate (no importer; the 3 real consumers use
embeddings.embedding_engine). This ports the numpy/packbits impl to the LIVE
embedding_engine._simhash_impl where simhash actually runs. Output verified
identical to the old loop; ~15x faster on CPU (16-4096 tokens, Ryzen 5 3600).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Pablo <suarezpm@csnat.unt.edu.ar>
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