Skip to content

Commit e31cec3

Browse files
author
bors-servo
authored
Auto merge of #231 - servo:nonzero, r=nox
Add the non-zero optimization to Atom
2 parents 571c140 + d653207 commit e31cec3

File tree

14 files changed

+284
-444
lines changed

14 files changed

+284
-444
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ lazy_static = "1"
2222
serde = "1"
2323
phf_shared = "0.8"
2424
new_debug_unreachable = "1.0"
25-
string_cache_shared = {path = "./shared", version = "0.3"}
2625

2726
[[test]]
2827
name = "small-stack"

examples/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ extern crate string_cache;
33
use string_cache::DefaultAtom;
44

55
fn main() {
6-
76
let mut interned_stuff = Vec::new();
87
let text = "here is a sentence of text that will be tokenised and interned and some repeated \
98
tokens is of text and";
109
for word in text.split_whitespace() {
11-
let seen_before = interned_stuff.iter()
10+
let seen_before = interned_stuff
11+
.iter()
1212
// We can use impl PartialEq<T> where T is anything string-like to compare to
1313
// interned strings to either other interned strings, or actual strings Comparing two
1414
// interned strings is very fast (normally a single cpu operation).

integration-tests/build.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ use std::path::Path;
66
fn main() {
77
string_cache_codegen::AtomType::new("TestAtom", "test_atom!")
88
.atoms(&[
9-
"a", "b", "address", "area", "body", "font-weight", "br", "html", "head", "id",
9+
"a",
10+
"b",
11+
"address",
12+
"area",
13+
"body",
14+
"font-weight",
15+
"br",
16+
"html",
17+
"head",
18+
"id",
1019
])
1120
.write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("test_atom.rs"))
1221
.unwrap()

integration-tests/src/bench.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and cheap to move around, which isn't reflected in these tests.
2828
*/
2929
use crate::TestAtom;
3030

31-
use test::{Bencher, black_box};
31+
use test::{black_box, Bencher};
3232

3333
// Just shorthand
3434
fn mk(x: &str) -> TestAtom {
@@ -142,10 +142,10 @@ macro_rules! bench_all (
142142
);
143143
);
144144

145-
pub const longer_dynamic_a: &'static str
146-
= "Thee Silver Mt. Zion Memorial Orchestra & Tra-La-La Band";
147-
pub const longer_dynamic_b: &'static str
148-
= "Thee Silver Mt. Zion Memorial Orchestra & Tra-La-La Ban!";
145+
pub const longer_dynamic_a: &'static str =
146+
"Thee Silver Mt. Zion Memorial Orchestra & Tra-La-La Band";
147+
pub const longer_dynamic_b: &'static str =
148+
"Thee Silver Mt. Zion Memorial Orchestra & Tra-La-La Ban!";
149149

150150
bench_all!([eq ne lt clone_string] for short_string = "e", "f");
151151
bench_all!([eq ne lt clone_string] for medium_string = "xyzzy01", "xyzzy02");
@@ -206,7 +206,7 @@ macro_rules! bench_rand ( ($name:ident, $len:expr) => (
206206
}
207207
));
208208

209-
bench_rand!(intern_rand_008, 8);
210-
bench_rand!(intern_rand_032, 32);
209+
bench_rand!(intern_rand_008, 8);
210+
bench_rand!(intern_rand_032, 32);
211211
bench_rand!(intern_rand_128, 128);
212212
bench_rand!(intern_rand_512, 512);

integration-tests/src/lib.rs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
// except according to those terms.
99

1010
#![cfg(test)]
11-
1211
#![deny(warnings)]
1312
#![allow(non_upper_case_globals)]
14-
1513
#![cfg_attr(feature = "unstable", feature(test))]
1614

1715
extern crate string_cache;
1816

19-
#[cfg(feature = "unstable")] extern crate test;
20-
#[cfg(feature = "unstable")] extern crate rand;
17+
#[cfg(feature = "unstable")]
18+
extern crate rand;
19+
#[cfg(feature = "unstable")]
20+
extern crate test;
2121

2222
use std::thread;
2323
use string_cache::atom::StaticAtomSet;
@@ -157,32 +157,32 @@ macro_rules! assert_eq_fmt (($fmt:expr, $x:expr, $y:expr) => ({
157157
#[test]
158158
fn repr() {
159159
fn check(s: &str, data: u64) {
160-
assert_eq_fmt!("0x{:016X}", Atom::from(s).unsafe_data, data);
160+
assert_eq_fmt!("0x{:016X}", Atom::from(s).unsafe_data(), data);
161161
}
162162

163163
fn check_static(s: &str, x: Atom) {
164-
assert_eq_fmt!("0x{:016X}", x.unsafe_data, Atom::from(s).unsafe_data);
165-
assert_eq!(0x2, x.unsafe_data & 0xFFFF_FFFF);
164+
assert_eq_fmt!("0x{:016X}", x.unsafe_data(), Atom::from(s).unsafe_data());
165+
assert_eq!(0x2, x.unsafe_data() & 0xFFFF_FFFF);
166166
// The index is unspecified by phf.
167-
assert!((x.unsafe_data >> 32) <= TestAtomStaticSet::get().atoms.len() as u64);
167+
assert!((x.unsafe_data() >> 32) <= TestAtomStaticSet::get().atoms.len() as u64);
168168
}
169169

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

174174
// Static atoms
175-
check_static("a", test_atom!("a"));
175+
check_static("a", test_atom!("a"));
176176
check_static("address", test_atom!("address"));
177-
check_static("area", test_atom!("area"));
177+
check_static("area", test_atom!("area"));
178178

179179
// Inline atoms
180-
check("e", 0x0000_0000_0000_6511);
181-
check("xyzzy", 0x0000_797A_7A79_7851);
180+
check("e", 0x0000_0000_0000_6511);
181+
check("xyzzy", 0x0000_797A_7A79_7851);
182182
check("xyzzy01", 0x3130_797A_7A79_7871);
183183

184184
// Dynamic atoms. This is a pointer so we can't verify every bit.
185-
assert_eq!(0x00, Atom::from("a dynamic string").unsafe_data & 0xf);
185+
assert_eq!(0x00, Atom::from("a dynamic string").unsafe_data() & 0xf);
186186
}
187187

188188
#[test]
@@ -203,23 +203,32 @@ fn atom_macro() {
203203

204204
#[test]
205205
fn match_atom() {
206-
assert_eq!(2, match Atom::from("head") {
207-
test_atom!("br") => 1,
208-
test_atom!("html") | test_atom!("head") => 2,
209-
_ => 3,
210-
});
211-
212-
assert_eq!(3, match Atom::from("body") {
213-
test_atom!("br") => 1,
214-
test_atom!("html") | test_atom!("head") => 2,
215-
_ => 3,
216-
});
217-
218-
assert_eq!(3, match Atom::from("zzzzzz") {
219-
test_atom!("br") => 1,
220-
test_atom!("html") | test_atom!("head") => 2,
221-
_ => 3,
222-
});
206+
assert_eq!(
207+
2,
208+
match Atom::from("head") {
209+
test_atom!("br") => 1,
210+
test_atom!("html") | test_atom!("head") => 2,
211+
_ => 3,
212+
}
213+
);
214+
215+
assert_eq!(
216+
3,
217+
match Atom::from("body") {
218+
test_atom!("br") => 1,
219+
test_atom!("html") | test_atom!("head") => 2,
220+
_ => 3,
221+
}
222+
);
223+
224+
assert_eq!(
225+
3,
226+
match Atom::from("zzzzzz") {
227+
test_atom!("br") => 1,
228+
test_atom!("html") | test_atom!("head") => 2,
229+
_ => 3,
230+
}
231+
);
223232
}
224233

225234
#[test]
@@ -240,29 +249,43 @@ fn ensure_as_ref() {
240249
fn test_ascii_lowercase() {
241250
assert_eq!(Atom::from("").to_ascii_lowercase(), Atom::from(""));
242251
assert_eq!(Atom::from("aZ9").to_ascii_lowercase(), Atom::from("az9"));
243-
assert_eq!(Atom::from("The Quick Brown Fox!").to_ascii_lowercase(), Atom::from("the quick brown fox!"));
244-
assert_eq!(Atom::from("JE VAIS À PARIS").to_ascii_lowercase(), Atom::from("je vais À paris"));
252+
assert_eq!(
253+
Atom::from("The Quick Brown Fox!").to_ascii_lowercase(),
254+
Atom::from("the quick brown fox!")
255+
);
256+
assert_eq!(
257+
Atom::from("JE VAIS À PARIS").to_ascii_lowercase(),
258+
Atom::from("je vais À paris")
259+
);
245260
}
246261

247262
#[test]
248263
fn test_ascii_uppercase() {
249264
assert_eq!(Atom::from("").to_ascii_uppercase(), Atom::from(""));
250265
assert_eq!(Atom::from("aZ9").to_ascii_uppercase(), Atom::from("AZ9"));
251-
assert_eq!(Atom::from("The Quick Brown Fox!").to_ascii_uppercase(), Atom::from("THE QUICK BROWN FOX!"));
252-
assert_eq!(Atom::from("Je vais à Paris").to_ascii_uppercase(), Atom::from("JE VAIS à PARIS"));
266+
assert_eq!(
267+
Atom::from("The Quick Brown Fox!").to_ascii_uppercase(),
268+
Atom::from("THE QUICK BROWN FOX!")
269+
);
270+
assert_eq!(
271+
Atom::from("Je vais à Paris").to_ascii_uppercase(),
272+
Atom::from("JE VAIS à PARIS")
273+
);
253274
}
254275

255276
#[test]
256277
fn test_eq_ignore_ascii_case() {
257278
assert!(Atom::from("").eq_ignore_ascii_case(&Atom::from("")));
258279
assert!(Atom::from("aZ9").eq_ignore_ascii_case(&Atom::from("aZ9")));
259280
assert!(Atom::from("aZ9").eq_ignore_ascii_case(&Atom::from("Az9")));
260-
assert!(Atom::from("The Quick Brown Fox!").eq_ignore_ascii_case(&Atom::from("THE quick BROWN fox!")));
281+
assert!(Atom::from("The Quick Brown Fox!")
282+
.eq_ignore_ascii_case(&Atom::from("THE quick BROWN fox!")));
261283
assert!(Atom::from("Je vais à Paris").eq_ignore_ascii_case(&Atom::from("je VAIS à PARIS")));
262284
assert!(!Atom::from("").eq_ignore_ascii_case(&Atom::from("az9")));
263285
assert!(!Atom::from("aZ9").eq_ignore_ascii_case(&Atom::from("")));
264286
assert!(!Atom::from("aZ9").eq_ignore_ascii_case(&Atom::from("9Za")));
265-
assert!(!Atom::from("The Quick Brown Fox!").eq_ignore_ascii_case(&Atom::from("THE quick BROWN fox!!")));
287+
assert!(!Atom::from("The Quick Brown Fox!")
288+
.eq_ignore_ascii_case(&Atom::from("THE quick BROWN fox!!")));
266289
assert!(!Atom::from("Je vais à Paris").eq_ignore_ascii_case(&Atom::from("JE vais À paris")));
267290
}
268291

shared/Cargo.toml

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)