Skip to content

perf(bridge/core): optimize bindSlice and bindMap#161

Open
repyh wants to merge 1 commit into
mainfrom
optimize-reflection-core-10905898188319771187
Open

perf(bridge/core): optimize bindSlice and bindMap#161
repyh wants to merge 1 commit into
mainfrom
optimize-reflection-core-10905898188319771187

Conversation

@repyh
Copy link
Copy Markdown
Owner

@repyh repyh commented May 22, 2026

  • Optimized reflection of []byte by converting to ArrayBuffer directly.
  • Optimized mapping keys using v.MapRange() instead of v.MapKeys().
  • Addressed boxing issues and reduced object allocations.

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

Summary by CodeRabbit

  • Refactor
    • Enhanced JavaScript-Go binding conversions with improved handling of common Go container types. Binary data transfers more efficiently through optimized conversion methods. Map operations benefit from faster key processing and streamlined iteration mechanisms. String key generation optimized for more efficient inter-language data transfer overall.

Review Change Stack

- Add fast-path for converting `[]byte` into JS ArrayBuffer without per-element reflection overhead.
- Use `v.MapRange()` and `strconv` fast-paths for mapping map objects directly from keys.

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 22, 2026

📝 Walkthrough

Walkthrough

bridge/core/reflection.go adds optimized fast-paths for common Go container type conversions to JavaScript bindings. The strconv import supports efficient primitive key formatting. Byte slices now copy data directly to JS ArrayBuffer instead of binding per-element. Map iteration switches to MapRange() with optimized key stringification via strconv for integers and unsigned integers, with fallback to fmt.Sprint for other key types.

Changes

JS Binding Performance Optimizations

Layer / File(s) Summary
Byte slice and map binding fast-paths
bridge/core/reflection.go
Adds strconv import and implements dedicated fast-path for []byte conversion to JS ArrayBuffer. Map binding switches to MapRange() iteration and optimizes key-to-string conversion with strconv.FormatInt/FormatUint for numeric keys, with fallback to fmt.Sprint for other key types.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Bytes now fly swift to buffers bright,
Maps with keys of integer might,
No more loops through each tiny bit—
Fast paths make the bindings fit! ✨

🚥 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 title 'perf(bridge/core): optimize bindSlice and bindMap' directly and specifically describes the main changes in the pull request, which optimize the bindSlice and bindMap functions for better performance.
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 optimize-reflection-core-10905898188319771187

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@bridge/core/reflection.go`:
- Around line 351-353: The extra allocation and copy using "copied :=
make([]byte, len(bytes)); copy(copied, bytes)" before calling ToArrayBuffer is
redundant because ToArrayBuffer already copies the data; remove those two lines
and pass the original bytes slice directly to ToArrayBuffer(vm, bytes) in the
function where this occurs (the return site shown uses the bytes variable and
calls ToArrayBuffer), eliminating the needless allocation and memory copy.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 650d968f-0793-4e6b-8b79-57f58a098ffd

📥 Commits

Reviewing files that changed from the base of the PR and between b85f440 and 076cfe5.

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

Comment thread bridge/core/reflection.go
Comment on lines +351 to +353
copied := make([]byte, len(bytes))
copy(copied, bytes)
return ToArrayBuffer(vm, copied), nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Remove the redundant byte-slice copy before ToArrayBuffer.

ToArrayBuffer already creates a copy of the input bytes, so this extra copied := make([]byte, ...) + copy(...) adds avoidable allocation and memory copy on the fast-path.

Suggested diff
-		copied := make([]byte, len(bytes))
-		copy(copied, bytes)
-		return ToArrayBuffer(vm, copied), nil
+		return ToArrayBuffer(vm, bytes), nil
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
copied := make([]byte, len(bytes))
copy(copied, bytes)
return ToArrayBuffer(vm, copied), nil
return ToArrayBuffer(vm, bytes), nil
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@bridge/core/reflection.go` around lines 351 - 353, The extra allocation and
copy using "copied := make([]byte, len(bytes)); copy(copied, bytes)" before
calling ToArrayBuffer is redundant because ToArrayBuffer already copies the
data; remove those two lines and pass the original bytes slice directly to
ToArrayBuffer(vm, bytes) in the function where this occurs (the return site
shown uses the bytes variable and calls ToArrayBuffer), eliminating the needless
allocation and memory copy.

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