|
| 1 | +//===--- StringEdits.swift ------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 let benchmarks: [BenchmarkInfo] = [ |
| 16 | + BenchmarkInfo( |
| 17 | + name: "StringDistance.characters.mixed", |
| 18 | + runFunction: { n in |
| 19 | + run_characters(string: mixedString, ranges: mixedRanges, n: n) |
| 20 | + }, |
| 21 | + tags: [.api, .String], |
| 22 | + setUpFunction: { blackHole(mixedRanges) }), |
| 23 | + BenchmarkInfo( |
| 24 | + name: "StringDistance.scalars.mixed", |
| 25 | + runFunction: { n in |
| 26 | + run_scalars(string: mixedString, ranges: mixedRanges, n: n) |
| 27 | + }, |
| 28 | + tags: [.api, .String], |
| 29 | + setUpFunction: { blackHole(mixedRanges) }), |
| 30 | + BenchmarkInfo( |
| 31 | + name: "StringDistance.utf16.mixed", |
| 32 | + runFunction: { n in |
| 33 | + run_utf16(string: mixedString, ranges: mixedRanges, n: n) |
| 34 | + }, |
| 35 | + tags: [.api, .String], |
| 36 | + setUpFunction: { blackHole(mixedRanges) }), |
| 37 | + BenchmarkInfo( |
| 38 | + name: "StringDistance.utf8.mixed", |
| 39 | + runFunction: { n in |
| 40 | + run_utf8(string: mixedString, ranges: mixedRanges, n: n) |
| 41 | + }, |
| 42 | + tags: [.api, .String], |
| 43 | + setUpFunction: { blackHole(mixedRanges) }), |
| 44 | + BenchmarkInfo( |
| 45 | + name: "StringDistance.characters.ascii", |
| 46 | + runFunction: { n in |
| 47 | + run_characters(string: asciiString, ranges: asciiRanges, n: n) |
| 48 | + }, |
| 49 | + tags: [.api, .String], |
| 50 | + setUpFunction: { blackHole(asciiRanges) }), |
| 51 | + BenchmarkInfo( |
| 52 | + name: "StringDistance.scalars.ascii", |
| 53 | + runFunction: { n in |
| 54 | + run_scalars(string: asciiString, ranges: asciiRanges, n: n) |
| 55 | + }, |
| 56 | + tags: [.api, .String], |
| 57 | + setUpFunction: { blackHole(asciiRanges) }), |
| 58 | + BenchmarkInfo( |
| 59 | + name: "StringDistance.utf16.ascii", |
| 60 | + runFunction: { n in |
| 61 | + run_utf16(string: asciiString, ranges: asciiRanges, n: n) |
| 62 | + }, |
| 63 | + tags: [.api, .String], |
| 64 | + setUpFunction: { blackHole(asciiRanges) }), |
| 65 | + BenchmarkInfo( |
| 66 | + name: "StringDistance.utf8.ascii", |
| 67 | + runFunction: { n in |
| 68 | + run_utf8(string: asciiString, ranges: asciiRanges, n: n) |
| 69 | + }, |
| 70 | + tags: [.api, .String], |
| 71 | + setUpFunction: { blackHole(asciiRanges) }), |
| 72 | +] |
| 73 | + |
| 74 | + |
| 75 | +let mixedString = #""" |
| 76 | + The powerful programming language that is also easy to learn. |
| 77 | + 손쉽게 학습할 수 있는 강력한 프로그래밍 언어. |
| 78 | + 🪙 A 🥞 short 🍰 piece 🫘 of 🌰 text 👨👨👧👧 with 👨👩👦 some 🚶🏽 emoji 🇺🇸🇨🇦 characters 🧈 |
| 79 | + some🔩times 🛺 placed 🎣 in 🥌 the 🆘 mid🔀dle 🇦🇶or🏁 around 🏳️🌈 a 🍇 w🍑o🥒r🥨d |
| 80 | + Unicode is such fun! |
| 81 | + U̷n̷i̷c̷o̴d̴e̷ ̶i̸s̷ ̸s̵u̵c̸h̷ ̸f̵u̷n̴!̵ |
| 82 | + U̴̡̲͋̾n̵̻̳͌ì̶̠̕c̴̭̈͘ǫ̷̯͋̊d̸͖̩̈̈́ḛ̴́ ̴̟͎͐̈i̴̦̓s̴̜̱͘ ̶̲̮̚s̶̙̞͘u̵͕̯̎̽c̵̛͕̜̓h̶̘̍̽ ̸̜̞̿f̵̤̽ṷ̴͇̎͘ń̷͓̒!̷͍̾̚ |
| 83 | + U̷̢̢̧̨̼̬̰̪͓̞̠͔̗̼̙͕͕̭̻̗̮̮̥̣͉̫͉̬̲̺͍̺͊̂ͅ\# |
| 84 | + n̶̨̢̨̯͓̹̝̲̣̖̞̼̺̬̤̝̊̌́̑̋̋͜͝ͅ\# |
| 85 | + ḭ̸̦̺̺͉̳͎́͑\# |
| 86 | + c̵̛̘̥̮̙̥̟̘̝͙̤̮͉͔̭̺̺̅̀̽̒̽̏̊̆͒͌̂͌̌̓̈́̐̔̿̂͑͠͝͝ͅ\# |
| 87 | + ö̶̱̠̱̤̙͚͖̳̜̰̹̖̣̻͎͉̞̫̬̯͕̝͔̝̟̘͔̙̪̭̲́̆̂͑̌͂̉̀̓́̏̎̋͗͛͆̌̽͌̄̎̚͝͝͝͝ͅ\# |
| 88 | + d̶̨̨̡̡͙̟͉̱̗̝͙͍̮͍̘̮͔͑\# |
| 89 | + e̶̢͕̦̜͔̘̘̝͈̪̖̺̥̺̹͉͎͈̫̯̯̻͑͑̿̽͂̀̽͋́̎̈́̈̿͆̿̒̈́̽̔̇͐͛̀̓͆̏̾̀̌̈́̆̽̕ͅ |
| 90 | + """# |
| 91 | + |
| 92 | +let mixedRanges = ( |
| 93 | + generateRanges(for: mixedString, by: 1) |
| 94 | + + generateRanges(for: mixedString, by: 2) |
| 95 | + + generateRanges(for: mixedString, by: 4) |
| 96 | + + generateRanges(for: mixedString, by: 8) |
| 97 | + + generateRanges(for: mixedString, by: 16) |
| 98 | + + generateRanges(for: mixedString, by: 32) |
| 99 | + + generateRanges(for: mixedString, by: 64) |
| 100 | + + generateRanges(for: mixedString, by: 128) |
| 101 | + + generateRanges(for: mixedString, by: 256) |
| 102 | + + generateRanges(for: mixedString, by: 512)) |
| 103 | + |
| 104 | +let _asciiString = #""" |
| 105 | + Swift is a high-performance system programming language. It has a clean |
| 106 | + and modern syntax, offers seamless access to existing C and Objective-C code |
| 107 | + and frameworks, and is memory safe by default. |
| 108 | +
|
| 109 | + Although inspired by Objective-C and many other languages, Swift is not itself |
| 110 | + a C-derived language. As a complete and independent language, Swift packages |
| 111 | + core features like flow control, data structures, and functions, with |
| 112 | + high-level constructs like objects, protocols, closures, and generics. Swift |
| 113 | + embraces modules, eliminating the need for headers and the code duplication |
| 114 | + they entail. |
| 115 | +
|
| 116 | + Swift toolchains are created using the script |
| 117 | + [build-toolchain](https://github.com/apple/swift/blob/main/utils/build-toolchain). |
| 118 | + This script is used by swift.org's CI to produce snapshots and can allow for |
| 119 | + one to locally reproduce such builds for development or distribution purposes. |
| 120 | + A typical invocation looks like the following: |
| 121 | +
|
| 122 | + ``` |
| 123 | + $ ./swift/utils/build-toolchain $BUNDLE_PREFIX |
| 124 | + ``` |
| 125 | +
|
| 126 | + where ``$BUNDLE_PREFIX`` is a string that will be prepended to the build date |
| 127 | + to give the bundle identifier of the toolchain's ``Info.plist``. For instance, |
| 128 | + if ``$BUNDLE_PREFIX`` was ``com.example``, the toolchain produced will have |
| 129 | + the bundle identifier ``com.example.YYYYMMDD``. It will be created in the |
| 130 | + directory you run the script with a filename of the form: |
| 131 | + ``swift-LOCAL-YYYY-MM-DD-a-osx.tar.gz``. |
| 132 | + """# |
| 133 | +let asciiString = String(repeating: _asciiString, count: 10) |
| 134 | + |
| 135 | +let asciiRanges = ( |
| 136 | + generateRanges(for: asciiString, by: 1) |
| 137 | + + generateRanges(for: asciiString, by: 2) |
| 138 | + + generateRanges(for: asciiString, by: 4) |
| 139 | + + generateRanges(for: asciiString, by: 8) |
| 140 | + + generateRanges(for: asciiString, by: 16) |
| 141 | + + generateRanges(for: asciiString, by: 32) |
| 142 | + + generateRanges(for: asciiString, by: 64) |
| 143 | + + generateRanges(for: asciiString, by: 128) |
| 144 | + + generateRanges(for: asciiString, by: 256) |
| 145 | + + generateRanges(for: asciiString, by: 512)) |
| 146 | + |
| 147 | +func generateRanges(for string: String, by step: Int) -> [Range<String.Index>] { |
| 148 | + var remaining = step |
| 149 | + var i = string.startIndex |
| 150 | + var last = i |
| 151 | + |
| 152 | + var ranges: [Range<String.Index>] = [] |
| 153 | + while i < string.endIndex { |
| 154 | + string.unicodeScalars.formIndex(after: &i) |
| 155 | + remaining -= 1 |
| 156 | + if remaining == 0 { |
| 157 | + ranges.append(last ..< i) |
| 158 | + remaining = step |
| 159 | + last = i |
| 160 | + } |
| 161 | + } |
| 162 | + ranges.append(last ..< i) |
| 163 | + return ranges |
| 164 | +} |
| 165 | + |
| 166 | +func run_characters(string: String, ranges: [Range<String.Index>], n: Int) { |
| 167 | + var c = 0 |
| 168 | + for _ in 0 ..< n { |
| 169 | + for r in ranges { |
| 170 | + c += string.distance(from: r.lowerBound, to: r.upperBound) |
| 171 | + } |
| 172 | + } |
| 173 | + blackHole(c) |
| 174 | +} |
| 175 | + |
| 176 | +func run_scalars(string: String, ranges: [Range<String.Index>], n: Int) { |
| 177 | + var c = 0 |
| 178 | + for _ in 0 ..< n { |
| 179 | + for r in ranges { |
| 180 | + c += string.unicodeScalars.distance(from: r.lowerBound, to: r.upperBound) |
| 181 | + } |
| 182 | + } |
| 183 | + blackHole(c) |
| 184 | +} |
| 185 | + |
| 186 | +func run_utf16(string: String, ranges: [Range<String.Index>], n: Int) { |
| 187 | + var c = 0 |
| 188 | + for _ in 0 ..< n { |
| 189 | + for r in ranges { |
| 190 | + c += string.utf16.distance(from: r.lowerBound, to: r.upperBound) |
| 191 | + } |
| 192 | + } |
| 193 | + blackHole(c) |
| 194 | +} |
| 195 | + |
| 196 | +func run_utf8(string: String, ranges: [Range<String.Index>], n: Int) { |
| 197 | + var c = 0 |
| 198 | + for _ in 0 ..< n { |
| 199 | + for r in ranges { |
| 200 | + c += string.utf8.distance(from: r.lowerBound, to: r.upperBound) |
| 201 | + } |
| 202 | + } |
| 203 | + blackHole(c) |
| 204 | +} |
0 commit comments