mirror of
https://github.com/apple/swift-nio
synced 2026-11-29 05:24:21 +00:00
Motivation: When calling ByteBuffer setBytes/writeBytes with CircularBuffer<Uint8> we take the slow path. Before we make any changes we want a baseline for current performance. Modifications: - This gives a very significant performance increase for CircularBuffer and likely other types that use the slow path. - Adds ByteBuferBenchmark.swift and tests circular_buffer_into_byte_buffer_1kb, circular_buffer_into_byte_buffer_1mb. - Make _setSlowPath @inlinable - Implement == for CircularBuffer.Index to make it @inlinable Result: - Extra performance tests - Improve performance
49 lines
1.4 KiB
Swift
49 lines
1.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
import NIO
|
|
|
|
final class ByteBufferBenchmark: Benchmark {
|
|
private let iterations: Int
|
|
private let bufferSize: Int
|
|
private var circularBuffer: CircularBuffer<UInt8>
|
|
private var buffer: ByteBuffer
|
|
|
|
init(iterations: Int, bufferSize: Int) {
|
|
self.iterations = iterations
|
|
self.bufferSize = bufferSize
|
|
self.circularBuffer = CircularBuffer<UInt8>(initialCapacity: self.bufferSize)
|
|
self.buffer = ByteBufferAllocator().buffer(capacity: self.bufferSize)
|
|
}
|
|
|
|
func setUp() throws {
|
|
for i in 0..<self.bufferSize {
|
|
self.circularBuffer.append(UInt8(i % 256))
|
|
}
|
|
}
|
|
|
|
func tearDown() {
|
|
}
|
|
|
|
func run() -> Int {
|
|
for _ in 1...self.iterations {
|
|
self.buffer.writeBytes(self.circularBuffer)
|
|
self.buffer.setBytes(self.circularBuffer, at: 0)
|
|
self.buffer.clear()
|
|
}
|
|
return 1
|
|
}
|
|
}
|