mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
add faster (Static)String write path
This commit is contained in:
1 parent
37cca97422
commit
02ff223eba
4 files changed
+59
-1
No files matched your search
@@ -330,6 +330,17 @@ public struct ByteBuffer : Equatable { // TODO: Comparable
|
||||
return nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func set(bytes: UnsafeBufferPointer<UInt8>, at index: Int) -> Int? {
|
||||
if expandIfNeeded(index: index, size: bytes.count) {
|
||||
let idx = applyOffset(index)
|
||||
self.backingData.replaceSubrange(idx..<idx + bytes.count, with: bytes)
|
||||
return bytes.count
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func set(data: Data, at index: Int) -> Int? {
|
||||
if expandIfNeeded(index: index, size: data.count) {
|
||||
let idx = applyOffset(index)
|
||||
@@ -367,6 +378,16 @@ public struct ByteBuffer : Equatable { // TODO: Comparable
|
||||
// TODO: indexOf, bytesBefore, forEachByte, backing byte array access?
|
||||
|
||||
// TODO: Generics to avoid this?
|
||||
|
||||
@discardableResult
|
||||
public mutating func write(staticString string: StaticString) -> Int? {
|
||||
if let bytes = set(staticString: string, at: writerIndex) {
|
||||
writerIndex += bytes
|
||||
return bytes
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func write(string: String) -> Int? {
|
||||
if let bytes = set(string: string, at: writerIndex) {
|
||||
@@ -376,6 +397,15 @@ public struct ByteBuffer : Equatable { // TODO: Comparable
|
||||
return nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func set(staticString string: StaticString, at index: Int) -> Int? {
|
||||
return string.withUTF8Buffer { buffer in
|
||||
print("set buffer at \(index)")
|
||||
return self.set(bytes: buffer, at: index)
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func set(string: String, at index: Int) -> Int? {
|
||||
let utf8 = string.utf8
|
||||
let count = utf8.count
|
||||
|
||||
@@ -25,7 +25,7 @@ public protocol ChannelOutboundInvoker {
|
||||
func connect(remote: SocketAddress) -> Future<Void>
|
||||
@discardableResult func connect(remote: SocketAddress, promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
func write(data: Any) -> Future<Void>
|
||||
@discardableResult func write(data: Any) -> Future<Void>
|
||||
@discardableResult func write(data: Any, promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
func flush()
|
||||
@@ -53,10 +53,12 @@ public extension ChannelOutboundInvoker {
|
||||
return connect(remote: remote, promise: newVoidPromise())
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func write(data: Any) -> Future<Void> {
|
||||
return write(data: data, promise: newVoidPromise())
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func writeAndFlush(data: Any) -> Future<Void> {
|
||||
return writeAndFlush(data: data, promise: newVoidPromise())
|
||||
}
|
||||
|
||||
@@ -315,6 +315,7 @@ public class ChannelPipeline : ChannelInboundInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func write(data: Any, promise: Promise<Void>) -> Future<Void> {
|
||||
if eventLoop.inEventLoop {
|
||||
tail!.invokeWrite(data: data, promise: promise)
|
||||
@@ -326,6 +327,7 @@ public class ChannelPipeline : ChannelInboundInvoker {
|
||||
return promise.futureResult
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void> {
|
||||
if eventLoop.inEventLoop {
|
||||
tail!.invokeWriteAndFlush(data: data, promise: promise)
|
||||
|
||||
@@ -52,6 +52,30 @@ class ByteBufferTest: XCTestCase {
|
||||
buf.withReadPointer(body: { ptr, size in XCTAssertEqual(12, size) })
|
||||
}
|
||||
|
||||
func testStaticStringReadTests() throws {
|
||||
var allBytes = 0
|
||||
for testString in ["", "Hello world!", "👍", "🇬🇧🇺🇸🇪🇺"] as [StaticString] {
|
||||
buf.withReadPointer(body: { ptr, size in
|
||||
XCTAssertEqual(0, size)
|
||||
})
|
||||
|
||||
let bytes = buf.write(staticString: testString)
|
||||
XCTAssertEqual(testString.utf8CodeUnitCount, bytes)
|
||||
allBytes += bytes!
|
||||
|
||||
let expected = testString.withUTF8Buffer { buf in
|
||||
String(bytes: buf, encoding: .utf8)
|
||||
}
|
||||
buf.withReadPointer { ptr, size in
|
||||
let actual = String(bytes: UnsafeBufferPointer<UInt8>(start: ptr, count: size), encoding: .utf8)
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
let d = buf.read(length: testString.utf8CodeUnitCount)
|
||||
XCTAssertNotNil(d)
|
||||
XCTAssertEqual(expected, String(bytes: d!, encoding: .utf8))
|
||||
}
|
||||
}
|
||||
|
||||
func testWriteStringMovesWriterIndex() {
|
||||
var buf = try! allocator.buffer(capacity: 1024)
|
||||
buf.write(string: "hello")
|
||||
|
||||
Reference in new issue
Block a user