Skip to content

Commit 4eb3ad1

Browse files
authored
Merge pull request #98 from shepmaster/benchmarking
Move benchmarking to Criterion
2 parents 7842902 + 72577ce commit 4eb3ad1

File tree

6 files changed

+88
-44
lines changed

6 files changed

+88
-44
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ license = "MIT"
1616

1717
[features]
1818
compile_failure = []
19-
unstable = []
19+
20+
__internal_expose_string_pool = []
2021

2122
[dependencies]
2223
peresil = "0.3.0"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This exists so that our Rust version choice doesn't affect our
2+
# benchmarking tool choice.
3+
4+
[package]
5+
name = "benchmarks"
6+
version = "0.1.0"
7+
edition = "2024"
8+
publish = false
9+
10+
[dependencies]
11+
sxd-document = { path = "../..", features = ["__internal_expose_string_pool"] }
12+
13+
criterion = "0.6.0"
14+
15+
[[bench]]
16+
name = "string_pool"
17+
harness = false
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
2+
use sxd_document::__internal::StringPool;
3+
4+
fn single_string(c: &mut Criterion) {
5+
let mut group = c.benchmark_group("single_string");
6+
7+
let original = "hello";
8+
9+
group.throughput(Throughput::Bytes(original.len() as u64));
10+
group.bench_function("single_string", |b| {
11+
let pool = StringPool::new();
12+
b.iter(|| pool.intern(original));
13+
});
14+
15+
group.finish();
16+
}
17+
18+
fn many_unique_string(c: &mut Criterion) {
19+
let mut group = c.benchmark_group("many_unique_string");
20+
21+
let strings: Vec<_> = (0..1000).map(|i| format!("str{i}str")).collect();
22+
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();
23+
24+
group.throughput(Throughput::Bytes(total_len as u64));
25+
group.bench_function("many_unique_string", |b| {
26+
let pool = StringPool::new();
27+
28+
b.iter(|| {
29+
for s in &strings {
30+
pool.intern(s);
31+
}
32+
});
33+
});
34+
35+
group.finish();
36+
}
37+
38+
fn many_repeated_string(c: &mut Criterion) {
39+
let mut group = c.benchmark_group("many_repeated_string");
40+
41+
let strings: Vec<_> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
42+
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();
43+
44+
group.throughput(Throughput::Bytes(total_len as u64));
45+
group.bench_function("many_unique_string", |b| {
46+
let pool = StringPool::new();
47+
48+
b.iter(|| {
49+
for s in &strings {
50+
pool.intern(s);
51+
}
52+
});
53+
});
54+
55+
group.finish();
56+
}
57+
58+
criterion_group!(
59+
benches,
60+
single_string,
61+
many_unique_string,
62+
many_repeated_string,
63+
);
64+
criterion_main!(benches);

compatibility-tests/benchmarks/src/lib.rs

Whitespace-only changes.

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
//! Try to leverage the type system as much as possible.
5050
5151
#![deny(rust_2018_idioms)]
52-
#![cfg_attr(feature = "unstable", feature(test))]
5352

5453
#[macro_use]
5554
extern crate peresil;
@@ -68,6 +67,11 @@ pub mod parser;
6867
pub mod thindom;
6968
pub mod writer;
7069

70+
#[cfg(feature = "__internal_expose_string_pool")]
71+
pub mod __internal {
72+
pub use super::string_pool::StringPool;
73+
}
74+
7175
pub use crate::str::XmlChar;
7276

7377
static XML_NS_PREFIX: &str = "xml";

src/string_pool.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -275,45 +275,3 @@ mod test {
275275
assert_eq!(interned.as_ptr(), ptr_val);
276276
}
277277
}
278-
279-
#[cfg(feature = "unstable")]
280-
mod bench {
281-
extern crate test;
282-
283-
use self::test::Bencher;
284-
285-
use super::StringPool;
286-
287-
#[bench]
288-
fn single_string(b: &mut Bencher) {
289-
let s = StringPool::new();
290-
b.iter(|| s.intern("hello"));
291-
b.bytes = "hello".len() as u64;
292-
}
293-
294-
#[bench]
295-
fn many_unique_string(b: &mut Bencher) {
296-
let s = StringPool::new();
297-
298-
let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i)).collect();
299-
b.iter(|| {
300-
for ss in strings.iter() {
301-
s.intern(ss);
302-
}
303-
});
304-
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
305-
}
306-
307-
#[bench]
308-
fn many_repeated_string(b: &mut Bencher) {
309-
let s = StringPool::new();
310-
311-
let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
312-
b.iter(|| {
313-
for ss in strings.iter() {
314-
s.intern(ss);
315-
}
316-
});
317-
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
318-
}
319-
}

0 commit comments

Comments
 (0)