-
Notifications
You must be signed in to change notification settings - Fork 57
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
Optimize size of TzOffset
#165
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the integers containing bitshifted fields should be wrapped in a newtype with a const
API that can handle both construction and getting out the individual values.
Instead of simply concatenating all abbrevations, how much smaller does the full string get if you concatenate only abbreviations that aren't already in the earlier string?
let mut abbreviations: Vec<_> = abbreviations.iter().collect(); | ||
abbreviations.sort(); | ||
let mut abbreviations_str = String::new(); | ||
for abbr in abbreviations.drain(..) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is abbrevations.join("")
?
chrono-tz-build/src/lib.rs
Outdated
@@ -208,16 +225,17 @@ impl FromStr for Tz {{ | |||
first: FixedTimespan {{ | |||
utc_offset: {utc}, | |||
dst_offset: {dst}, | |||
name: \"{name}\", | |||
abbreviation: {index_len}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's suffix the field name with _idx
?
Three changes that bring the size of
DateTime<Tz>
down from 48 bytes to 20 bytes:i16
. All abbreviations are at most 6 characters and the entire string is ca. 650 characters. So it fits easily.i16
, it needs 17 bits. We give it 18 bits of ani32
. The DST offset from the base offset is much smaller, it can fit into the remaining 14 bits. This spares onei32
.FixedTimespan
type, and 2 bytes of padding in theTzOffset
type. If we don't wrap theFixedTimespan
but copy its two fields intoTzOffset
we get rid of the padding, which is 33% of the type.A binary compiled with these size optimizations is 1,5Mb smaller 🎉.
Fixes #27.