|
| 1 | +//===--- NaiveRangeReplaceableCollectionConformance.swift -----------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | + |
| 15 | +var contiguous:[UInt8] = [] |
| 16 | + |
| 17 | +public let benchmarks = [ |
| 18 | + BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", |
| 19 | + runFunction: runAppendLargeContiguous, |
| 20 | + tags: [.validation, .api], |
| 21 | + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }), |
| 22 | + BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeated", |
| 23 | + runFunction: runAppendSmallContiguousRepeatedly, |
| 24 | + tags: [.validation, .api], |
| 25 | + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }), |
| 26 | + BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", |
| 27 | + runFunction: runInitLargeContiguous, |
| 28 | + tags: [.validation, .api], |
| 29 | + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }) |
| 30 | +] |
| 31 | + |
| 32 | +struct NaiveRRC : RangeReplaceableCollection { |
| 33 | + |
| 34 | + var storage:[UInt8] = [] |
| 35 | + |
| 36 | + init() {} |
| 37 | + |
| 38 | + func index(after i: Int) -> Int { |
| 39 | + i + 1 |
| 40 | + } |
| 41 | + |
| 42 | + func index(before i: Int) -> Int { |
| 43 | + i - 1 |
| 44 | + } |
| 45 | + |
| 46 | + var startIndex: Int { |
| 47 | + 0 |
| 48 | + } |
| 49 | + |
| 50 | + var endIndex: Int { |
| 51 | + count |
| 52 | + } |
| 53 | + |
| 54 | + var count: Int { |
| 55 | + storage.count |
| 56 | + } |
| 57 | + |
| 58 | + subscript(position: Int) -> UInt8 { |
| 59 | + get { |
| 60 | + storage[position] |
| 61 | + } |
| 62 | + set(newValue) { |
| 63 | + storage[position] = newValue |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + mutating func replaceSubrange(_ subrange: Range<Int>, with newElements: some Collection<UInt8>) { |
| 68 | + storage.replaceSubrange(subrange, with: newElements) |
| 69 | + } |
| 70 | + |
| 71 | + mutating func reserveCapacity(_ n: Int) { |
| 72 | + storage.reserveCapacity(n) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@inline(never) |
| 77 | +public func runAppendLargeContiguous(N: Int) { |
| 78 | + for _ in 1...N { |
| 79 | + var rrc = NaiveRRC() |
| 80 | + rrc.append(contentsOf: contiguous) |
| 81 | + blackHole(rrc) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +@inline(never) |
| 86 | +public func runAppendSmallContiguousRepeatedly(N: Int) { |
| 87 | + for _ in 1...N { |
| 88 | + var rrc = NaiveRRC() |
| 89 | + for _ in 1...5_000 { |
| 90 | + rrc.append(contentsOf: contiguous) |
| 91 | + } |
| 92 | + blackHole(rrc) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +@inline(never) |
| 97 | +public func runInitLargeContiguous(N: Int) { |
| 98 | + for _ in 1...N { |
| 99 | + blackHole(NaiveRRC(contiguous)) |
| 100 | + } |
| 101 | +} |
0 commit comments