Skip to content

Commit 51c560f

Browse files
authored
Merge pull request #14052 from milseman/ascii_view
[string] Hoist UTF8View fast-path for known ASCII
2 parents 1901c30 + 744f5a6 commit 51c560f

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

stdlib/public/core/StringUTF8.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,32 +484,28 @@ extension String.UTF8View.Iterator : IteratorProtocol {
484484

485485
@_inlineable // FIXME(sil-serialize-all)
486486
public mutating func next() -> Unicode.UTF8.CodeUnit? {
487+
if _slowPath(_nextOffset == _endOffset) {
488+
if _slowPath(_buffer.isEmpty) {
489+
return nil
490+
}
491+
}
492+
if _guts.isASCII {
493+
defer { _nextOffset += 1 }
494+
return _guts._unmanagedASCIIView.buffer[_nextOffset]
495+
}
496+
487497
if _fastPath(!_buffer.isEmpty) {
488498
return _buffer.removeFirst()
489499
}
490-
if _nextOffset == _endOffset { return nil }
491500
return _fillBuffer()
492501
}
493502

494503
@_versioned
495504
@inline(never)
496505
internal mutating func _fillBuffer() -> Unicode.UTF8.CodeUnit {
497-
_sanityCheck(_buffer.isEmpty)
498-
_sanityCheck(_nextOffset < _endOffset)
506+
_sanityCheck(!_guts.isASCII, "next() already checks for known ASCII")
499507
defer { _fixLifetime(_guts) }
500-
if _guts.isASCII {
501-
// FIXME: Measure if it's worth inlining this path
502-
let ascii = _guts._unmanagedASCIIView.buffer
503-
let result = ascii[_nextOffset]
504-
_nextOffset += 1
505-
let fillCount = min(_buffer.capacity, _endOffset - _nextOffset)
506-
for _ in 0 ..< fillCount {
507-
_buffer.append(ascii[_nextOffset])
508-
_nextOffset += 1
509-
}
510-
return result
511-
}
512-
if _guts._isContiguous {
508+
if _fastPath(_guts._isContiguous) {
513509
return _fillBuffer(from: _guts._unmanagedUTF16View)
514510
}
515511
return _fillBuffer(from: _guts._asOpaque())

0 commit comments

Comments
 (0)