From e06d03e8603e0be4d821365cc3cadda19e5e9de6 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 10 Aug 2021 12:05:22 -0600 Subject: [PATCH 1/2] [benchmark] initialize an UMBP from an URBP --- benchmark/single-source/BufferFill.swift | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/benchmark/single-source/BufferFill.swift b/benchmark/single-source/BufferFill.swift index 072703c63154a..37473211fdbe5 100644 --- a/benchmark/single-source/BufferFill.swift +++ b/benchmark/single-source/BufferFill.swift @@ -28,6 +28,11 @@ public let BufferFill = [ tags: [.validation, .api], setUpFunction: rawBufferInitializeMemorySetup, tearDownFunction: rawBufferInitializeMemoryTeardown), + BenchmarkInfo(name: "RawBuffer.copyContents", + runFunction: rawBufferCopyContentsExecute, + tags: [.validation, .api], + setUpFunction: rawBufferCopyContentsSetup, + tearDownFunction: rawBufferCopyContentsTeardown), ] let c = 100_000 @@ -119,3 +124,41 @@ public func rawBufferInitializeMemoryExecute(n: Int) { let value = offset.load(as: Int.self) CheckResults(value == a[r]) } + +var r8: UnsafeRawBufferPointer = .init(start: nil, count: 0) +var b8: UnsafeMutableBufferPointer = .init(start: nil, count: 0) + +public func rawBufferCopyContentsSetup() { + assert(r8.baseAddress == nil) + let count = a.count * MemoryLayout.stride + let rb = UnsafeMutableRawBufferPointer.allocate(byteCount: count, alignment: 8) + a.withUnsafeBytes { + rb.copyMemory(from: $0) + } + r8 = UnsafeRawBufferPointer(rb) + assert(b8.baseAddress == nil) + b8 = .allocate(capacity: rb.count) + r = rb.indices.randomElement()! +} + +public func rawBufferCopyContentsTeardown() { + r8.deallocate() + r8 = .init(start: nil, count: 0) + b8.deallocate() + b8 = .init(start: nil, count: 0) +} + +@inline(never) +public func rawBufferCopyContentsExecute(n: Int) { + // Measure performance of copying bytes from an + // `UnsafeRawBufferPointer` to an `UnsafeMutableBufferPointer`. + // See: https://bugs.swift.org/browse/SR-9604 + + for _ in 0.. Date: Thu, 12 Aug 2021 12:49:25 -0600 Subject: [PATCH 2/2] Update benchmark/single-source/BufferFill.swift Co-authored-by: Karoy Lorentey --- benchmark/single-source/BufferFill.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmark/single-source/BufferFill.swift b/benchmark/single-source/BufferFill.swift index 37473211fdbe5..b676cca06354e 100644 --- a/benchmark/single-source/BufferFill.swift +++ b/benchmark/single-source/BufferFill.swift @@ -131,7 +131,9 @@ var b8: UnsafeMutableBufferPointer = .init(start: nil, count: 0) public func rawBufferCopyContentsSetup() { assert(r8.baseAddress == nil) let count = a.count * MemoryLayout.stride - let rb = UnsafeMutableRawBufferPointer.allocate(byteCount: count, alignment: 8) + let rb = UnsafeMutableRawBufferPointer.allocate( + byteCount: count, + alignment: MemoryLayout.alignment) a.withUnsafeBytes { rb.copyMemory(from: $0) }