mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
_BString.Index: Add alignment bits
Use them to speed up index rounding. (And, somewhat questionably, subscripting.)
This commit is contained in:
4 files changed
+145
-58
No files matched your search
@@ -194,11 +194,11 @@ extension _BString {
|
||||
}
|
||||
|
||||
func characterIndex(_ i: Index, offsetBy distance: Int) -> Index {
|
||||
index(i, offsetBy: distance, in: CharacterMetric())
|
||||
index(i, offsetBy: distance, in: CharacterMetric())._knownCharacterAligned()
|
||||
}
|
||||
|
||||
func unicodeScalarIndex(_ i: Index, offsetBy distance: Int) -> Index {
|
||||
index(i, offsetBy: distance, in: UnicodeScalarMetric())
|
||||
index(i, offsetBy: distance, in: UnicodeScalarMetric())._knownScalarAligned()
|
||||
}
|
||||
|
||||
func utf16Index(_ i: Index, offsetBy distance: Int) -> Index {
|
||||
@@ -234,11 +234,17 @@ extension _BString {
|
||||
}
|
||||
|
||||
func characterIndex(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
|
||||
index(i, offsetBy: distance, limitedBy: limit, in: CharacterMetric())
|
||||
guard let j = index(i, offsetBy: distance, limitedBy: limit, in: CharacterMetric()) else {
|
||||
return nil
|
||||
}
|
||||
return j._knownCharacterAligned()
|
||||
}
|
||||
|
||||
func unicodeScalarIndex(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
|
||||
index(i, offsetBy: distance, limitedBy: limit, in: UnicodeScalarMetric())
|
||||
guard let j = index(i, offsetBy: distance, limitedBy: limit, in: UnicodeScalarMetric()) else {
|
||||
return nil
|
||||
}
|
||||
return j._knownScalarAligned()
|
||||
}
|
||||
|
||||
func utf16Index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
|
||||
@@ -253,11 +259,11 @@ extension _BString {
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
extension _BString {
|
||||
func characterIndex(after i: Index) -> Index {
|
||||
index(i, offsetBy: 1, in: CharacterMetric())
|
||||
index(i, offsetBy: 1, in: CharacterMetric())._knownCharacterAligned()
|
||||
}
|
||||
|
||||
func unicodeScalarIndex(after i: Index) -> Index {
|
||||
index(i, offsetBy: 1, in: UnicodeScalarMetric())
|
||||
index(i, offsetBy: 1, in: UnicodeScalarMetric())._knownScalarAligned()
|
||||
}
|
||||
|
||||
func utf16Index(after i: Index) -> Index {
|
||||
@@ -284,11 +290,11 @@ extension _BString {
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
extension _BString {
|
||||
func characterIndex(before i: Index) -> Index {
|
||||
index(i, offsetBy: -1, in: CharacterMetric())
|
||||
index(i, offsetBy: -1, in: CharacterMetric())._knownCharacterAligned()
|
||||
}
|
||||
|
||||
func unicodeScalarIndex(before i: Index) -> Index {
|
||||
index(i, offsetBy: -1, in: UnicodeScalarMetric())
|
||||
index(i, offsetBy: -1, in: UnicodeScalarMetric())._knownScalarAligned()
|
||||
}
|
||||
|
||||
func utf16Index(before i: Index) -> Index {
|
||||
@@ -320,10 +326,12 @@ extension _BString {
|
||||
func characterIndex(roundingDown i: Index) -> Index {
|
||||
let offset = i._utf8Offset
|
||||
precondition(offset >= 0 && offset <= utf8Count, "Index out of bounds")
|
||||
guard offset > 0 else { return resolve(i, preferEnd: false) }
|
||||
guard offset < utf8Count else { return resolve(i, preferEnd: true) }
|
||||
guard offset > 0 else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
|
||||
guard offset < utf8Count else { return resolve(i, preferEnd: true)._knownCharacterAligned() }
|
||||
|
||||
let i = resolve(i, preferEnd: true)
|
||||
guard !i._isKnownCharacterAligned else { return i }
|
||||
|
||||
var ri = i._rope!
|
||||
let ci = i._chunkIndex
|
||||
var chunk = rope[ri]
|
||||
@@ -332,11 +340,13 @@ extension _BString {
|
||||
let last = chunk.lastBreak
|
||||
if ci == first || ci == last { return i }
|
||||
if ci > last {
|
||||
return Index(baseUTF8Offset: i._utf8BaseOffset, rope: ri, chunk: last)
|
||||
return Index(
|
||||
baseUTF8Offset: i._utf8BaseOffset, rope: ri, chunk: last
|
||||
)._knownCharacterAligned()
|
||||
}
|
||||
if ci > first {
|
||||
let j = chunk.wholeCharacters._index(roundingDown: ci)
|
||||
return Index(baseUTF8Offset: i._utf8BaseOffset, rope: ri, chunk: j)
|
||||
return Index(baseUTF8Offset: i._utf8BaseOffset, rope: ri, chunk: j)._knownCharacterAligned()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,26 +357,31 @@ extension _BString {
|
||||
baseOffset -= chunk.utf8Count
|
||||
if chunk.hasBreaks { break }
|
||||
}
|
||||
return Index(baseUTF8Offset: baseOffset, rope: ri, chunk: chunk.lastBreak)
|
||||
return Index(
|
||||
baseUTF8Offset: baseOffset, rope: ri, chunk: chunk.lastBreak
|
||||
)._knownCharacterAligned()
|
||||
}
|
||||
|
||||
func unicodeScalarIndex(roundingDown i: Index) -> Index {
|
||||
precondition(i <= endIndex, "Index out of bounds")
|
||||
guard i > startIndex else { return resolve(i, preferEnd: false) }
|
||||
guard i < endIndex else { return endIndex }
|
||||
guard i > startIndex else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
|
||||
guard i < endIndex else { return resolve(i, preferEnd: true)._knownCharacterAligned() }
|
||||
|
||||
let start = self.resolve(i, preferEnd: false)
|
||||
guard !i._isKnownScalarAligned else { return i }
|
||||
let ri = start._rope!
|
||||
let chunk = self.rope[ri]
|
||||
let ci = chunk.string.unicodeScalars._index(roundingDown: start._chunkIndex)
|
||||
return Index(baseUTF8Offset: start._utf8BaseOffset, rope: ri, chunk: ci)
|
||||
return Index(baseUTF8Offset: start._utf8BaseOffset, rope: ri, chunk: ci)._knownScalarAligned()
|
||||
}
|
||||
|
||||
func utf8Index(roundingDown i: Index) -> Index {
|
||||
precondition(i <= endIndex, "Index out of bounds")
|
||||
guard i < endIndex else { return endIndex }
|
||||
var r = i
|
||||
r._clearUTF16TrailingSurrogate()
|
||||
if i._isUTF16TrailingSurrogate {
|
||||
r._clearUTF16TrailingSurrogate()
|
||||
}
|
||||
return resolve(r, preferEnd: false)
|
||||
}
|
||||
|
||||
@@ -421,8 +436,11 @@ extension _BString {
|
||||
let char = chunk.string[ci]
|
||||
let endOffset = start._utf8ChunkOffset + char.utf8.count
|
||||
if endOffset < chunk.utf8Count {
|
||||
let endIndex = chunk.string._utf8Index(at: endOffset)
|
||||
return (char, Index(baseUTF8Offset: start._utf8BaseOffset, rope: ri, chunk: endIndex))
|
||||
let endStringIndex = chunk.string._utf8Index(at: endOffset)
|
||||
let endIndex = Index(
|
||||
baseUTF8Offset: start._utf8BaseOffset, rope: ri, chunk: endStringIndex
|
||||
)._knownCharacterAligned()
|
||||
return (char, endIndex)
|
||||
}
|
||||
var s = String(char)
|
||||
var base = start._utf8BaseOffset + chunk.utf8Count
|
||||
@@ -440,7 +458,7 @@ extension _BString {
|
||||
}
|
||||
base += chunk.utf8Count
|
||||
}
|
||||
return (Character(s), Index(baseUTF8Offset: base, rope: ri, chunk: ci))
|
||||
return (Character(s), Index(baseUTF8Offset: base, rope: ri, chunk: ci)._knownCharacterAligned())
|
||||
}
|
||||
|
||||
subscript(utf8 index: Index) -> UInt8 {
|
||||
|
||||
@@ -16,11 +16,18 @@ extension _BString {
|
||||
internal struct Index: Sendable {
|
||||
typealias Rope = _BString.Rope
|
||||
|
||||
// ┌─────────────────────┬──────────────┬────────────────────┐
|
||||
// │ b63:b9 │ b8 │ b7:b0 │
|
||||
// ├─────────────────────┼──────────────┼────────────────────┤
|
||||
// │ UTF-8 global offset │ UTF-16 delta │ UTF-8 chunk offset │
|
||||
// └─────────────────────┴──────────────┴────────────────────┘
|
||||
// ┌───────────────────────────────┬───┬───────────┬────────────────────┐
|
||||
// │ b63:b11 │b10│ b9:b8 │ b7:b0 │
|
||||
// ├───────────────────────────────┼───┼───────────┼────────────────────┤
|
||||
// │ UTF-8 global offset │ T │ alignment │ UTF-8 chunk offset │
|
||||
// └───────────────────────────────┴───┴───────────┴────────────────────┘
|
||||
// b10 (T): UTF-16 trailing surrogate indicator
|
||||
// b9: isCharacterAligned
|
||||
// b8: isScalarAligned
|
||||
//
|
||||
// 100: UTF-16 trailing surrogate
|
||||
// 001: Index known to be scalar aligned
|
||||
// 011: Index known to be Character aligned
|
||||
var _rawBits: UInt64
|
||||
|
||||
/// A (possibly invalid) rope index.
|
||||
@@ -35,17 +42,32 @@ extension _BString {
|
||||
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
extension _BString.Index {
|
||||
@inline(__always)
|
||||
internal static func _bitsForUTF8Offset(_ utf8Offset: Int) -> UInt64 {
|
||||
let v = UInt64(truncatingIfNeeded: UInt(bitPattern: utf8Offset))
|
||||
assert(v &>> 53 == 0)
|
||||
return v &<< 11
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal static var _flagsMask: UInt64 { 0x700 }
|
||||
|
||||
@inline(__always)
|
||||
internal static var _utf16TrailingSurrogateBits: UInt64 { 0x400 }
|
||||
|
||||
@inline(__always)
|
||||
internal static var _characterAlignmentBits: UInt64 { 0x300 }
|
||||
|
||||
@inline(__always)
|
||||
internal static var _scalarAlignmentBits: UInt64 { 0x100 }
|
||||
|
||||
internal var _utf8Offset: Int {
|
||||
Int(truncatingIfNeeded: _rawBits &>> 9)
|
||||
Int(truncatingIfNeeded: _rawBits &>> 11)
|
||||
}
|
||||
|
||||
internal var _isUTF16TrailingSurrogate: Bool {
|
||||
_orderingValue & 1 != 0
|
||||
}
|
||||
|
||||
/// The base offset of the addressed chunk. Only valid if `_rope` is not nil.
|
||||
internal var _utf8BaseOffset: Int {
|
||||
_utf8Offset - _utf8ChunkOffset
|
||||
@inline(__always)
|
||||
internal var _orderingValue: UInt64 {
|
||||
_rawBits &>> 10
|
||||
}
|
||||
|
||||
/// The offset within the addressed chunk. Only valid if `_rope` is not nil.
|
||||
@@ -54,32 +76,71 @@ extension _BString.Index {
|
||||
return Int(truncatingIfNeeded: _rawBits & 0xFF)
|
||||
}
|
||||
|
||||
internal var _chunkIndex: String.Index {
|
||||
assert(_rope != nil)
|
||||
return String.Index(
|
||||
_utf8Offset: _utf8ChunkOffset, utf16TrailingSurrogate: _isUTF16TrailingSurrogate)
|
||||
/// The base offset of the addressed chunk. Only valid if `_rope` is not nil.
|
||||
internal var _utf8BaseOffset: Int {
|
||||
_utf8Offset - _utf8ChunkOffset
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal var _orderingValue: UInt64 {
|
||||
_rawBits &>> 8
|
||||
internal var _flags: UInt64 {
|
||||
get {
|
||||
_rawBits & Self._flagsMask
|
||||
}
|
||||
set {
|
||||
assert(newValue & ~Self._flagsMask == 0)
|
||||
_rawBits &= ~Self._flagsMask
|
||||
_rawBits |= newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String.Index {
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
func _copyingAlignmentBits(from i: _BString.Index) -> String.Index {
|
||||
var bits = _abi_rawBits & ~3
|
||||
bits |= (i._flags &>> 8) & 3
|
||||
return String.Index(_rawBits: bits)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
extension _BString.Index {
|
||||
@inline(__always)
|
||||
internal static func _bitsForUTF8Offset(_ utf8Offset: Int) -> UInt64 {
|
||||
let v = UInt64(truncatingIfNeeded: UInt(bitPattern: utf8Offset))
|
||||
assert(v &>> 55 == 0)
|
||||
return v &<< 9
|
||||
internal var _chunkIndex: String.Index {
|
||||
assert(_rope != nil)
|
||||
return String.Index(
|
||||
_utf8Offset: _utf8ChunkOffset, utf16TrailingSurrogate: _isUTF16TrailingSurrogate
|
||||
)._copyingAlignmentBits(from: self)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
|
||||
extension _BString.Index {
|
||||
internal mutating func _clearUTF16TrailingSurrogate() {
|
||||
_flags = 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal static var _utf16TrailingSurrogateBits: UInt64 { 0x100 }
|
||||
internal var _isUTF16TrailingSurrogate: Bool {
|
||||
_orderingValue & 1 != 0
|
||||
}
|
||||
|
||||
internal mutating func _clearUTF16TrailingSurrogate() {
|
||||
_rawBits &= ~Self._utf16TrailingSurrogateBits
|
||||
internal func _knownScalarAligned() -> Self {
|
||||
var copy = self
|
||||
copy._flags = Self._scalarAlignmentBits
|
||||
return copy
|
||||
}
|
||||
|
||||
internal func _knownCharacterAligned() -> Self {
|
||||
var copy = self
|
||||
copy._flags = Self._characterAlignmentBits
|
||||
return copy
|
||||
}
|
||||
|
||||
internal var _isKnownScalarAligned: Bool {
|
||||
_rawBits & Self._scalarAlignmentBits != 0
|
||||
}
|
||||
|
||||
internal var _isKnownCharacterAligned: Bool {
|
||||
_rawBits & Self._characterAlignmentBits != 0
|
||||
}
|
||||
|
||||
internal init(_utf8Offset: Int) {
|
||||
@@ -111,7 +172,7 @@ extension _BString.Index {
|
||||
let chunkUTF8Offset = chunk._utf8Offset
|
||||
self.init(
|
||||
_utf8Offset: baseUTF8Offset + chunkUTF8Offset,
|
||||
utf16TrailingSurrogate: chunk._utf16Delta > 0,
|
||||
utf16TrailingSurrogate: chunk._isUTF16TrailingSurrogate,
|
||||
rope: rope,
|
||||
chunkOffset: chunkUTF8Offset)
|
||||
}
|
||||
@@ -155,12 +216,16 @@ extension _BString {
|
||||
rope.formIndex(before: &ri)
|
||||
let length = rope[ri].utf8Count
|
||||
let ci = String.Index(_utf8Offset: length)
|
||||
return Index(baseUTF8Offset: i._utf8Offset - length, rope: ri, chunk: ci)
|
||||
var j = Index(baseUTF8Offset: i._utf8Offset - length, rope: ri, chunk: ci)
|
||||
j._flags = i._flags
|
||||
return j
|
||||
}
|
||||
guard i._utf8ChunkOffset == rope[ri].utf8Count else { return i }
|
||||
rope.formIndex(after: &ri)
|
||||
let ci = String.Index(_utf8Offset: 0)
|
||||
return Index(baseUTF8Offset: i._utf8Offset, rope: ri, chunk: ci)
|
||||
var j = Index(baseUTF8Offset: i._utf8Offset, rope: ri, chunk: ci)
|
||||
j._flags = i._flags
|
||||
return j
|
||||
}
|
||||
|
||||
let (ri, chunkOffset) = rope.find(
|
||||
@@ -170,5 +235,4 @@ extension _BString {
|
||||
return Index(baseUTF8Offset: i._utf8Offset - ci._utf8Offset, rope: ri, chunk: ci)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -145,7 +145,7 @@ extension String {
|
||||
maxUTF8Count: Int
|
||||
) -> Range<Index>? {
|
||||
assert(maxUTF8Count >= 0)
|
||||
assert(i._isScalarAligned)
|
||||
assert(i._isKnownScalarAligned)
|
||||
guard i < limit else { return nil }
|
||||
let end = self.utf8.index(i, offsetBy: maxUTF8Count, limitedBy: limit) ?? limit
|
||||
let j = self.unicodeScalars._index(roundingDown: end)
|
||||
|
||||
Reference in new issue
Block a user