Skip to content

Commit

Permalink
More idiomatic code
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 17, 2024
1 parent 9172d26 commit f534a9b
Showing 1 changed file with 32 additions and 42 deletions.
74 changes: 32 additions & 42 deletions string-cache-codegen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,19 @@ impl AtomType {
// which would cause divisions by zero in rust-phf.
self.atoms.insert(String::new());

// Strings over 7 bytes and empty string added to static set
let atoms: Vec<&str> = self
// Strings over 7 bytes + empty string added to static set.
// Otherwise stored inline.
let (static_strs, inline_strs): (Vec<_>, Vec<_>) = self
.atoms
.iter()
.filter(|s| s.len() > 7 || s.is_empty())
.map(|s| &**s)
.collect();
let hash_state = phf_generator::generate_hash(&atoms);
.map(String::as_str)
.partition(|s| s.len() > 7 || s.is_empty());

// Static strings
let hash_state = phf_generator::generate_hash(&static_strs);
let phf_generator::HashState { key, disps, map } = hash_state;
let (disps0, disps1): (Vec<_>, Vec<_>) = disps.into_iter().unzip();
let atoms: Vec<&str> = map.iter().map(|&idx| atoms[idx]).collect();
let atoms: Vec<&str> = map.iter().map(|&idx| static_strs[idx]).collect();
let empty_string_index = atoms.iter().position(|s| s.is_empty()).unwrap() as u32;
let indices = 0..atoms.len() as u32;

Expand Down Expand Up @@ -234,45 +236,33 @@ impl AtomType {
let macro_name = new_term(&*self.macro_name);
let module = module.parse::<proc_macro2::TokenStream>().unwrap();
let atom_prefix = format!("ATOM_{}_", type_name.to_string().to_uppercase());
let const_names: Vec<_> = atoms
.iter()
.map(|atom| {
let mut name = atom_prefix.clone();
for c in atom.chars() {
name.push_str(&format!("_{:02X}", c as u32))
}
new_term(&name)
})
.collect();
let new_const_name = |atom: &str| {
let mut name = atom_prefix.clone();
for c in atom.chars() {
name.push_str(&format!("_{:02X}", c as u32))
}
new_term(&name)
};
let const_names: Vec<_> = atoms.iter().copied().map(new_const_name).collect();

// Strings 7 bytes or less (except empty string) stored inline
let short_strs: Vec<&str> = self
.atoms
.iter()
.filter(|s| s.len() < 8 && !s.is_empty())
.map(|s| &**s)
.collect();
let short_const_names: Vec<_> = short_strs
.iter()
.map(|s| {
let mut name = atom_prefix.clone();
for c in s.chars() {
name.push_str(&format!("_{:02X}", c as u32))
}
new_term(&name)
})
.collect();
let short_values: Vec<_> = short_strs
// Inline strings
let (inline_const_names, inline_values_and_lengths): (Vec<_>, Vec<_>) = inline_strs
.iter()
.map(|s| {
let mut n = 0u64;
let const_name = new_const_name(s);

let mut value = 0u64;
for (index, c) in s.bytes().enumerate() {
n = n | ((c as u64) << (index * 8 + 8));
value = value | ((c as u64) << (index * 8 + 8));
}
n

let len = s.len() as u8;

(const_name, (value, len))
})
.collect();
let short_lens: Vec<_> = short_strs.iter().map(|s| s.len() as u8).collect();
.unzip();
let (inline_values, inline_lengths): (Vec<_>, Vec<_>) =
inline_values_and_lengths.into_iter().unzip();

quote! {
#atom_doc
Expand Down Expand Up @@ -301,7 +291,7 @@ impl AtomType {
pub const #const_names: #type_name = #type_name::pack_static(#indices);
)*
#(
pub const #short_const_names: #type_name = #type_name::pack_inline(#short_values, #short_lens);
pub const #inline_const_names: #type_name = #type_name::pack_inline(#inline_values, #inline_lengths);
)*

#macro_doc
Expand All @@ -311,7 +301,7 @@ impl AtomType {
(#atoms) => { #module::#const_names };
)*
#(
(#short_strs) => { #module::#short_const_names };
(#inline_strs) => { #module::#inline_const_names };
)*
}
}
Expand Down

0 comments on commit f534a9b

Please sign in to comment.