Skip to content

Introduce ByteSymbol #141875

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

nnethercote
Copy link
Contributor

r? @ghost

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 2, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 2, 2025
bors added a commit that referenced this pull request Jun 2, 2025
Introduce `ByteSymbol`

r? `@ghost`
@bors
Copy link
Collaborator

bors commented Jun 2, 2025

⌛ Trying commit e83f532 with merge f661a16...

@bors
Copy link
Collaborator

bors commented Jun 2, 2025

☀️ Try build successful - checks-actions
Build commit: f661a16 (f661a16cf599bf46f7b251ef3f5caba62f9ce4ac)

@rust-timer

This comment has been minimized.

@petrochenkov petrochenkov self-assigned this Jun 2, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f661a16): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.5% [0.5%, 0.5%] 2
Improvements ✅
(primary)
-3.0% [-3.0%, -3.0%] 1
Improvements ✅
(secondary)
-0.4% [-0.5%, -0.3%] 2
All ❌✅ (primary) -1.4% [-3.0%, 0.2%] 2

Max RSS (memory usage)

Results (primary -1.9%, secondary -1.7%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.2% [0.4%, 6.7%] 8
Improvements ✅
(primary)
-1.9% [-2.2%, -1.6%] 2
Improvements ✅
(secondary)
-2.5% [-7.9%, -0.4%] 35
All ❌✅ (primary) -1.9% [-2.2%, -1.6%] 2

Cycles

Results (primary -2.4%, secondary 0.3%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.0% [0.5%, 2.9%] 11
Improvements ✅
(primary)
-2.4% [-3.1%, -1.7%] 2
Improvements ✅
(secondary)
-0.8% [-1.3%, -0.4%] 7
All ❌✅ (primary) -2.4% [-3.1%, -1.7%] 2

Binary size

Results (primary -1.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.1% [-1.1%, -1.1%] 1

Bootstrap: 775.84s -> 774.376s (-0.19%)
Artifact size: 372.26 MiB -> 372.24 MiB (-0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 3, 2025
@nnethercote
Copy link
Contributor Author

The motivation here: introduce ByteSymbol, to eliminate the Arcs in ast::LitKind, to make ast::LitKind impl Copy, to avoid the need to arena-allocate ast::LitKind in HIR.

It works, and the max-rss results (and page faults) are down for the literal-heavy benchmarks: deep-vector, tuple-stress, coercions, ucd. (And it's difficult to get max-rss down in general.) But it has no real effect on speed. So I'm not sure if it's worth the extra code.

@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 3, 2025
@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 3, 2025

cc #65818 (comment)

Upd: but maybe most of the (non-backend) improvements we already done in #103812.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 3, 2025

This is a real issue because people want to include relatively large binary blobs into their programs, but I'm not sure we have a relevant benchmark in the perf suite.

It makes sense to benchmark this change on something specifically binary-blob-oriented.

@petrochenkov
Copy link
Contributor

I actually wanted to share the interner between string symbols and byte symbols.
The interner shouldn't care whether the bytes it stores or looks up are UTF-8 or not.
Symbol and ByteSymbol would then both be wrappers around an InternerIndex, but Symbol would give a guarantee that the bytes it points to are UTF-8, and ByteSymbol would give no such guarantee, and Symbol(index = 123) and ByteSymbol(index = 123) would actually share the storage if the bytes are UTF-8.

#65818 (comment) suggested making Symbol and ByteSymbol symbols dynamically discernible from each other (e.g. the former counts from 0, and the latter from u32::MAX), but maybe just having two static types would be enough.

Not sure if the same sharing can be done for metadata encoding/decoding tables.

@petrochenkov
Copy link
Contributor

I think this generally makes sense, the byte array interner is not large and is mostly a copypaste of the string interner, so not much new complexity.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@nnethercote
Copy link
Contributor Author

nnethercote commented Jun 4, 2025

I benchmarked this program:

pub const BYTES: &[u8] = include_bytes!("/bin/containerd");

fn main() {
    println!("len = {}", BYTES.len());
}

On my Linux box /bin/containerd is a 48MB file.

hyperfine results, before and after:

Benchmark 1: $RUSTC1 b2.rs
  Time (mean ± σ):     414.3 ms ±  27.7 ms    [User: 183.2 ms, System: 294.1 ms]
  Range (min … max):   366.9 ms … 461.0 ms    10 runs
 
Benchmark 1: $RUSTC3 b2.rs
  Time (mean ± σ):     443.1 ms ±  26.2 ms    [User: 191.3 ms, System: 315.1 ms]
  Range (min … max):   413.7 ms … 500.2 ms    10 runs

I.e. this PR makes things a little slower. Probably because of the cost of hashing the binary blob to convert it into a ByteSymbol.

@petrochenkov
Copy link
Contributor

Seems reasonable, big binary blobs are unlikely to be identical, so not much sense to try deduplicating them.

@nnethercote
Copy link
Contributor Author

nnethercote commented Jun 4, 2025

Surprising it's only a little slower: Cachegrind says my example program takes 39 million instrs to compile normally and 67 million instructions with this PR's changes. The differences being all in hashing and some memcpy.

Edit: if I put the benchmark into rustc-perf and measure it, for a check full build I get a 58% wall-time increase from 0.15s to 0.23s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants