Skip to content

Commit

Permalink
Implement custom TinyStr
Browse files Browse the repository at this point in the history
TinyStr produces terrible code for arbitrary lengths
  • Loading branch information
Kijewski committed Feb 22, 2022
1 parent dc3198f commit 5cad70d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 69 deletions.
69 changes: 10 additions & 59 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions make-tzdb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ pub fn main() -> anyhow::Result<()> {
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use once_cell::race::OnceBox;
use tinystr::TinyAsciiStr;
use tz::TimeZone;
use crate::DbTimeZone;
use crate::{{DbTimeZone, Lower32}};
"#
)?;

Expand Down Expand Up @@ -169,13 +168,12 @@ use crate::DbTimeZone;
f,
"pub(crate) fn tz_by_name(s: &str) -> Option<&'static DbTimeZone> {{"
)?;
let max_len = names_and_indices.iter().map(|t| t.1.len()).max().unwrap();
assert!(max_len <= 32);
writeln!(
f,
" let s: TinyAsciiStr<{}> = s.parse().ok()?;",
names_and_indices.iter().map(|t| t.1.len()).max().unwrap(),
" Some(*TIME_ZONES_BY_NAME.get(Lower32([0u64; 4]).for_str(s)?)?)"
)?;
writeln!(f, " let s = s.to_ascii_lowercase();")?;
writeln!(f, " Some(*TIME_ZONES_BY_NAME.get(&s)?)")?;
writeln!(f, "}}")?;
writeln!(f)?;

Expand Down
4 changes: 2 additions & 2 deletions tzdb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tzdb"
version = "0.0.1"
version = "0.0.3"
edition = "2021"
authors = ["René Kijewski <[email protected]>"]
repository = "https://github.com/Kijewski/tzdb"
Expand All @@ -11,7 +11,7 @@ categories = ["date-and-time"]
readme = "README.md"

[dependencies]
byte-slice-cast = "1.2.0"
once_cell = { version = "1.9.0", features = ["parking_lot", "race"] }
phf = "0.10.1"
tinystr = "0.5.0"
tz-rs = "0.5.3"
4 changes: 2 additions & 2 deletions tzdb/src/generated.rs
Git LFS file not shown
23 changes: 23 additions & 0 deletions tzdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ mod generated;
use std::fmt;
use std::ops::Deref;

use byte_slice_cast::{AsByteSlice, AsMutByteSlice};
use once_cell::race::OnceBox;
use tz::TimeZone;

Expand Down Expand Up @@ -137,6 +138,28 @@ pub trait TimeZoneExt {

impl TimeZoneExt for TimeZone {}

#[repr(align(32))]
struct Lower32([u64; 4]);

impl Lower32 {
#[inline]
fn for_str<'a>(&'a mut self, s: &str) -> Option<&'a str> {
self.0
.as_mut_byte_slice()
.get_mut(..s.len())?
.copy_from_slice(s.as_bytes());

self.0[0] |= 0x2020_2020_2020_2020_u64;
self.0[1] |= 0x2020_2020_2020_2020_u64;
self.0[2] |= 0x2020_2020_2020_2020_u64;
self.0[3] |= 0x2020_2020_2020_2020_u64;

std::str::from_utf8(self.0.as_byte_slice())
.ok()?
.get(..s.len())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 5cad70d

Please sign in to comment.