From 31ae8625ac5fcb2e64efefc4dc1a4aa879124234 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 13 Mar 2023 12:37:00 +0000 Subject: [PATCH 1/2] Reduce byteLength calls for JSTypedArray --- Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift index f82864e86..0b5e0b3f9 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift @@ -82,7 +82,7 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh let rawBuffer = malloc(bytesLength)! defer { free(rawBuffer) } _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) - let length = lengthInBytes / MemoryLayout.size + let length = bytesLength / MemoryLayout.size let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) let result = try body(bufferPtr) @@ -109,7 +109,7 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh let rawBuffer = malloc(bytesLength)! defer { free(rawBuffer) } _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) - let length = lengthInBytes / MemoryLayout.size + let length = bytesLength / MemoryLayout.size let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) let result = try await body(bufferPtr) From 07a06f12ebaee662e2f2c6522fabfd175cb1edaa Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 13 Mar 2023 12:55:04 +0000 Subject: [PATCH 2/2] Use UnsafeMutableBufferPointer.allocate instead of malloc directly --- .../BasicObjects/JSTypedArray.swift | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift index 0b5e0b3f9..963419c99 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift @@ -79,12 +79,16 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh /// - Returns: The return value, if any, of the `body` closure parameter. public func withUnsafeBytes(_ body: (UnsafeBufferPointer) throws -> R) rethrows -> R { let bytesLength = lengthInBytes - let rawBuffer = malloc(bytesLength)! - defer { free(rawBuffer) } - _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) + let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) + defer { rawBuffer.deallocate() } + let baseAddress = rawBuffer.baseAddress! + _load_typed_array(jsObject.id, baseAddress) let length = bytesLength / MemoryLayout.size - let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) - let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) + let rawBaseAddress = UnsafeRawPointer(baseAddress) + let bufferPtr = UnsafeBufferPointer( + start: rawBaseAddress.assumingMemoryBound(to: Element.self), + count: length + ) let result = try body(bufferPtr) return result } @@ -106,12 +110,16 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withUnsafeBytesAsync(_ body: (UnsafeBufferPointer) async throws -> R) async rethrows -> R { let bytesLength = lengthInBytes - let rawBuffer = malloc(bytesLength)! - defer { free(rawBuffer) } - _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) + let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) + defer { rawBuffer.deallocate() } + let baseAddress = rawBuffer.baseAddress! + _load_typed_array(jsObject.id, baseAddress) let length = bytesLength / MemoryLayout.size - let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) - let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) + let rawBaseAddress = UnsafeRawPointer(baseAddress) + let bufferPtr = UnsafeBufferPointer( + start: rawBaseAddress.assumingMemoryBound(to: Element.self), + count: length + ) let result = try await body(bufferPtr) return result }