mirror of
https://github.com/apple/swift-nio.git
synced 2026-05-20 20:30:36 +00:00
Motivation: Protocols like Sequence have "private" hooks that can be implemented to provide fast-paths for some operations. We missed a few on BBV, and I'd like to add them. This is one use-case where they can help. Modifications: - Add an allocation-counter benchmark and a runtime benchmark for copying BBV to Array. Result: We have some benchmarks.
45 lines
1.1 KiB
Swift
45 lines
1.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2022 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 NIOCore
|
|
|
|
final class ByteBufferViewCopyToArrayBenchmark: Benchmark {
|
|
private let iterations: Int
|
|
private let size: Int
|
|
private var view: ByteBufferView
|
|
|
|
init(iterations: Int, size: Int) {
|
|
self.iterations = iterations
|
|
self.size = size
|
|
self.view = ByteBufferView()
|
|
}
|
|
|
|
func setUp() throws {
|
|
self.view = ByteBuffer(repeating: 0xfe, count: self.size).readableBytesView
|
|
}
|
|
|
|
func tearDown() {
|
|
}
|
|
|
|
func run() -> Int {
|
|
var count = 0
|
|
for _ in 0..<self.iterations {
|
|
let array = Array(self.view)
|
|
count &+= array.count
|
|
}
|
|
|
|
return count
|
|
}
|
|
}
|