Skip to content

perf: Daily incremental optimization to TypeGo core bridge and event loop#148

Open
repyh wants to merge 2 commits into
mainfrom
daily-opt-bridge-core-12838533661778031702
Open

perf: Daily incremental optimization to TypeGo core bridge and event loop#148
repyh wants to merge 2 commits into
mainfrom
daily-opt-bridge-core-12838533661778031702

Conversation

@repyh
Copy link
Copy Markdown
Owner

@repyh repyh commented May 9, 2026

Implemented several micro-optimizations inside the TypeGo core bridge logic:

  1. JS Bridge Performance: Optimized iteration inside reflection arrays and maps for Sobek interoperability.
  2. Structural Stability: Prevented double execution on WaitGroup completions inside Promises.
  3. Cache Lines: Grouped frequently hit mu, running, and autoStop fields together separated from heavier primitives inside EventLoop struct.

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed promise settlement execution to occur exactly once, preventing potential issues that could arise from multiple callback invocations in edge cases.
  • Performance Improvements

    • Enhanced byte array conversion efficiency when interfacing with the JavaScript runtime.
    • Optimized performance of map data binding through improved iteration and key formatting logic.
    • Refined event loop promise handling for improved reliability and consistency.

Review Change Stack

…s to the `bridge/core` and `eventloop` components. Here is a breakdown of the changes I've made:

- I refactored `bindMap` to use `v.MapRange()` with specialized `strconv` formatting for integer keys, which avoids unnecessary allocations and `fmt.Sprint` overhead.
- I added a fast-path in `bindSlice` for `[]byte` that utilizes `Uint8Array` directly, preventing full interface boxing overhead while safeguarding memory via a buffer copy.
- I enhanced `ToUint8Array` in `arraybuffer.go` to robustly check for global object existence.
- I refactored `CreatePromise` in `eventloop.go` to ensure `.Done()` strictly fires only once by utilizing `sync.Once`.
- I reordered the fields within the `EventLoop` struct to optimize memory alignment and CPU cache locality for intensely hit variables protected under the same mutex.

Let me know if you would like me to explain any of these changes in more detail!

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

Warning

Rate limit exceeded

@repyh has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 48 minutes and 5 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ce1d6ee2-32b1-4659-9300-d0229071b7f8

📥 Commits

Reviewing files that changed from the base of the PR and between ab8fb9a and b50937f.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • examples/projects/link-shortener/typego.modules.json
  • go.mod
  • typego
📝 Walkthrough

Walkthrough

This PR optimizes the JavaScript-Go bridge layer by introducing safe byte array conversion to prevent JS mutation of Go memory, improving reflection binding performance through numeric key formatting, and guarding promise callbacks against double-settlement via sync.Once.

Changes

JS-Go Bridge Performance and Safety

Layer / File(s) Summary
Byte Array Type Contract
bridge/core/arraybuffer.go
ToUint8Array function converts Go []byte to JavaScript Uint8Array backed by ArrayBuffer, with fallback when typed array constructor unavailable.
Reflection Optimization Imports
bridge/core/reflection.go
strconv import added to enable optimized numeric type-to-string formatting in reflection binding.
Byte Slice Binding
bridge/core/reflection.go
Slice binding copies byte arrays and converts via ToUint8Array to prevent JavaScript mutation of Go memory.
Map Binding Optimization
bridge/core/reflection.go
Map iteration switches from MapKeys/MapIndex to MapRange and uses strconv for numeric key string conversion; other types fallback to fmt.Sprint.
Promise Callback Safety
eventloop/eventloop.go
CreatePromise callbacks guarded by sync.Once to ensure single JS-thread settlement and WaitGroup.Done() even if both handlers invoked. EventLoop struct fields reorganized with @optimized comments.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 Bytes now traverse the bridge so safe,
Once guards the promise, never late,
Reflection blooms with strconv's grace,
Go and JS meet in their place. 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 accurately describes the main objective of the pull request as performance optimization to TypeGo core bridge and event loop components, which matches the changeset in arraybuffer.go, reflection.go, and eventloop.go.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch daily-opt-bridge-core-12838533661778031702

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.

…mple

The latest version of `github.com/gin-gonic/gin` (v1.12.0) requires Go 1.25, but the link-shortener project's `typego.modules.json` restricts the compiler version to `goVersion: 1.24`. This constraint mismatch caused the CI build check to fail with "package requires newer Go version". Pinning `gin` to `v1.11.0` ensures compatibility with Go 1.24.

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