Skip to content

perf(codegen): reduce checks printing strings#10341

Merged
graphite-app[bot] merged 1 commit intomainfrom
04-09-perf_codegen_reduce_checks_printing_strings
Apr 9, 2025
Merged

perf(codegen): reduce checks printing strings#10341
graphite-app[bot] merged 1 commit intomainfrom
04-09-perf_codegen_reduce_checks_printing_strings

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Apr 9, 2025

Speed up printing strings.

I had wrongly assumed that bytes.next().unwrap_unchecked() would skip checks, but it seems it behaves the same as bytes.next() - i.e. check iterator is not empty, and only advance if it's not.

Use assert_unchecked! instead to inform compiler that there are definitely the required number of bytes remaining in bytes iterator before consuming them.

This reduces instruction count considerably, and removes a branch on every byte consumed.

https://godbolt.org/z/TWzfK1eKj

The mysteries of the compiler!

@github-actions github-actions bot added A-codegen Area - Code Generation C-performance Category - Solution not expected to change functional behavior, only performance labels Apr 9, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel marked this pull request as ready for review April 9, 2025 18:40
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 9, 2025

CodSpeed Instrumentation Performance Report

Merging #10341 will not alter performance

Comparing 04-09-perf_codegen_reduce_checks_printing_strings (426f8cb) with main (0370363)

Summary

✅ 36 untouched benchmarks

@overlookmotel
Copy link
Member Author

overlookmotel commented Apr 9, 2025

Tiny effect on benchmark, because strings are a small part of AST, and this change probably only affects printing escapes. In the hot loop where no escape required, compiler probably already deduced there was definitely a byte to consume, because we just peeked it a few instructions earlier.

@overlookmotel
Copy link
Member Author

Tiny optimization, so merging without review.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Apr 9, 2025
Copy link
Member Author

overlookmotel commented Apr 9, 2025

Merge activity

Speed up printing strings.

I had wrongly assumed that `bytes.next().unwrap_unchecked()` would skip checks, but it seems it behaves the same as `bytes.next()` - i.e. check iterator is not empty, and only advance if it's not.

Use `assert_unchecked!` instead to inform compiler that there are definitely the required number of bytes remaining in `bytes` iterator before consuming them.

This reduces instruction count considerably, and removes a branch on every byte consumed.

https://godbolt.org/z/TWzfK1eKj

The mysteries of the compiler!
@graphite-app graphite-app bot force-pushed the 04-09-perf_codegen_reduce_checks_printing_strings branch from 3d0cf25 to 426f8cb Compare April 9, 2025 18:54
@graphite-app graphite-app bot merged commit 426f8cb into main Apr 9, 2025
28 checks passed
@graphite-app graphite-app bot deleted the 04-09-perf_codegen_reduce_checks_printing_strings branch April 9, 2025 19:00
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant