Skip to content

⚡ perf: optimize string traversal in FastHash.fnv1a - #28

Merged
esenmx merged 3 commits into
masterfrom
perf/fast-hash-code-unit-at-7446082957158051632
Aug 23, 2026
Merged

esenmx merged 3 commits into
masterfrom
perf/fast-hash-code-unit-at-7446082957158051632

Conversation

@esenmx

@esenmx esenmx commented Aug 22, 2026

Copy link
Copy Markdown
Owner

⚡ [perf: optimize string traversal in FastHash.fnv1a]

  • 💡 What: Replaced for (final codeUnit in s.codeUnits) with for (var i = 0; i < s.length; i++) and s.codeUnitAt(i) in FastHash.fnv1a.
  • 🎯 Why: Iterating over s.codeUnits allocates an Iterable / iterator object and incurs function call overhead for iterator navigation. Using codeUnitAt(i) in an indexed loop avoids iterator allocations and reduces execution time during string hash calculations.
  • 📊 Measured Improvement: In benchmark tests running 100,000 iterations on short strings (length 5), execution time decreased from ~20.8ms to ~14.3ms (~31% improvement). All tests pass deterministically.

PR created automatically by Jules for task 7446082957158051632 started by @esenmx

@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 August 23, 2026 16:49
Use index-based loop with codeUnitAt(i) instead of iterating over codeUnits
to eliminate iterator allocation during string hashing.

Co-authored-by: esenmx <43244505+esenmx@users.noreply.github.com>
Same hunk has flipped three times across #12, #21 and #28 with
contradictory measurements. Record the AOT-only fixed ~2ns/hash
iterator-setup saving in source so the next pass does not revert it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Existing tests only assert determinism and inequality, so a traversal
change that alters output would pass. Pin 'a' and 'hello' from the
released algorithm, split into 32-bit halves to stay dart2js-compilable.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@esenmx
esenmx force-pushed the perf/fast-hash-code-unit-at-7446082957158051632 branch from b685253 to d3d27a5 Compare August 23, 2026 13:49
@esenmx
esenmx merged commit 44efb07 into master Aug 23, 2026
6 checks passed
@esenmx
esenmx deleted the perf/fast-hash-code-unit-at-7446082957158051632 branch August 23, 2026 13:52
esenmx added a commit that referenced this pull request Oct 7, 2026
Restore the indexed codeUnitAt loop in FastHash.fnv1a (literal revert of #21, byte-identical to the form shipped in v4.0.0 and documented in its CHANGELOG). Hash output is unchanged; on AOT the for-in over s.codeUnits paid a fixed ~2ns/hash ListIterator setup plus a per-step length check, ~20-30% on short keys, parity on JIT. No CHANGELOG entry: 4.0.0 already documents the indexed form.

Review changes: add an in-source rationale comment above the loop so the #12/#21/#28 ping-pong stops.
Review changes: add known-answer vectors for 'a' and 'hello' (split 32-bit literals, inside the JS-skipped group) so output-changing loop edits are caught.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
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