Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions bridge/core/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"reflect"
"strconv"
"sync"

"github.com/grafana/sobek"
Expand Down Expand Up @@ -337,6 +338,21 @@ func wrapJSCallback(vm *sobek.Runtime, callable sobek.Callable, goType reflect.T
}

func bindSlice(vm *sobek.Runtime, v reflect.Value, visited map[uintptr]sobek.Value) (sobek.Value, error) {
// @optimized: fast-path for []byte using ArrayBuffer
if v.Type().Elem() == reflect.TypeOf(byte(0)) {
var bytes []byte
if v.CanAddr() || v.Kind() == reflect.Slice {
bytes = v.Bytes()
} else {
l := v.Len()
bytes = make([]byte, l)
reflect.Copy(reflect.ValueOf(bytes), v)
}
copied := make([]byte, len(bytes))
copy(copied, bytes)
return ToArrayBuffer(vm, copied), nil
Comment on lines +351 to +353
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.

}

// @optimized: Pre-allocate slice and use NewArray(vals...) to avoid repeated Set calls.
l := v.Len()
vals := make([]interface{}, l)
Expand All @@ -352,16 +368,23 @@ func bindSlice(vm *sobek.Runtime, v reflect.Value, visited map[uintptr]sobek.Val

func bindMap(vm *sobek.Runtime, v reflect.Value, visited map[uintptr]sobek.Value) (sobek.Value, error) {
obj := vm.NewObject()
for _, key := range v.MapKeys() {
// @optimized: MapRange iteration and strconv fast paths for primitives
iter := v.MapRange()
for iter.Next() {
key := iter.Key()
var keyStr string
// @optimized: Avoid Sprintf if key is already a string.
if key.Kind() == reflect.String {
switch key.Kind() {
case reflect.String:
keyStr = key.String()
} else {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
keyStr = strconv.FormatInt(key.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
keyStr = strconv.FormatUint(key.Uint(), 10)
default:
keyStr = fmt.Sprint(key.Interface())
}

val, err := bindValue(vm, v.MapIndex(key), visited)
val, err := bindValue(vm, iter.Value(), visited)
if err != nil {
return nil, err
}
Expand Down
Loading