Skip to content

[FormattedStream] Add ASCII fast path #117892

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
Dec 2, 2024
Merged

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Nov 27, 2024

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.

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.
@nikic nikic requested a review from fhahn November 27, 2024 14:52
@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2024

@llvm/pr-subscribers-llvm-support

Author: Nikita Popov (nikic)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/117892.diff

1 Files Affected:

  • (modified) llvm/lib/Support/FormattedStream.cpp (+7)
diff --git a/llvm/lib/Support/FormattedStream.cpp b/llvm/lib/Support/FormattedStream.cpp
index c50530e76efc0a..4192893b38a763 100644
--- a/llvm/lib/Support/FormattedStream.cpp
+++ b/llvm/lib/Support/FormattedStream.cpp
@@ -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

@nikic nikic requested a review from ostannard December 2, 2024 08:56
Copy link
Collaborator

@ostannard ostannard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic merged commit da23a83 into llvm:main Dec 2, 2024
10 checks passed
@nikic nikic deleted the formatted-stream-fast branch December 2, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants