Skip to content

[string] Hoist UTF8View fast-path for known ASCII #14052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions stdlib/public/core/StringUTF8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,32 +484,28 @@ extension String.UTF8View.Iterator : IteratorProtocol {

@_inlineable // FIXME(sil-serialize-all)
public mutating func next() -> Unicode.UTF8.CodeUnit? {
if _slowPath(_nextOffset == _endOffset) {
if _slowPath(_buffer.isEmpty) {
return nil
}
}
if _guts.isASCII {
defer { _nextOffset += 1 }
return _guts._unmanagedASCIIView.buffer[_nextOffset]
}

if _fastPath(!_buffer.isEmpty) {
return _buffer.removeFirst()
}
if _nextOffset == _endOffset { return nil }
return _fillBuffer()
}

@_versioned
@inline(never)
internal mutating func _fillBuffer() -> Unicode.UTF8.CodeUnit {
_sanityCheck(_buffer.isEmpty)
_sanityCheck(_nextOffset < _endOffset)
_sanityCheck(!_guts.isASCII, "next() already checks for known ASCII")
defer { _fixLifetime(_guts) }
if _guts.isASCII {
// FIXME: Measure if it's worth inlining this path
let ascii = _guts._unmanagedASCIIView.buffer
let result = ascii[_nextOffset]
_nextOffset += 1
let fillCount = min(_buffer.capacity, _endOffset - _nextOffset)
for _ in 0 ..< fillCount {
_buffer.append(ascii[_nextOffset])
_nextOffset += 1
}
return result
}
if _guts._isContiguous {
if _fastPath(_guts._isContiguous) {
return _fillBuffer(from: _guts._unmanagedUTF16View)
}
return _fillBuffer(from: _guts._asOpaque())
Expand Down