Skip to content
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

Shrink DecodeError from 48 to 32 bytes on 64-bit arch #553

Merged
merged 7 commits into from Jun 15, 2022

Conversation

maciejhirsz
Copy link
Contributor

@maciejhirsz maciejhirsz commented Jun 15, 2022

This is done by two tricks:

  1. Packing the entire description of an enum with it's type name and allowed variants behind a &'static ref.
  2. Reducing DecodeError::CStringNulError to just the position of the null byte.

Aside from the extra struct being added, this is a breaking change due to type names for many manual impls being reduced to just the identifier name (derived impls were already just the identifier here). This is unfortunately necessary as std::any::type_name is not const, and even if it were using it with an external generic type would make it unusable in const environment, meaning it's impossible to construct a static AllowedEnum struct with it. CStringNulError I think is exotic enough and rare enough that dropping information here makes sense, if not we could also Box the internal error (std already allocates there anyway).

If these changes are unacceptable, it's still possible to reduce the size of DecodeError to 40 bytes by just putting the AllowedEnumVariants behind a &'static ref, as that enum alone is 3 words wide.

@codecov
Copy link

codecov bot commented Jun 15, 2022

Codecov Report

Merging #553 (fd9c82e) into trunk (18d5249) will increase coverage by 0.05%.
The diff coverage is 42.85%.

@@            Coverage Diff             @@
##            trunk     #553      +/-   ##
==========================================
+ Coverage   54.44%   54.49%   +0.05%     
==========================================
  Files          49       50       +1     
  Lines        4423     4426       +3     
==========================================
+ Hits         2408     2412       +4     
+ Misses       2015     2014       -1     
Impacted Files Coverage Δ
derive/src/derive_enum.rs 0.00% <ø> (ø)
src/de/impls.rs 58.51% <0.00%> (ø)
src/de/mod.rs 26.37% <0.00%> (ø)
src/error.rs 0.00% <ø> (ø)
tests/derive.rs 97.76% <ø> (-0.02%) ⬇️
tests/issues/issue_498.rs 80.00% <ø> (ø)
src/features/impl_std.rs 66.34% <40.00%> (+0.48%) ⬆️
tests/error_size.rs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 18d5249...fd9c82e. Read the comment docs.

@VictorKoenders

This comment was marked as outdated.

@maciejhirsz
Copy link
Contributor Author

Actually I think I miscalculated, and we can get 32 bytes wide DecodeError by just having the AllowedEnumVariants behind a ref: type_name is 2 words, allowed (AllowedEnumVariants) behind a ref is 1 word, found is half a word and can fit alongside the tag -> 4 words total.

Gonna redo that.

@VictorKoenders
Copy link
Contributor

Can we add a test to validate the size of all Error types in bincode? That way we know if they change and what the actual value is

@maciejhirsz
Copy link
Contributor Author

FYI: this is the variant that makes EncodeError 40 bytes wide with std on:

    /// The encoder tried to encode a `SystemTime`, but it was before `SystemTime::UNIX_EPOCH`
    #[cfg(feature = "std")]
    InvalidSystemTime {
        /// The error that was thrown by the SystemTime
        inner: std::time::SystemTimeError,
        /// The SystemTime that caused the error
        time: std::time::SystemTime,
    },

Could remove time, or box one or the other.

@VictorKoenders
Copy link
Contributor

Interesting, on x86_64-pc-windows-gnu + std, the EncodeError is 32 bytes

But why

@maciejhirsz
Copy link
Contributor Author

Interesting, on x86_64-pc-windows-gnu + std, the EncodeError is 32 bytes

But why

Kind of makes sense, SystemTime is platform specific and it's more precise on Linux than Windows. What is interesting is that SystemTimeError is a newtype for Duration, which has the same layout all platforms.

I've boxed time in that variant, that should keep the whole thing behave well on all platforms with 32-byte EncodeError.

@VictorKoenders
Copy link
Contributor

Oh I thought we were compiling against -msvc as well, and I was very confused that -gnu was different.

Boxing the SystemTime seems like a good solution to me

@VictorKoenders VictorKoenders merged commit 85a9cc7 into bincode-org:trunk Jun 15, 2022
@VictorKoenders
Copy link
Contributor

Thanks!

@maciejhirsz maciejhirsz deleted the slim_error branch June 15, 2022 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants