Skip to content

Commit ad7475f

Browse files
committed
directly pass the offset and length to the Uint8ClampedArray constructor in wgpu.BytesToJS to improve performance and safety; should fix the Cannot perform Construct on a detached ArrayBuffer issue
1 parent c49c394 commit ad7475f

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

wgpu/util_js.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
// copying present in [js.CopyBytesToJS].
1313
func BytesToJS(b []byte) js.Value {
1414
ptr := uintptr(unsafe.Pointer(&b[0]))
15-
memoryBytes := js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"))
16-
// using subarray instead of slice gives a 5x performance improvement due to no copying
17-
return memoryBytes.Call("subarray", ptr, ptr+uintptr(len(b)))
15+
// We directly pass the offset and length to the constructor to avoid calling subarray or slice,
16+
// thereby improving performance and safety (this fixes a detached array buffer crash).
17+
return js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"), ptr, len(b))
1818
}
1919

2020
// mapSlice can be used to transform one slice into another by providing a

0 commit comments

Comments
 (0)