Skip to content

Use new string_cache_codegen crate for static atoms #136

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -15,5 +15,7 @@ script:
- cargo test --features heapsize
- "cd examples/event-log/ && cargo build && cd ../.."
- "cd examples/summarize-events/ && cargo build && cd ../.."
- "cd string-cache-codegen/ && cargo build && cd .."
- "cd string-cache-codegen/test/ && cargo test && cd ../.."
notifications:
webhooks: http://build.servo.org:54856/travis
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ license = "MIT / Apache-2.0"
repository = "https://github.com/servo/string-cache"
documentation = "http://doc.servo.org/string_cache/"
build = "build.rs"
exclude = ["string-cache-codegen/**/*"]

[lib]
name = "string_cache"
@@ -27,7 +28,8 @@ heap_size = ["heapsize"]
[dependencies]
lazy_static = "0.2"
serde = ">=0.6, <0.9"
phf_shared = "0.7.4"
phf = "0.7.15"
phf_shared = "0.7.15"
debug_unreachable = "0.1.1"

[dev-dependencies]
@@ -41,6 +43,6 @@ optional = true
version = ">=0.1.1, <0.4"
optional = true

[build-dependencies]
phf_generator = "0.7.4"
phf_shared = "0.7.4"
[build-dependencies.string_cache_codegen]
version = "0.2.27"
path = "string-cache-codegen"
68 changes: 7 additions & 61 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,19 @@
extern crate phf_shared;
extern crate phf_generator;
extern crate string_cache_codegen;

#[path = "src/shared.rs"] #[allow(dead_code)] mod shared;
#[path = "src/static_atom_list.rs"] mod static_atom_list;

use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::mem;
use std::io::BufWriter;
use std::path::Path;
use std::slice;

fn main() {
let hash_state = generate();
write_static_atom_set(&hash_state);
write_atom_macro(&hash_state);
}

fn generate() -> phf_generator::HashState {
let mut set = std::collections::HashSet::new();
for atom in static_atom_list::ATOMS {
if !set.insert(atom) {
panic!("duplicate static atom `{:?}`", atom);
}
}
phf_generator::generate_hash(static_atom_list::ATOMS)
}

fn write_static_atom_set(hash_state: &phf_generator::HashState) {
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs");
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("static_atoms.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
macro_rules! w {
($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) }
}
w!("pub static STATIC_ATOM_SET: StaticAtomSet = StaticAtomSet {{");
w!(" key: {},", hash_state.key);
w!(" disps: &[");
for &(d1, d2) in &hash_state.disps {
w!(" ({}, {}),", d1, d2);
}
w!(" ],");
w!(" atoms: &[");
for &idx in &hash_state.map {
w!(" {:?},", static_atom_list::ATOMS[idx]);
}
w!(" ],");
w!("}};");
}

fn write_atom_macro(hash_state: &phf_generator::HashState) {
let set = shared::StaticAtomSet {
key: hash_state.key,
disps: leak(hash_state.disps.clone()),
atoms: leak(hash_state.map.iter().map(|&idx| static_atom_list::ATOMS[idx]).collect()),
};

let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
writeln!(file, r"#[macro_export]").unwrap();
writeln!(file, r"macro_rules! atom {{").unwrap();
for &s in set.iter() {
let data = shared::pack_static(set.get_index_or_hash(s).unwrap() as u32);
writeln!(file, r"({:?}) => {{ $crate::Atom {{ unsafe_data: 0x{:x} }} }};", s, data).unwrap();
let mut builder = string_cache_codegen::AtomSetBuilder::new();
for atom in static_atom_list::ATOMS {
builder.atom(atom);
}
writeln!(file, r"}}").unwrap();
}

fn leak<T>(v: Vec<T>) -> &'static [T] {
let slice = unsafe { slice::from_raw_parts(v.as_ptr(), v.len()) };
mem::forget(v);
slice
builder.build(&mut file, "ServoAtom", "STATIC_ATOM_SET", "atom");
}
3 changes: 2 additions & 1 deletion examples/summarize-events/Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@ authors = [ "The Servo Project Developers" ]
[dependencies]
csv = "0"
rustc-serialize = "0"
phf_shared = "0.7.4"
phf = "0.7.15"
phf_shared = "0.7.15"

[dependencies.string_cache]
path = "../.."
4 changes: 3 additions & 1 deletion examples/summarize-events/src/main.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
extern crate csv;
extern crate string_cache;
extern crate rustc_serialize;
pub extern crate phf;
extern crate phf_shared;

#[path = "../../../src/shared.rs"]
@@ -20,6 +21,7 @@ use string_cache::Atom;

use std::{env, cmp};
use std::collections::hash_map::{HashMap, Entry};
use std::marker::PhantomData;
use std::path::Path;

#[derive(RustcDecodable, Debug)]
@@ -88,7 +90,7 @@ fn main() {

// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
// hazard; the field is only public for the atom!() macro.
_ => Atom { unsafe_data: ev.id }.to_string(),
_ => Atom { unsafe_data: ev.id, kind: PhantomData }.to_string(),
};

match summary.entry(string) {
Loading