Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ license = "MIT"

[features]
compile_failure = []
unstable = []

__internal_expose_string_pool = []

[dependencies]
peresil = "0.3.0"
Expand Down
17 changes: 17 additions & 0 deletions compatibility-tests/benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This exists so that our Rust version choice doesn't affect our
# benchmarking tool choice.

[package]
name = "benchmarks"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
sxd-document = { path = "../..", features = ["__internal_expose_string_pool"] }

criterion = "0.6.0"

[[bench]]
name = "string_pool"
harness = false
64 changes: 64 additions & 0 deletions compatibility-tests/benchmarks/benches/string_pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use sxd_document::__internal::StringPool;

fn single_string(c: &mut Criterion) {
let mut group = c.benchmark_group("single_string");

let original = "hello";

group.throughput(Throughput::Bytes(original.len() as u64));
group.bench_function("single_string", |b| {
let pool = StringPool::new();
b.iter(|| pool.intern(original));
});

group.finish();
}

fn many_unique_string(c: &mut Criterion) {
let mut group = c.benchmark_group("many_unique_string");

let strings: Vec<_> = (0..1000).map(|i| format!("str{i}str")).collect();
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();

group.throughput(Throughput::Bytes(total_len as u64));
group.bench_function("many_unique_string", |b| {
let pool = StringPool::new();

b.iter(|| {
for s in &strings {
pool.intern(s);
}
});
});

group.finish();
}

fn many_repeated_string(c: &mut Criterion) {
let mut group = c.benchmark_group("many_repeated_string");

let strings: Vec<_> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();

group.throughput(Throughput::Bytes(total_len as u64));
group.bench_function("many_unique_string", |b| {
let pool = StringPool::new();

b.iter(|| {
for s in &strings {
pool.intern(s);
}
});
});

group.finish();
}

criterion_group!(
benches,
single_string,
many_unique_string,
many_repeated_string,
);
criterion_main!(benches);
Empty file.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
//! Try to leverage the type system as much as possible.

#![deny(rust_2018_idioms)]
#![cfg_attr(feature = "unstable", feature(test))]

#[macro_use]
extern crate peresil;
Expand All @@ -68,6 +67,11 @@ pub mod parser;
pub mod thindom;
pub mod writer;

#[cfg(feature = "__internal_expose_string_pool")]
pub mod __internal {
pub use super::string_pool::StringPool;
}

pub use crate::str::XmlChar;

static XML_NS_PREFIX: &str = "xml";
Expand Down
42 changes: 0 additions & 42 deletions src/string_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,45 +275,3 @@ mod test {
assert_eq!(interned.as_ptr(), ptr_val);
}
}

#[cfg(feature = "unstable")]
mod bench {
extern crate test;

use self::test::Bencher;

use super::StringPool;

#[bench]
fn single_string(b: &mut Bencher) {
let s = StringPool::new();
b.iter(|| s.intern("hello"));
b.bytes = "hello".len() as u64;
}

#[bench]
fn many_unique_string(b: &mut Bencher) {
let s = StringPool::new();

let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i)).collect();
b.iter(|| {
for ss in strings.iter() {
s.intern(ss);
}
});
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
}

#[bench]
fn many_repeated_string(b: &mut Bencher) {
let s = StringPool::new();

let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
b.iter(|| {
for ss in strings.iter() {
s.intern(ss);
}
});
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
}
}