Skip to content

Commit

Permalink
directly pass the offset and length to the Uint8ClampedArray construc…
Browse files Browse the repository at this point in the history
…tor in wgpu.BytesToJS to improve performance and safety; should fix the Cannot perform Construct on a detached ArrayBuffer issue
  • Loading branch information
kkoreilly committed Dec 12, 2024
1 parent c49c394 commit ad7475f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions wgpu/util_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
// copying present in [js.CopyBytesToJS].
func BytesToJS(b []byte) js.Value {
ptr := uintptr(unsafe.Pointer(&b[0]))
memoryBytes := js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"))
// using subarray instead of slice gives a 5x performance improvement due to no copying
return memoryBytes.Call("subarray", ptr, ptr+uintptr(len(b)))
// We directly pass the offset and length to the constructor to avoid calling subarray or slice,
// thereby improving performance and safety (this fixes a detached array buffer crash).
return js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"), ptr, len(b))
}

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

0 comments on commit ad7475f

Please sign in to comment.