Tweak enum serialization to generate better LLVM IR and more compact code - #77
Merged
Conversation
…code
Change enum serialization form
match self {
Enum::Variant1(field1, field2, ...) => {
// serialize variant idx
let variant_idx: u8 = 0;
writer.write_all(&variant_idx.to_le_bytes())?;
// serialize variant fields
BorshSerialize::serialize(field1, writer)?;
BorshSerialize::serialize(field2, writer)?;
...
}
Enum::Variant2(field1, field2, ...) => {
let variant_idx: u8 = 1;
writer.write_all(&variant_idx.to_le_bytes())?;
// serialize variant fields
BorshSerialize::serialize(field1, writer)?;
BorshSerialize::serialize(field2, writer)?;
...
}
...
}
To:
let variant_idx: u8 = match self {
Enum::Variant1(..) => 0,
Enum::Variant2(..) => 1,
...
};
// serialize variant_idx
writer.write_all(&variant_idx.to_le_bytes())?;
match self {
Enum::Variant1(field1, field2, ...) => {
// serialize variant fields
BorshSerialize::serialize(field1, writer)?;
BorshSerialize::serialize(field2, writer)?;
...
}
Enum::Variant2(field1, field2, ...) => {
// serialize variant fields
BorshSerialize::serialize(field1, writer)?;
BorshSerialize::serialize(field2, writer)?;
...
}
...
}
The latter generates better LLVM IR, and avoids
writer.write_all(&variant_idx.to_le_bytes())?; from being inlined into each
match branch.
alessandrod
requested review from
austinabell,
frol and
matklad
as code owners
February 3, 2022 00:59
austinabell
approved these changes
Feb 3, 2022
Contributor
|
excellent find, thanks! |
matklad
added a commit
to matklad/borsh-rs
that referenced
this pull request
Feb 3, 2022
matklad
added a commit
that referenced
this pull request
Feb 4, 2022
* CI: drop coverage reports from CI Coverage doesn't work today: https://github.com/near/borsh-rs/runs/4955323970?check_suite_focus=true#step:3:1141 We might think of reviving it in the future, but first, let's makes sure that our CI actually reflects de-facto practice * CI: test all feature combinations We didn't check that `--no-default-features` works, and it did broke. Note that we `pushd borsh` before testing -- this is to make sure that other ws crates (like fuzzing) don't enable extra features. * ci: drop needless rustfmt args * fix: fix no_std build * chore: changelog for #77 * publish 0.9.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello!
I'm looking at reducing the size of some programs that use borsh, and I've noticed that deriving
BorshSerializeon enums can sometimes trigger some pretty bad inlining from rustc/LLVM.This PR changes enum serialization from:
To:
The latter generates better LLVM IR, and avoids
writer.write_all(&variant_idx.to_le_bytes())?from being inlined into each match branch.Here's a test case that shows the issue: https://gist.github.com/alessandrod/f192c59f6b75159733d2cb6c60a78f1b
Cargo bloat before:
Cargo bloat after: