Skip to content

Conversation

@pverscha
Copy link
Owner

⚡ Bolt: Optimize defragmentation copying

💡 What:
Replaced the DataView-based byte-by-byte copy loop in defragment methods with a hybrid approach using Uint8Array.

  • Small blocks (<64 bytes): Manual copy loop (faster than subarray creation overhead).
  • Large blocks (>=64 bytes): Uint8Array.prototype.set with subarray (native speed).
  • Also fixed a bug in ShareableArray.defragment where DATA_OBJECT_OFFSET was erroneously added twice, creating 8-byte gaps between every object.

🎯 Why:
The original implementation used DataView.getUint8 / setUint8 in a loop, which is significantly slower than direct typed array access or bulk operations. Benchmarks showed Uint8Array.set is 10x-50x faster for large blocks, while manual loops are 2x faster for very small blocks.

📊 Impact:

  • ShareableMap defragmentation test time reduced from ~9.1s to ~7.8s (~14% faster).
  • ShareableArray space efficiency improved by removing unnecessary gaps.

🔬 Measurement:
Run npm test and observe the execution time of src/map/__tests__/ShareableMap.spec.ts, specifically the "should correctly defragment the map if required" test case.


PR created automatically by Jules for task 15823903321355726499 started by @pverscha

- Replaces slow `DataView` byte-by-byte copying with a hybrid `Uint8Array` strategy.
- Uses manual loops for small blocks (<64 bytes) to avoid TypedArray view creation overhead.
- Uses `Uint8Array.prototype.set` with `subarray` for larger blocks to leverage native optimized copying.
- Fixes a bug in `ShareableArray.defragment` where `DATA_OBJECT_OFFSET` was added twice to the data pointer, causing memory gaps.

Performance impact:
- Reduces `ShareableMap` defragmentation time by ~14% in tests.
- Significantly faster copying for larger items.
- Fixes incorrect memory usage in `ShareableArray`.

Co-authored-by: pverscha <9608686+pverscha@users.noreply.github.com>
@google-labs-jules
Copy link

👋 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.

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.

2 participants