Skip to content

Commit 4632d47

Browse files
aidanhsSimonSapin
authored andcommitted
Add and use new string-cache-codegen crate
1 parent f8ae0ec commit 4632d47

File tree

10 files changed

+189
-103
lines changed

10 files changed

+189
-103
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ script:
1313
- cargo test --features log-events
1414
- "if [ $TRAVIS_RUST_VERSION = nightly ]; then cargo test --features unstable; fi"
1515
- cargo test --features heapsize
16+
- "cd string-cache-codegen/ && cargo build && cd .."
1617
- "cd examples/event-log/ && cargo build && cd ../.."
1718
- "cd examples/summarize-events/ && cargo build && cd ../.."
1819
notifications:

Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ repository = "https://github.com/servo/string-cache"
99
documentation = "https://docs.rs/string_cache/"
1010
build = "build.rs"
1111

12+
# Do not `exclude` ./string-cache-codegen because we want to include
13+
# ./string-cache-codegen/shared.rs, and `include` is a pain to use
14+
# (It has to be exhaustive.)
15+
# This means that packages for this crate include some unused files,
16+
# but they’re not too big so that shouldn’t be a problem.
17+
1218
[lib]
1319
name = "string_cache"
1420

@@ -36,4 +42,4 @@ heapsize = { version = "0.3", optional = true }
3642
rand = "0.3"
3743

3844
[build-dependencies]
39-
phf_generator = "0.7.4"
45+
string_cache_codegen = { version = "0.3", path = "./string-cache-codegen" }

build.rs

+7-55
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,14 @@
1-
extern crate phf_generator;
1+
extern crate string_cache_codegen;
22

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

66
use std::env;
7-
use std::fs::File;
8-
use std::io::{BufWriter, Write};
97
use std::path::Path;
108

119
fn main() {
12-
let hash_state = generate();
13-
write_static_atom_set(&hash_state);
14-
write_atom_macro(&hash_state);
15-
}
16-
17-
fn generate() -> phf_generator::HashState {
18-
let mut set = std::collections::HashSet::new();
19-
for atom in static_atom_list::ATOMS {
20-
if !set.insert(atom) {
21-
panic!("duplicate static atom `{:?}`", atom);
22-
}
23-
}
24-
phf_generator::generate_hash(static_atom_list::ATOMS)
25-
}
26-
27-
fn write_static_atom_set(hash_state: &phf_generator::HashState) {
28-
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs");
29-
let mut file = BufWriter::new(File::create(&path).unwrap());
30-
macro_rules! w {
31-
($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) }
32-
}
33-
w!("pub static STATIC_ATOM_SET: PhfStrSet = PhfStrSet {{");
34-
w!(" key: {},", hash_state.key);
35-
w!(" disps: &[");
36-
for &(d1, d2) in &hash_state.disps {
37-
w!(" ({}, {}),", d1, d2);
38-
}
39-
w!(" ],");
40-
w!(" atoms: &[");
41-
for &idx in &hash_state.map {
42-
w!(" {:?},", static_atom_list::ATOMS[idx]);
43-
}
44-
w!(" ],");
45-
w!("}};");
46-
}
47-
48-
fn write_atom_macro(hash_state: &phf_generator::HashState) {
49-
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs");
50-
let mut file = BufWriter::new(File::create(&path).unwrap());
51-
writeln!(file, r"#[macro_export]").unwrap();
52-
writeln!(file, r"macro_rules! atom {{").unwrap();
53-
for (i, &idx) in hash_state.map.iter().enumerate() {
54-
writeln!(
55-
file,
56-
r"({:?}) => {{ $crate::Atom {{ unsafe_data: 0x{:x}, phantom: ::std::marker::PhantomData }} }};",
57-
static_atom_list::ATOMS[idx],
58-
shared::pack_static(i as u32),
59-
).unwrap();
60-
}
61-
writeln!(file, r"}}").unwrap();
10+
string_cache_codegen::AtomType::new("atom::tests::TestAtom", "test_atom!")
11+
.atoms(static_atom_list::ATOMS)
12+
.write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("test_atom.rs"))
13+
.unwrap()
6214
}

examples/summarize-events/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern crate string_cache;
1212
extern crate rustc_serialize;
1313
extern crate phf_shared;
1414

15-
#[path = "../../../src/shared.rs"]
15+
#[path = "../../../string-cache-codegen/shared.rs"]
1616
#[allow(dead_code)]
1717
mod shared;
1818

src/atom.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ use self::UnpackedAtom::{Dynamic, Inline, Static};
3737
#[cfg(feature = "log-events")]
3838
use event::Event;
3939

40-
include!(concat!(env!("OUT_DIR"), "/static_atom_set.rs"));
41-
4240
#[cfg(not(feature = "log-events"))]
4341
macro_rules! log (($e:expr) => (()));
4442

@@ -199,7 +197,7 @@ impl StaticAtomSet for EmptyStaticAtomSet {
199197
pub type DefaultAtom = Atom<EmptyStaticAtomSet>;
200198

201199
pub struct Atom<Static: StaticAtomSet> {
202-
/// This field is public so that the `atom!()` macro can use it.
200+
/// This field is public so that the `atom!()` macros can use it.
203201
/// You should not otherwise access this field.
204202
#[doc(hidden)]
205203
pub unsafe_data: u64,
@@ -234,7 +232,7 @@ impl<Static: StaticAtomSet> Atom<Static> {
234232

235233
impl<Static: StaticAtomSet> Default for Atom<Static> {
236234
fn default() -> Self {
237-
atom!("")
235+
Self::from("")
238236
}
239237
}
240238

@@ -581,26 +579,17 @@ fn copy_memory(src: &[u8], dst: &mut [u8]) {
581579
}
582580
}
583581

584-
#[cfg(all(test, feature = "unstable"))]
585-
#[path = "bench.rs"]
586-
mod bench;
587-
588582
#[cfg(test)]
583+
#[macro_use]
589584
mod tests {
590585
use std::mem;
591586
use std::thread;
592-
use super::Atom as GenericAtom;
593-
use super::{StaticAtomSet, StringCacheEntry, STATIC_ATOM_SET, PhfStrSet};
587+
use super::{StaticAtomSet, StringCacheEntry};
594588
use super::UnpackedAtom::{Dynamic, Inline, Static};
595589
use shared::ENTRY_ALIGNMENT;
596590

597-
pub type Atom = GenericAtom<DefaultStatic>;
598-
pub struct DefaultStatic;
599-
impl StaticAtomSet for DefaultStatic {
600-
fn get() -> &'static PhfStrSet {
601-
&STATIC_ATOM_SET
602-
}
603-
}
591+
include!(concat!(env!("OUT_DIR"), "/test_atom.rs"));
592+
pub type Atom = TestAtom;
604593

605594
#[test]
606595
fn test_as_slice() {
@@ -741,17 +730,17 @@ mod tests {
741730
assert_eq_fmt!("0x{:016X}", x.unsafe_data, Atom::from(s).unsafe_data);
742731
assert_eq!(0x2, x.unsafe_data & 0xFFFF_FFFF);
743732
// The index is unspecified by phf.
744-
assert!((x.unsafe_data >> 32) <= DefaultStatic::get().atoms.len() as u64);
733+
assert!((x.unsafe_data >> 32) <= TestAtomStaticSet::get().atoms.len() as u64);
745734
}
746735

747736
// This test is here to make sure we don't change atom representation
748737
// by accident. It may need adjusting if there are changes to the
749738
// static atom table, the tag values, etc.
750739

751740
// Static atoms
752-
check_static("a", atom!("a"));
753-
check_static("address", atom!("address"));
754-
check_static("area", atom!("area"));
741+
check_static("a", test_atom!("a"));
742+
check_static("address", test_atom!("address"));
743+
check_static("area", test_atom!("area"));
755744

756745
// Inline atoms
757746
check("e", 0x0000_0000_0000_6511);
@@ -789,27 +778,27 @@ mod tests {
789778

790779
#[test]
791780
fn atom_macro() {
792-
assert_eq!(atom!("body"), Atom::from("body"));
793-
assert_eq!(atom!("font-weight"), Atom::from("font-weight"));
781+
assert_eq!(test_atom!("body"), Atom::from("body"));
782+
assert_eq!(test_atom!("font-weight"), Atom::from("font-weight"));
794783
}
795784

796785
#[test]
797786
fn match_atom() {
798787
assert_eq!(2, match Atom::from("head") {
799-
atom!("br") => 1,
800-
atom!("html") | atom!("head") => 2,
788+
test_atom!("br") => 1,
789+
test_atom!("html") | test_atom!("head") => 2,
801790
_ => 3,
802791
});
803792

804793
assert_eq!(3, match Atom::from("body") {
805-
atom!("br") => 1,
806-
atom!("html") | atom!("head") => 2,
794+
test_atom!("br") => 1,
795+
test_atom!("html") | test_atom!("head") => 2,
807796
_ => 3,
808797
});
809798

810799
assert_eq!(3, match Atom::from("zzzzzz") {
811-
atom!("br") => 1,
812-
atom!("html") | atom!("head") => 2,
800+
test_atom!("br") => 1,
801+
test_atom!("html") | test_atom!("head") => 2,
813802
_ => 3,
814803
});
815804
}
@@ -868,3 +857,7 @@ mod tests {
868857
assert!(Atom::from("camembert".to_owned()) == Atom::from("camembert"));
869858
}
870859
}
860+
861+
#[cfg(all(test, feature = "unstable"))]
862+
#[path = "bench.rs"]
863+
mod bench;

src/bench.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,12 @@ and cheap to move around, which isn't reflected in these tests.
2727
2828
*/
2929

30-
use atom::tests::Atom;
30+
use atom::tests::TestAtom;
3131
use test::{Bencher, black_box};
3232

33-
macro_rules! test_atom {
34-
($tt: tt) => {{
35-
// Add type annotation to help inference
36-
let atom: Atom = atom!($tt);
37-
atom
38-
}}
39-
}
40-
4133
// Just shorthand
42-
fn mk(x: &str) -> Atom {
43-
Atom::from(x)
34+
fn mk(x: &str) -> TestAtom {
35+
TestAtom::from(x)
4436
}
4537

4638
macro_rules! check_type (($name:ident, $x:expr, $p:pat) => (
@@ -89,7 +81,7 @@ macro_rules! bench_one (
8981
fn intern(b: &mut Bencher) {
9082
let x = $x.to_string();
9183
b.iter(|| {
92-
black_box(Atom::from(&*x));
84+
black_box(TestAtom::from(&*x));
9385
});
9486
}
9587
);
@@ -142,7 +134,7 @@ macro_rules! bench_all (
142134
use std::string::ToString;
143135
use std::iter::repeat;
144136

145-
use atom::tests::Atom;
137+
use atom::tests::TestAtom;
146138
use atom::UnpackedAtom::{Static, Inline, Dynamic};
147139

148140
use super::mk;
@@ -213,7 +205,7 @@ macro_rules! bench_rand ( ($name:ident, $len:expr) => (
213205
*n = (*n % 0x40) + 0x20;
214206
}
215207
let s = str::from_utf8(&buf[..]).unwrap();
216-
black_box(Atom::from(s));
208+
black_box(TestAtom::from(s));
217209
});
218210
}
219211
));

src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ extern crate phf_shared;
2424

2525
pub use atom::{Atom, StaticAtomSet, PhfStrSet, EmptyStaticAtomSet, DefaultAtom};
2626

27-
include!(concat!(env!("OUT_DIR"), "/atom_macro.rs"));
28-
2927
#[cfg(feature = "log-events")]
3028
#[macro_use]
3129
pub mod event;
3230

3331
pub mod atom;
34-
pub mod shared;
32+
33+
#[path = "../string-cache-codegen/shared.rs"]
34+
mod shared;
35+
36+
// Make test_atom! macro work in this crate.
37+
// `$crate` would not be appropriate for other crates creating such macros
38+
mod string_cache {
39+
pub use {Atom, StaticAtomSet, PhfStrSet};
40+
}

string-cache-codegen/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
3+
name = "string_cache_codegen"
4+
version = "0.3.0"
5+
authors = [ "The Servo Project Developers" ]
6+
description = "A codegen library for string-cache, developed as part of the Servo project."
7+
license = "MIT / Apache-2.0"
8+
repository = "https://github.com/servo/string-cache"
9+
documentation = "https://docs.rs/string_cache_codegen/"
10+
11+
[lib]
12+
name = "string_cache_codegen"
13+
path = "lib.rs"
14+
15+
[dependencies]
16+
phf_generator = "0.7.15"

0 commit comments

Comments
 (0)