jsutil: Avoid allocating ArrayBuffer unnecessarily

This commit is contained in:
Hajime Hoshi 2019-09-26 23:59:00 +09:00
parent 9a42b264fd
commit 59333ba1ad

View File

@ -96,44 +96,56 @@ func sliceToByteSlice(s interface{}) (bs []byte) {
return return
} }
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
func getTemporaryUint8Array(size int) js.Value {
if l := temporaryBuffer.Get("byteLength").Int(); l < size {
for l < size {
l *= 2
}
temporaryBuffer = js.Global().Get("ArrayBuffer").New(l)
}
return js.Global().Get("Uint8Array").New(temporaryBuffer, 0, size)
}
func SliceToTypedArray(s interface{}) (js.Value, func()) { func SliceToTypedArray(s interface{}) (js.Value, func()) {
switch s := s.(type) { switch s := s.(type) {
case []int8: case []int8:
a := js.Global().Get("Uint8Array").New(len(s)) a := getTemporaryUint8Array(len(s))
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {} return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
case []int16: case []int16:
a := js.Global().Get("Uint8Array").New(len(s) * 2) a := getTemporaryUint8Array(len(s) * 2)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {} return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
case []int32: case []int32:
a := js.Global().Get("Uint8Array").New(len(s) * 4) a := getTemporaryUint8Array(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {} return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
case []uint8: case []uint8:
a := js.Global().Get("Uint8Array").New(len(s)) a := getTemporaryUint8Array(len(s))
js.CopyBytesToJS(a, s) js.CopyBytesToJS(a, s)
return a, func() {} return a, func() {}
case []uint16: case []uint16:
a := js.Global().Get("Uint8Array").New(len(s) * 2) a := getTemporaryUint8Array(len(s) * 2)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {} return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
case []uint32: case []uint32:
a := js.Global().Get("Uint8Array").New(len(s) * 4) a := getTemporaryUint8Array(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {} return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
case []float32: case []float32:
a := js.Global().Get("Uint8Array").New(len(s) * 4) a := getTemporaryUint8Array(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {} return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
case []float64: case []float64:
a := js.Global().Get("Uint8Array").New(len(s) * 8) a := getTemporaryUint8Array(len(s) * 8)
js.CopyBytesToJS(a, sliceToByteSlice(s)) js.CopyBytesToJS(a, sliceToByteSlice(s))
buf := a.Get("buffer") buf := a.Get("buffer")
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {} return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}