Skip to content

Conversation

emilk
Copy link
Member

@emilk emilk commented Aug 8, 2025

Large errors leads to large Results, which may lead to a lot of memcpy:s when returning a Result (even an Ok one), which can hurt performance.

Some of our error types were over 100 bytes, even 200 bytes.

Many of our errors are still pretty huge (64+ bytes), but this is a start.

Large errors leads to large `Result`s, which leads to a lot of memcpy:s
and slow performance.
@emilk emilk added 📉 performance Optimization, memory use, etc exclude from changelog PRs with this won't show up in CHANGELOG.md labels Aug 8, 2025
Copy link

github-actions bot commented Aug 8, 2025

Web viewer built successfully. If applicable, you should also test it:

  • I have tested the web viewer
Result Commit Link Manifest
a11cb2f https://rerun.io/viewer/pr/10845 +nightly +main

Note: This comment is updated whenever you push a commit.

@Wumpf Wumpf self-requested a review August 13, 2025 11:56
Comment on lines +65 to +72
#[test]
fn test_error_size() {
assert!(
std::mem::size_of::<ChunkError>() <= 72,
"Size of error is {} bytes. Let's try to keep errors small.",
std::mem::size_of::<ChunkError>()
);
}
Copy link
Member

@jprochazk jprochazk Aug 13, 2025

Choose a reason for hiding this comment

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

All of these can be const assertions instead, which will fail these earlier, at compile time.

Suggested change
#[test]
fn test_error_size() {
assert!(
std::mem::size_of::<ChunkError>() <= 72,
"Size of error is {} bytes. Let's try to keep errors small.",
std::mem::size_of::<ChunkError>()
);
}
const _: () = assert!(
std::mem::size_of::<ChunkError>() <= 72,
"Error type is too large. Try to reduce its size by boxing some of its variants.",
);

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=9c0bead836c6b305b767f4e6f9ed4b6b

Copy link
Member Author

Choose a reason for hiding this comment

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

My test prints the size of the struct, which is very useful. This can't be done statically afaict.

Also: they don't need to fail on compile-time. This is a performance problem, but you should be able to test and run your new code without having to care about that.

Copy link
Member

@jprochazk jprochazk Aug 13, 2025

Choose a reason for hiding this comment

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

My test prints the size of the struct, which is very useful. This can't be done statically afaict.

No, this is possible.

But bringing in a dependency for that seems overkill. Why do you need it to print the size of the type? R-A will report the failure at the assertion site, which is in the same file as the type. You can hover over it in your IDE when the assertion fails at compile time.

Also: they don't need to fail on compile-time. This is a performance problem, but you should be able to test and run your new code without having to care about that.

It's a performance problem, but one that you are now forcing everyone to be aware of and always solve before merging a PR. The sooner they find out about it, the better.

Scenario: You add an error variant, the size now exceeds some arbitrary threshold. You don't notice this. Then you write some code using that error variant, constructing it in different places, mapping to it from different errors, etc. Then you run your tests-- only NOW you find out about the issue. So you have to go back and fix not only the error type, but also every place you construct it (probably inserting a bunch of Box::new).

Hopefully you've only added one error variant, otherwise you have to also figure out which of the variants you just added is causing the error to go over the size limit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair points, but the added code is quite minimal (just adding Box and a new From implementation), and yes - printing the size is very useful. You wouldn't want to tell clement to just hover the type in vim (especially since it doesn't even work reliably in VSCode)

Copy link
Member

@jprochazk jprochazk Aug 14, 2025

Choose a reason for hiding this comment

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

Fair points, but the added code is quite minimal (just adding Box and a new From implementation)

I think you've underestimating how annoying it is for this to be a test. Two reviewers on this PR asked you to change it... We have precedent for types being checked against some constraints at compile-time, or code not compiling if you're not handling all struct fields/enum variants somewhere. With tests, at least personally I'll only find out about this issue after CI runs on my PR, adding another thing to do just to "make CI green".

You wouldn't want to tell clement to just hover the type in vim

I would, because he uses rust analyzer, like everyone else in this company. Hover information is not a VSCode specific thing.

(especially since it doesn't even work reliably in VSCode)

It is reliable. The only time it doesn't show the size is if the type does not have a size yet. None of these errors have type parameters, so they show the size just fine:

image

Here's what a generic type looks like:
image

And if you fill in the type:
image

Copy link
Member

Choose a reason for hiding this comment

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

We do already depend on const_format in the repository, so using it for this wouldn't be adding a new dependency.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Jan! rustanalyzer struct size reports have been reliable for me as well and anything that puts a failure from compile time to test time is a potential productivity loss

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

thank you <3

Copy link
Member

@Wumpf Wumpf left a comment

Choose a reason for hiding this comment

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

what @jprochazk wrote :)
otherwise lgtm

@emilk emilk merged commit b36df51 into main Aug 13, 2025
41 checks passed
@emilk emilk deleted the emilk/shrink-errors branch August 13, 2025 18:52
emilk added a commit that referenced this pull request Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exclude from changelog PRs with this won't show up in CHANGELOG.md 📉 performance Optimization, memory use, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants