Merge pull request #486 from lorentey/BigString-easy-speedups

[BigString] Harvest some low-hanging performance fruit
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2025-06-24 15:05:22 -07:00
commit 96bf8b85f5
4 files changed
+586 -6

No files matched your search

+192
View File
@@ -0,0 +1,192 @@
{
"kind": "group",
"title": "BigString Benchmarks",
"directory": "BigString",
"contents": [
{
"kind": "group",
"title": "BigString operations",
"directory": "BigString",
"contents": [
{
"kind": "chart",
"title": "ingestion",
"tasks": [
"BigString init from native string",
"BigString init from bridged string"
]
},
{
"kind": "chart",
"title": "sequential iteration",
"tasks": [
"BigString character iteration",
"BigString scalar iteration",
"BigString UTF-16 iteration",
"BigString UTF-8 iteration"
]
},
{
"kind": "chart",
"title": "index-offset-by",
"tasks": [
"BigString.index(offsetBy:)",
"BigString.unicodeScalars.index(offsetBy:)",
"BigString.utf16.index(offsetBy:)",
"BigString.utf8.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "distance",
"tasks": [
"BigString.distance(from:to:)",
"BigString.unicodeScalars.distance(from:to:)",
"BigString.utf16.distance(from:to:)",
"BigString.utf8.distance(from:to:)"
]
}
]
},
{
"kind": "group",
"title": "String operations",
"directory": "String",
"contents": [
{
"kind": "chart",
"title": "sequential iteration",
"tasks": [
"String character iteration",
"String scalar iteration",
"String UTF-16 iteration",
"String UTF-8 iteration"
]
},
{
"kind": "chart",
"title": "index-offset-by",
"tasks": [
"String.index(offsetBy:)",
"String.unicodeScalars.index(offsetBy:)",
"String.utf16.index(offsetBy:)",
"String.utf8.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "distance",
"tasks": [
"String.distance(from:to:)",
"String.unicodeScalars.distance(from:to:)",
"String.utf16.distance(from:to:)",
"String.utf8.distance(from:to:)"
]
}
]
},
{
"kind": "group",
"title": "BigString vs String",
"directory": "versus String",
"contents": [
{
"kind": "chart",
"title": "character iteration",
"tasks": [
"BigString character iteration",
"String character iteration"
]
},
{
"kind": "chart",
"title": "scalar iteration",
"tasks": [
"BigString scalar iteration",
"String scalar iteration"
]
},
{
"kind": "chart",
"title": "UTF-8 iteration",
"tasks": [
"BigString UTF-8 iteration",
"String UTF-8 iteration"
]
},
{
"kind": "chart",
"title": "UTF-16 iteration",
"tasks": [
"BigString UTF-16 iteration",
"String UTF-16 iteration"
]
},
{
"kind": "chart",
"title": "Character-based index(offsetBy:)",
"tasks": [
"BigString.index(offsetBy:)",
"String.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "UnicodeScalar-based index(offsetBy:)",
"tasks": [
"BigString.unicodeScalars.index(offsetBy:)",
"String.unicodeScalars.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "UTF-8 index(offsetBy:)",
"tasks": [
"BigString.utf8.index(offsetBy:)",
"String.utf8.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "UTF-16 index(offsetBy:)",
"tasks": [
"BigString.utf16.index(offsetBy:)",
"String.utf16.index(offsetBy:)"
]
},
{
"kind": "chart",
"title": "Character-based distance",
"tasks": [
"BigString.distance(from:to:)",
"String.distance(from:to:)"
]
},
{
"kind": "chart",
"title": "UnicodeScalar-based distance",
"tasks": [
"BigString.unicodeScalars.distance(from:to:)",
"String.unicodeScalars.distance(from:to:)"
]
},
{
"kind": "chart",
"title": "UTF-8 distance",
"tasks": [
"BigString.utf8.distance(from:to:)",
"String.utf8.distance(from:to:)"
]
},
{
"kind": "chart",
"title": "UTF-16 distance",
"tasks": [
"BigString.utf16.distance(from:to:)",
"String.utf16.distance(from:to:)"
]
}
]
}
]
}
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -74,6 +74,17 @@ struct BridgedStringInput {
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
struct BigStringInput {
let flat: String
let big: BigString
init(runeCount: Int, using rng: inout some RandomNumberGenerator) {
self.flat = String.randomLatin(runeCount: runeCount, using: &rng)
self.big = BigString(self.flat)
}
}
extension Benchmark {
public mutating func addBigStringBenchmarks() {
@@ -91,6 +102,13 @@ extension Benchmark {
return BridgedStringInput(runeCount: c, using: &rng)
}
self.registerInputGenerator(for: BigStringInput.self) { c in
var rng = SystemRandomNumberGenerator()
return BigStringInput(runeCount: c, using: &rng)
}
// MARK: BigString initialization
self.addSimple(
title: "BigString init from native string",
input: NativeStringInput.self
@@ -104,5 +122,337 @@ extension Benchmark {
) { input in
blackHole(BigString(input.value))
}
// MARK: BigString iteration
self.addSimple(
title: "BigString character iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for char in input.big {
blackHole(char)
}
}
self.addSimple(
title: "BigString scalar iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for scalar in input.big.unicodeScalars {
blackHole(scalar)
}
}
self.addSimple(
title: "BigString UTF-8 iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for u8 in input.big.utf8 {
blackHole(u8)
}
}
self.addSimple(
title: "BigString UTF-16 iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for u16 in input.big.utf16 {
blackHole(u16)
}
}
// MARK: String iteration
self.addSimple(
title: "String character iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for char in input.flat {
blackHole(char)
}
}
self.addSimple(
title: "String scalar iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for scalar in input.flat.unicodeScalars {
blackHole(scalar)
}
}
self.addSimple(
title: "String UTF-8 iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for u8 in input.flat.utf8 {
blackHole(u8)
}
}
self.addSimple(
title: "String UTF-16 iteration",
input: BigStringInput.self,
maxSize: nil
) { input in
for u16 in input.flat.utf16 {
blackHole(u16)
}
}
// MARK: BigString index(offsetBy:)
self.addSimple(
title: "BigString.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let start = str.startIndex
for i in 0 ... str.count {
let index = str.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "BigString.unicodeScalars.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let start = str.startIndex
for i in 0 ... str.unicodeScalars.count {
let index = str.unicodeScalars.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "BigString.utf8.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let start = str.startIndex
for i in 0 ... str.utf8.count {
let index = str.utf8.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "BigString.utf16.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let start = str.startIndex
for i in 0 ... str.utf16.count {
let index = str.utf16.index(start, offsetBy: i)
blackHole(index)
}
}
// MARK: String index(offsetBy:)
self.addSimple(
title: "String.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let start = str.startIndex
for i in 0 ... str.count {
let index = str.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "String.unicodeScalars.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let start = str.startIndex
for i in 0 ... str.unicodeScalars.count {
let index = str.unicodeScalars.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "String.utf8.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let start = str.startIndex
for i in 0 ... str.utf8.count {
let index = str.utf8.index(start, offsetBy: i)
blackHole(index)
}
}
self.addSimple(
title: "String.utf16.index(offsetBy:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let start = str.startIndex
for i in 0 ... str.utf16.count {
let index = str.utf16.index(start, offsetBy: i)
blackHole(index)
}
}
// MARK: BigString distance(from:to:)
self.add(
title: "BigString.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let indices = Array(str.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "BigString.unicodeScalars.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let indices = Array(str.unicodeScalars.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.unicodeScalars.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "BigString.utf8.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let indices = Array(str.utf8.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.utf8.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "BigString.utf16.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.big
let indices = Array(str.utf16.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.utf16.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
// MARK: String distance(from:to:)
self.add(
title: "String.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let indices = Array(str.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "String.unicodeScalars.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let indices = Array(str.unicodeScalars.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.unicodeScalars.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "String.utf8.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let indices = Array(str.utf8.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.utf8.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
self.add(
title: "String.utf16.distance(from:to:)",
input: BigStringInput.self,
maxSize: nil
) { input in
let str = input.flat
let indices = Array(str.utf16.indices) + [str.endIndex]
return { timer in
for i in 0 ..< indices.count {
let j = (i + indices.count) / 2
let expected = j - i
let actual = str.utf16.distance(from: indices[i], to: indices[j])
precondition(actual == expected)
}
}
}
}
}
@@ -110,11 +110,24 @@ extension BigString {
in chunk: BigString._Chunk
) -> Int {
assert(start <= end)
return chunk.string.unicodeScalars[start ..< end].count
// Make use of the chunk's known scalar count to reduce the amount of data we need to look at.
if end._utf8Offset - start._utf8Offset < chunk.utf8Count / 2 {
return chunk.string.unicodeScalars[start ..< end].count
}
var result: Int
result = chunk.unicodeScalarCount
result -= chunk.string.unicodeScalars[..<start].count
result -= chunk.string.unicodeScalars[end...].count
return result
}
func prefixSize(to end: String.Index, in chunk: BigString._Chunk) -> Int {
return chunk.string.unicodeScalars[..<end].count
// Make use of the chunk's known scalar count to reduce the amount of data we need to look at.
if end._utf8Offset < chunk.utf8Count / 2 {
return chunk.string.unicodeScalars[..<end].count
}
return chunk.unicodeScalarCount - chunk.string.unicodeScalars[end...].count
}
func formIndex(
@@ -122,6 +135,7 @@ extension BigString {
offsetBy distance: inout Int,
in chunk: BigString._Chunk
) -> (found: Bool, forward: Bool) {
// FIXME: Make use of the chunk's known scalar count to reduce work
guard distance != 0 else {
i = chunk.string.unicodeScalars._index(roundingDown: i)
return (true, false)
@@ -214,11 +228,23 @@ extension BigString {
in chunk: BigString._Chunk
) -> Int {
assert(start <= end)
return chunk.string.utf16[start ..< end].count
// Make use of the chunk's known UTF-16 count to reduce the amount of data we need to look at.
if end._utf8Offset - start._utf8Offset < chunk.utf8Count / 2 {
return chunk.string.utf16[start ..< end].count
}
var result = chunk.utf16Count
result -= chunk.string.utf16[..<start].count
result -= chunk.string.utf16[end...].count
return result
}
func prefixSize(to end: String.Index, in chunk: BigString._Chunk) -> Int {
return chunk.string.utf16[..<end].count
// Make use of the chunk's known UTF-16 count to reduce the amount of data we need to look at.
if end._utf8Offset < chunk.utf8Count / 2 {
return chunk.string.utf16[..<end].count
}
return chunk.utf16Count - chunk.string.utf16[end...].count
}
func formIndex(
@@ -226,6 +252,7 @@ extension BigString {
offsetBy distance: inout Int,
in chunk: BigString._Chunk
) -> (found: Bool, forward: Bool) {
// FIXME: Make use of the chunk's known UTF-16 count to reduce work
if distance >= 0 {
if
distance <= chunk.utf16Count,
@@ -41,7 +41,17 @@ extension BigString._Chunk {
let wholeCharacters = self.wholeCharacters
let s = Swift.max(start, firstBreak)
let e = Swift.min(end, lastBreak)
var result = wholeCharacters.distance(from: s, to: e)
var result: Int
// Run the actual grapheme breaking algorithm, making sure we measure
// as little data as possible, by making best use of the known character
// count for the whole chunk.
if e._utf8Offset - s._utf8Offset <= (lastBreak._utf8Offset - firstBreak._utf8Offset) / 2 {
result = wholeCharacters.distance(from: s, to: e)
} else {
result = characterCount - 1 // The last break is handled below
result -= wholeCharacters.distance(from: firstBreak, to: s)
result -= wholeCharacters.distance(from: e, to: lastBreak)
}
if end > lastBreak {
/// We know `end` is rounded, so it can only ever be after the last break
@@ -75,6 +85,7 @@ extension BigString._Chunk {
func formCharacterIndex(
_ i: inout String.Index, offsetBy distance: inout Int
) -> (found: Bool, forward: Bool) {
// FIXME: Make use of the chunk's known character count to reduce work
if distance == 0 {
if i < firstBreak {
i = string.startIndex