Files
swift-nio/Sources/NIOPerformanceTester/ByteBufferViewIteratorBenchmark.swift
T
Johannes Weiss c6e5fe88e1 ByteBuffer: make getInteger for UInt8 faster (#1380)
Motivation:

getInteger used the normal, generic implementation for UInt8s, however
they can be made considerably faster by using a specialised method.
ByteBufferIterator was also affected by this.

Modifications:

Use a specialised version for UInt8s for getInteger.

Result:

- ByteBufferView can be iterated about 20% faster.
2020-02-06 11:59:10 +00:00

50 lines
1.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 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 ByteBufferViewIteratorBenchmark: Benchmark {
private let iterations: Int
private let bufferSize: Int
private var buffer: ByteBuffer
init(iterations: Int, bufferSize: Int) {
self.iterations = iterations
self.bufferSize = bufferSize
self.buffer = ByteBufferAllocator().buffer(capacity: self.bufferSize)
}
func setUp() throws {
self.buffer.writeBytes(Array(repeating: UInt8(ascii: "A"), count: self.bufferSize - 1))
self.buffer.writeInteger(UInt8(ascii: "B"))
}
func tearDown() {
}
func run() -> Int {
var which: UInt8 = 0
for _ in 1...self.iterations {
for byte in self.buffer.readableBytesView {
if byte != UInt8(ascii: "A") {
which = byte
}
}
}
return Int(which)
}
}