From 89e5574bb48710e71936b67a897e27aef07ea33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Wei=C3=9F?= Date: Thu, 26 Apr 2018 14:41:49 +0100 Subject: [PATCH] fix optimiser problem triggered in BB.set(staticString:) (#354) Motivation: There's a presumed optimiser bug which will trigger an allocation when using `self` within `staticString.withUTF8Buffer { ... }` which makes it cause an extra allocation. Modifications: use StaticString.utf8Start instead of withUTF8Buffer Result: less allocations --- Sources/NIO/ByteBuffer-aux.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/NIO/ByteBuffer-aux.swift b/Sources/NIO/ByteBuffer-aux.swift index 17149d58e..fc8df682f 100644 --- a/Sources/NIO/ByteBuffer-aux.swift +++ b/Sources/NIO/ByteBuffer-aux.swift @@ -73,9 +73,9 @@ extension ByteBuffer { /// - index: The index for the first serialized byte. /// - returns: The number of bytes written. public mutating func set(staticString string: StaticString, at index: Int) -> Int { - return string.withUTF8Buffer { ptr -> Int in - self.set(bytes: UnsafeRawBufferPointer(ptr), at: index) - } + // please do not replace the code below with code that uses `string.withUTF8Buffer { ... }` (see SR-7541) + return self.set(bytes: UnsafeRawBufferPointer(start: string.utf8Start, + count: string.utf8CodeUnitCount), at: index) } // MARK: String APIs