[FormattedStream] Add ASCII fast path (#117892)

Printing IR spends a large amount of time updating the column count via
generic UTF-8 APIs. Speed it up by adding a fast path for the common
printable ASCII case.

This makes `-print-on-crash -print-module-scope` about two times faster.
This commit is contained in:
Nikita Popov 2024-12-02 14:31:24 +01:00 committed by GitHub
parent ccc471fe3e
commit da23a8372c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -75,6 +75,13 @@ void formatted_raw_ostream::UpdatePosition(const char *Ptr, size_t Size) {
// Now scan the rest of the buffer.
unsigned NumBytes;
for (const char *End = Ptr + Size; Ptr < End; Ptr += NumBytes) {
// Fast path for printable ASCII characters without special handling.
if (*Ptr >= 0x20 && *Ptr <= 0x7e) {
NumBytes = 1;
++Column;
continue;
}
NumBytes = getNumBytesForUTF8(*Ptr);
// The buffer might end part way through a UTF-8 code unit sequence for a