-
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
Open
pitdicker
wants to merge
6
commits into
chronotope:main
Choose a base branch
from
pitdicker:abbreviations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e31ae73
Deny dead code
pitdicker 130e88c
Complete FIXME related to phf
pitdicker 43a9611
Test type size
pitdicker e8d41a5
Store abbreviations in a single string, compactly encode index and len
pitdicker 74a4588
Merge offsets into one `i32`
pitdicker 7c47b18
Don't wrap `FixedTimespan` in `TzOffset`
pitdicker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ extern crate parse_zoneinfo; | |
#[cfg(feature = "filter-by-regex")] | ||
extern crate regex; | ||
|
||
use std::collections::BTreeSet; | ||
use std::collections::{BTreeSet, HashSet}; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::{self, BufRead, BufReader, Write}; | ||
|
@@ -30,17 +30,17 @@ fn strip_comments(mut line: String) -> String { | |
|
||
// Generate a list of the time zone periods beyond the first that apply | ||
// to this zone, as a string representation of a static slice. | ||
fn format_rest(rest: Vec<(i64, FixedTimespan)>) -> String { | ||
fn format_rest(rest: Vec<(i64, FixedTimespan)>, abbreviations: &str) -> String { | ||
let mut ret = "&[\n".to_string(); | ||
for (start, FixedTimespan { utc_offset, dst_offset, name }) in rest { | ||
ret.push_str(&format!( | ||
" ({start}, FixedTimespan {{ \ | ||
utc_offset: {utc}, dst_offset: {dst}, name: \"{name}\" \ | ||
utc_offset: {utc}, dst_offset: {dst}, abbreviation: {index_len} \ | ||
}}),\n", | ||
start = start, | ||
utc = utc_offset, | ||
dst = dst_offset, | ||
name = name, | ||
index_len = (abbreviations.find(&name).unwrap() << 3) | name.len(), | ||
)); | ||
} | ||
ret.push_str(" ]"); | ||
|
@@ -68,12 +68,29 @@ fn convert_bad_chars(name: &str) -> String { | |
// TimeZone for any contained struct that implements `Timespans`. | ||
fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()> { | ||
let zones = table.zonesets.keys().chain(table.links.keys()).collect::<BTreeSet<_>>(); | ||
|
||
// Collect all unique abbreviations into a HashSet, sort, and concatenate into a string. | ||
let mut abbreviations = HashSet::new(); | ||
for zone in &zones { | ||
let timespans = table.timespans(zone).unwrap(); | ||
for (_, timespan) in timespans.rest.into_iter().chain(Some((0, timespans.first))) { | ||
abbreviations.insert(timespan.name.clone()); | ||
} | ||
} | ||
let mut abbreviations: Vec<_> = abbreviations.iter().collect(); | ||
abbreviations.sort(); | ||
let mut abbreviations_str = String::new(); | ||
for abbr in abbreviations.drain(..) { | ||
abbreviations_str.push_str(abbr) | ||
} | ||
|
||
writeln!(timezone_file, "use core::fmt::{{self, Debug, Display, Formatter}};",)?; | ||
writeln!(timezone_file, "use core::str::FromStr;\n",)?; | ||
writeln!( | ||
timezone_file, | ||
"use crate::timezone_impl::{{TimeSpans, FixedTimespanSet, FixedTimespan}};\n", | ||
)?; | ||
writeln!(timezone_file, "pub(crate) const ABBREVIATIONS: &str = \"{}\";\n", abbreviations_str)?; | ||
writeln!( | ||
timezone_file, | ||
"/// TimeZones built at compile time from the tz database | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Let's suffix the field name with |
||
}}, | ||
rest: REST | ||
}} | ||
}},\n", | ||
zone = zone_name, | ||
rest = format_rest(timespans.rest), | ||
rest = format_rest(timespans.rest, &abbreviations_str), | ||
utc = timespans.first.utc_offset, | ||
dst = timespans.first.dst_offset, | ||
name = timespans.first.name, | ||
index_len = (abbreviations_str.find(×pans.first.name).unwrap() << 3) | ||
| timespans.first.name.len(), | ||
)?; | ||
} | ||
write!( | ||
|
This file contains 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
This file contains 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
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.
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("")
?