From f1b0eba9482d82251f383ea4964546d87db78903 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 17 Jun 2025 19:51:37 -0700 Subject: [PATCH 1/2] [BigString] Add some basic benchmarks --- Benchmarks/Libraries/BigString.json | 192 ++++++++++ .../Benchmarks/BigStringBenchmarks.swift | 352 +++++++++++++++++- 2 files changed, 543 insertions(+), 1 deletion(-) create mode 100644 Benchmarks/Libraries/BigString.json diff --git a/Benchmarks/Libraries/BigString.json b/Benchmarks/Libraries/BigString.json new file mode 100644 index 00000000..afc373d3 --- /dev/null +++ b/Benchmarks/Libraries/BigString.json @@ -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:)" + ] + } + ] + } + ] +} diff --git a/Benchmarks/Sources/Benchmarks/BigStringBenchmarks.swift b/Benchmarks/Sources/Benchmarks/BigStringBenchmarks.swift index e1c96ae6..657d945e 100644 --- a/Benchmarks/Sources/Benchmarks/BigStringBenchmarks.swift +++ b/Benchmarks/Sources/Benchmarks/BigStringBenchmarks.swift @@ -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) + } + } + } } } From fc411858ee8c2d1ca4113555c185752f5d8212f5 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 17 Jun 2025 20:09:58 -0700 Subject: [PATCH 2/2] [BigString] Speed up index distance calculations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This implements a roughly 40-45% improvement for distance measurements over `BigString`’s character and Unicode Scalar views. Interestingly, we don’t see an improvement for UTF-16 views — I’m guessing this is due to String’s breadcrumbing. (And it will go away with the upcoming UTF8Span-ification of this type.) The benchmarks I added only show an improvement for multi-chunk big string instances, but this is merely an artifact of the specific algorithm that is being measured — for single-chunk strings, the existing measurement method is already optimal, so the new heuristic never triggers for this particular payload. --- .../BigString/Basics/BigString+Metrics.swift | 35 ++++++++++++++++--- ...gString+Chunk+Indexing by Characters.swift | 13 ++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Sources/RopeModule/BigString/Basics/BigString+Metrics.swift b/Sources/RopeModule/BigString/Basics/BigString+Metrics.swift index 4aeb696d..42775e9c 100644 --- a/Sources/RopeModule/BigString/Basics/BigString+Metrics.swift +++ b/Sources/RopeModule/BigString/Basics/BigString+Metrics.swift @@ -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[.. Int { - return chunk.string.unicodeScalars[.. (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[.. Int { - return chunk.string.utf16[.. (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, diff --git a/Sources/RopeModule/BigString/Chunk/BigString+Chunk+Indexing by Characters.swift b/Sources/RopeModule/BigString/Chunk/BigString+Chunk+Indexing by Characters.swift index 4eae68fc..4371a259 100644 --- a/Sources/RopeModule/BigString/Chunk/BigString+Chunk+Indexing by Characters.swift +++ b/Sources/RopeModule/BigString/Chunk/BigString+Chunk+Indexing by Characters.swift @@ -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