|
| 1 | +//===--- BufferFind.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 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 | +public var benchmarks: [BenchmarkInfo] = [ |
| 16 | + BenchmarkInfo( |
| 17 | + name: "RawBuffer.100000.findFirst", |
| 18 | + runFunction: run_BufferFindFirst, |
| 19 | + tags: [.validation, .api], |
| 20 | + setUpFunction: buffer100000Setup, |
| 21 | + tearDownFunction: bufferTeardown |
| 22 | + ), |
| 23 | + BenchmarkInfo( |
| 24 | + name: "RawBuffer.100000.findLast", |
| 25 | + runFunction: run_BufferFindLast, |
| 26 | + tags: [.validation, .api], |
| 27 | + setUpFunction: buffer100000Setup, |
| 28 | + tearDownFunction: bufferTeardown |
| 29 | + ), |
| 30 | + // size 1000, alignment 0 |
| 31 | + BenchmarkInfo( |
| 32 | + name: "RawBuffer.1000.findFirst", |
| 33 | + runFunction: run_BufferFindFirst, |
| 34 | + tags: [.validation, .api], |
| 35 | + setUpFunction: buffer1000Setup, |
| 36 | + tearDownFunction: bufferTeardown |
| 37 | + ), |
| 38 | + BenchmarkInfo( |
| 39 | + name: "RawBuffer.1000.findLast", |
| 40 | + runFunction: run_BufferFindLast, |
| 41 | + tags: [.validation, .api], |
| 42 | + setUpFunction: buffer1000Setup, |
| 43 | + tearDownFunction: bufferTeardown |
| 44 | + ), |
| 45 | + // size 100, alignment 0 |
| 46 | + BenchmarkInfo( |
| 47 | + name: "RawBuffer.100.findFirst", |
| 48 | + runFunction: run_BufferFindFirst, |
| 49 | + tags: [.validation, .api], |
| 50 | + setUpFunction: buffer100Setup, |
| 51 | + tearDownFunction: bufferTeardown |
| 52 | + ), |
| 53 | + BenchmarkInfo( |
| 54 | + name: "RawBuffer.100.findLast", |
| 55 | + runFunction: run_BufferFindLast, |
| 56 | + tags: [.validation, .api], |
| 57 | + setUpFunction: buffer100Setup, |
| 58 | + tearDownFunction: bufferTeardown |
| 59 | + ), |
| 60 | + // size 10, alignment 0 |
| 61 | + BenchmarkInfo( |
| 62 | + name: "RawBuffer.10.findFirst", |
| 63 | + runFunction: run_BufferFindFirst, |
| 64 | + tags: [.validation, .api], |
| 65 | + setUpFunction: buffer10Setup, |
| 66 | + tearDownFunction: bufferTeardown |
| 67 | + ), |
| 68 | + BenchmarkInfo( |
| 69 | + name: "RawBuffer.10.findLast", |
| 70 | + runFunction: run_BufferFindLast, |
| 71 | + tags: [.validation, .api], |
| 72 | + setUpFunction: buffer10Setup, |
| 73 | + tearDownFunction: bufferTeardown |
| 74 | + ) |
| 75 | +] |
| 76 | + |
| 77 | +var buffer: UnsafeMutableRawBufferPointer = .init(start: nil, count: 0) |
| 78 | + |
| 79 | +func buffer100000Setup() { |
| 80 | + bufferSetup(size: 100_000, alignment: 0) |
| 81 | +} |
| 82 | + |
| 83 | +func buffer1000Setup() { |
| 84 | + bufferSetup(size: 1000, alignment: 0) |
| 85 | +} |
| 86 | + |
| 87 | +func buffer100Setup() { |
| 88 | + bufferSetup(size: 100, alignment: 0) |
| 89 | +} |
| 90 | + |
| 91 | +func buffer10Setup() { |
| 92 | + bufferSetup(size: 10, alignment: 0) |
| 93 | +} |
| 94 | + |
| 95 | +func bufferTeardown() { |
| 96 | + buffer.deallocate() |
| 97 | + buffer = .init(start: nil, count: 0) |
| 98 | +} |
| 99 | + |
| 100 | +func bufferSetup(size: Int, alignment: Int) { |
| 101 | + buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: size, alignment: alignment) |
| 102 | + buffer.initializeMemory(as: UInt8.self, repeating: UInt8.min) |
| 103 | + buffer[size / 2] = UInt8.max |
| 104 | +} |
| 105 | + |
| 106 | +@inline(never) |
| 107 | +public func run_BufferFindFirst(n: Int) { |
| 108 | + var offset = 0 |
| 109 | + for _ in 0 ..< n * 10 { |
| 110 | + if let index = buffer.firstIndex(of: UInt8.max) { |
| 111 | + offset += index |
| 112 | + } |
| 113 | + } |
| 114 | + blackHole(offset) |
| 115 | +} |
| 116 | + |
| 117 | +@inline(never) |
| 118 | +public func run_BufferFindLast(n: Int) { |
| 119 | + var offset = 0 |
| 120 | + for _ in 0 ..< n * 10 { |
| 121 | + if let index = buffer.lastIndex(of: UInt8.max) { |
| 122 | + offset += index |
| 123 | + } |
| 124 | + } |
| 125 | + blackHole(offset) |
| 126 | +} |
0 commit comments