Skip to content

Conversation

@pitdicker
Copy link
Contributor

Three changes that bring the size of DateTime<Tz> down from 48 bytes to 20 bytes:

  • The build script collects all abbreviations and concatenates them into one string. We compactly store an index and length into this string in one i16. All abbreviations are at most 6 characters and the entire string is ca. 650 characters. So it fits easily.
  • The base offset from UTC doesn't fit into an i16, it needs 17 bits. We give it 18 bits of an i32. The DST offset from the base offset is much smaller, it can fit into the remaining 14 bits. This spares one i32.
  • There are 2 bytes of padding in the FixedTimespan type, and 2 bytes of padding in the TzOffset type. If we don't wrap the FixedTimespan but copy its two fields into TzOffset 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.

Copy link
Member

@djc djc left a 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(..) {
Copy link
Member

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("")?

utc_offset: {utc},
dst_offset: {dst},
name: \"{name}\",
abbreviation: {index_len},
Copy link
Member

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?

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.

Very large memory size for DateTime carrying Tz from chrono-tz

2 participants