Skip to content

Commit 125e9dd

Browse files
committed
Add and use new string-cache-codegen crate
1 parent 1c62469 commit 125e9dd

File tree

15 files changed

+330
-141
lines changed

15 files changed

+330
-141
lines changed

Diff for: .travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ script:
1515
- cargo test --features heapsize
1616
- "cd examples/event-log/ && cargo build && cd ../.."
1717
- "cd examples/summarize-events/ && cargo build && cd ../.."
18+
- "cd string-cache-codegen/ && cargo build && cd .."
19+
- "cd string-cache-codegen/test/ && cargo test && cd ../.."
1820
notifications:
1921
webhooks: http://build.servo.org:54856/travis

Diff for: Cargo.toml

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "MIT / Apache-2.0"
88
repository = "https://github.com/servo/string-cache"
99
documentation = "http://doc.servo.org/string_cache/"
1010
build = "build.rs"
11+
exclude = ["string-cache-codegen/**/*"]
1112

1213
[lib]
1314
name = "string_cache"
@@ -27,7 +28,8 @@ heap_size = ["heapsize"]
2728
[dependencies]
2829
lazy_static = "0.2"
2930
serde = ">=0.6, <0.9"
30-
phf_shared = "0.7.4"
31+
phf = "0.7.15"
32+
phf_shared = "0.7.15"
3133
debug_unreachable = "0.1.1"
3234

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

44-
[build-dependencies]
45-
phf_generator = "0.7.4"
46-
phf_shared = "0.7.4"
46+
[build-dependencies.string_cache_codegen]
47+
version = "0.2.27"
48+
path = "string-cache-codegen"

Diff for: build.rs

+7-61
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,19 @@
1-
extern crate phf_shared;
2-
extern crate phf_generator;
1+
extern crate string_cache_codegen;
32

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

75
use std::env;
86
use std::fs::File;
9-
use std::io::{BufWriter, Write};
10-
use std::mem;
7+
use std::io::BufWriter;
118
use std::path::Path;
12-
use std::slice;
139

1410
fn main() {
15-
let hash_state = generate();
16-
write_static_atom_set(&hash_state);
17-
write_atom_macro(&hash_state);
18-
}
19-
20-
fn generate() -> phf_generator::HashState {
21-
let mut set = std::collections::HashSet::new();
22-
for atom in static_atom_list::ATOMS {
23-
if !set.insert(atom) {
24-
panic!("duplicate static atom `{:?}`", atom);
25-
}
26-
}
27-
phf_generator::generate_hash(static_atom_list::ATOMS)
28-
}
29-
30-
fn write_static_atom_set(hash_state: &phf_generator::HashState) {
31-
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs");
11+
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("static_atoms.rs");
3212
let mut file = BufWriter::new(File::create(&path).unwrap());
33-
macro_rules! w {
34-
($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) }
35-
}
36-
w!("pub static STATIC_ATOM_SET: StaticAtomSet = StaticAtomSet {{");
37-
w!(" key: {},", hash_state.key);
38-
w!(" disps: &[");
39-
for &(d1, d2) in &hash_state.disps {
40-
w!(" ({}, {}),", d1, d2);
41-
}
42-
w!(" ],");
43-
w!(" atoms: &[");
44-
for &idx in &hash_state.map {
45-
w!(" {:?},", static_atom_list::ATOMS[idx]);
46-
}
47-
w!(" ],");
48-
w!("}};");
49-
}
5013

51-
fn write_atom_macro(hash_state: &phf_generator::HashState) {
52-
let set = shared::StaticAtomSet {
53-
key: hash_state.key,
54-
disps: leak(hash_state.disps.clone()),
55-
atoms: leak(hash_state.map.iter().map(|&idx| static_atom_list::ATOMS[idx]).collect()),
56-
};
57-
58-
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs");
59-
let mut file = BufWriter::new(File::create(&path).unwrap());
60-
writeln!(file, r"#[macro_export]").unwrap();
61-
writeln!(file, r"macro_rules! atom {{").unwrap();
62-
for &s in set.iter() {
63-
let data = shared::pack_static(set.get_index_or_hash(s).unwrap() as u32);
64-
writeln!(file, r"({:?}) => {{ $crate::Atom {{ unsafe_data: 0x{:x} }} }};", s, data).unwrap();
14+
let mut builder = string_cache_codegen::AtomSetBuilder::new();
15+
for atom in static_atom_list::ATOMS {
16+
builder.atom(atom);
6517
}
66-
writeln!(file, r"}}").unwrap();
67-
}
68-
69-
fn leak<T>(v: Vec<T>) -> &'static [T] {
70-
let slice = unsafe { slice::from_raw_parts(v.as_ptr(), v.len()) };
71-
mem::forget(v);
72-
slice
18+
builder.build(&mut file, "ServoAtom", "STATIC_ATOM_SET", "atom");
7319
}

Diff for: examples/summarize-events/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ authors = [ "The Servo Project Developers" ]
77
[dependencies]
88
csv = "0"
99
rustc-serialize = "0"
10-
phf_shared = "0.7.4"
10+
phf = "0.7.15"
11+
phf_shared = "0.7.15"
1112

1213
[dependencies.string_cache]
1314
path = "../.."

Diff for: examples/summarize-events/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
extern crate csv;
1111
extern crate string_cache;
1212
extern crate rustc_serialize;
13+
pub extern crate phf;
1314
extern crate phf_shared;
1415

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

2122
use std::{env, cmp};
2223
use std::collections::hash_map::{HashMap, Entry};
24+
use std::marker::PhantomData;
2325
use std::path::Path;
2426

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

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

9496
match summary.entry(string) {

0 commit comments

Comments
 (0)