Skip to content

perf: optimize bindSlice and bindMap allocations#169

Open
repyh wants to merge 1 commit into
mainfrom
chore/bindslice-bindmap-optimizations-17481360139873684640
Open

perf: optimize bindSlice and bindMap allocations#169
repyh wants to merge 1 commit into
mainfrom
chore/bindslice-bindmap-optimizations-17481360139873684640

Conversation

@repyh
Copy link
Copy Markdown
Owner

@repyh repyh commented May 30, 2026

These optimizations remove substantial garbage collection and memory overhead from TypeGo's core reflection bridge when handling large buffers and map structures.


PR created automatically by Jules for task 17481360139873684640 started by @repyh

Summary by CodeRabbit

  • Performance Improvements
    • Improved performance when handling byte slices during Go-to-JavaScript conversions
    • Optimized map iteration and numeric key handling for better runtime efficiency

Review Change Stack

Optimizes the `bindSlice` and `bindMap` functions in `bridge/core/reflection.go` to eliminate significant memory allocations and reduce boxing overhead:

- `bindSlice`: Avoids copying byte slices element-by-element (which creates 1 interface per byte) by detecting `[]byte` and using zero-copy extraction with `v.Bytes()`. The extracted data is then explicitly copied for memory safety and wrapped inside a JS `Uint8Array`. Benchmark time dropped from ~68.8M ns/op to ~1.0M ns/op for 1MB buffers.
- `bindMap`: Prevents allocating a slice of keys by replacing `v.MapKeys()` with a `v.MapRange()` iterator. It also leverages `strconv` instead of `fmt.Sprint` for primitive numeric keys to sidestep interface boxing latency. Benchmark time improved from ~766k ns/op down to ~656k ns/op.

Co-authored-by: repyh <63894915+repyh@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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1cac3cf7-241b-4443-9e65-c42191bae2ee

📥 Commits

Reviewing files that changed from the base of the PR and between b85f440 and 9c61dfb.

📒 Files selected for processing (1)
  • bridge/core/reflection.go

📝 Walkthrough

Walkthrough

This PR optimizes Go→JavaScript reflection bindings in bridge/core/reflection.go by adding a fast path for byte slices to convert directly to ArrayBuffer/Uint8Array, and improving map binding efficiency through zero-allocation iteration and faster numeric key stringification.

Changes

Reflection Binding Performance

Layer / File(s) Summary
Byte slice to ArrayBuffer fast path
bridge/core/reflection.go
bindSlice detects []byte slices, copies bytes to an ArrayBuffer, and wraps as Uint8Array when the runtime constructor is available; otherwise returns the raw ArrayBuffer.
Map iteration and key stringification optimization
bridge/core/reflection.go
strconv import is added; bindMap replaces MapKeys() iteration with MapRange() to avoid intermediate allocation, and stringifies integer/unsigned integer keys via strconv.FormatInt/FormatUint instead of fmt.Sprint.

🎯 2 (Simple) | ⏱️ ~12 minutes

🐰 Bytes now hop straight to buffers bright,
Maps iterate lighter, keys stringify right!
No slices delayed, no allocations to spare—
Just swift reflection magic in the air. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'perf: optimize bindSlice and bindMap allocations' accurately summarizes the main changes: performance optimizations to bindSlice and bindMap functions to reduce memory allocations.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/bindslice-bindmap-optimizations-17481360139873684640

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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