Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: update sharded-slab to 0.1, pool hashmap allocations #1062

Merged
merged 15 commits into from
Oct 22, 2020
Merged
2 changes: 1 addition & 1 deletion tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tracing-serde = { path = "../tracing-serde", version = "0.2", optional = true }
parking_lot = { version = ">= 0.7, <= 0.11", optional = true }

# registry
sharded-slab = { version = "^0.0.9", optional = true }
sharded-slab = { version = "0.1.0", optional = true }
thread_local = { version = "1.0.1", optional = true }

[dev-dependencies]
Expand Down
87 changes: 81 additions & 6 deletions tracing-subscriber/benches/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ fn bench_new_span(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("multithreaded", i), i, |b, &i| {
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
let dispatch = mk_dispatch();
for _ in 0..iters {
let bench = MultithreadedBench::new(mk_dispatch());
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(move || {
for n in 0..i {
Expand Down Expand Up @@ -95,7 +96,8 @@ fn mk_dispatch() -> tracing::Dispatch {
fn bench_event(c: &mut Criterion) {
bench_thrpt(c, "event", |group, i| {
group.bench_with_input(BenchmarkId::new("root/single_threaded", i), i, |b, &i| {
tracing::dispatcher::with_default(&mk_dispatch(), || {
let dispatch = mk_dispatch();
tracing::dispatcher::with_default(&dispatch, || {
b.iter(|| {
for n in 0..i {
tracing::info!(n);
Expand All @@ -106,8 +108,9 @@ fn bench_event(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("root/multithreaded", i), i, |b, &i| {
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
let dispatch = mk_dispatch();
for _ in 0..iters {
let bench = MultithreadedBench::new(mk_dispatch());
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(move || {
for n in 0..i {
Expand Down Expand Up @@ -156,8 +159,9 @@ fn bench_event(c: &mut Criterion) {
|b, &i| {
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
let dispatch = mk_dispatch();
for _ in 0..iters {
let bench = MultithreadedBench::new(mk_dispatch());
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread_with_setup(move |start| {
let span = tracing::info_span!("unique_parent", foo = false);
Expand Down Expand Up @@ -203,13 +207,13 @@ fn bench_event(c: &mut Criterion) {
i,
|b, &i| {
b.iter_custom(|iters| {
let dispatch = mk_dispatch();
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let dispatch = mk_dispatch();
let parent = tracing::dispatcher::with_default(&dispatch, || {
tracing::info_span!("shared_parent", foo = "hello world")
});
let bench = MultithreadedBench::new(dispatch);
let bench = MultithreadedBench::new(dispatch.clone());
let parent2 = parent.clone();
bench.thread_with_setup(move |start| {
let _guard = parent2.enter();
Expand Down Expand Up @@ -249,6 +253,77 @@ fn bench_event(c: &mut Criterion) {
})
},
);
group.bench_with_input(
BenchmarkId::new("multi-parent/multithreaded", i),
i,
|b, &i| {
b.iter_custom(|iters| {
let dispatch = mk_dispatch();
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let parent = tracing::dispatcher::with_default(&dispatch, || {
tracing::info_span!("multiparent", foo = "hello world")
});
let bench = MultithreadedBench::new(dispatch.clone());
let parent2 = parent.clone();
bench.thread_with_setup(move |start| {
let _guard = parent2.enter();
start.wait();
let mut span = tracing::info_span!("parent");
for n in 0..i {
let s = tracing::info_span!(parent: &span, "parent2", n, i);
s.in_scope(|| {
tracing::info!(n);
});
span = s;
}
});
let parent2 = parent.clone();
bench.thread_with_setup(move |start| {
let _guard = parent2.enter();
start.wait();
let mut span = tracing::info_span!("parent");
for n in 0..i {
let s = tracing::info_span!(parent: &span, "parent2", n, i);
s.in_scope(|| {
tracing::info!(n);
});
span = s;
}
});
let parent2 = parent.clone();
bench.thread_with_setup(move |start| {
let _guard = parent2.enter();
start.wait();
let mut span = tracing::info_span!("parent");
for n in 0..i {
let s = tracing::info_span!(parent: &span, "parent2", n, i);
s.in_scope(|| {
tracing::info!(n);
});
span = s;
}
});
let parent2 = parent.clone();
bench.thread_with_setup(move |start| {
let _guard = parent2.enter();
start.wait();
let mut span = tracing::info_span!("parent");
for n in 0..i {
let s = tracing::info_span!(parent: &span, "parent2", n, i);
s.in_scope(|| {
tracing::info!(n);
});
span = s;
}
});
let elapsed = bench.run();
total += elapsed;
}
total
})
},
);
});
}

Expand Down
38 changes: 19 additions & 19 deletions tracing-subscriber/src/registry/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ impl<'a> ExtensionsMut<'a> {
pub(crate) struct ExtensionsInner {
// If extensions are never used, no need to carry around an empty HashMap.
// That's 3 words. Instead, this is only 1 word.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
map: Option<Box<AnyMap>>,
map: AnyMap,
}

impl ExtensionsInner {
/// Create an empty `Extensions`.
#[inline]
pub(crate) fn new() -> ExtensionsInner {
ExtensionsInner { map: None }
ExtensionsInner {
map: AnyMap::default(),
}
}

/// Insert a type into this `Extensions`.
Expand All @@ -129,7 +131,6 @@ impl ExtensionsInner {
/// be returned.
pub(crate) fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T> {
self.map
.get_or_insert_with(|| Box::new(HashMap::default()))
.insert(TypeId::of::<T>(), Box::new(val))
.and_then(|boxed| {
#[allow(warnings)]
Expand All @@ -145,35 +146,34 @@ impl ExtensionsInner {
/// Get a reference to a type previously inserted on this `Extensions`.
pub(crate) fn get<T: 'static>(&self) -> Option<&T> {
self.map
.as_ref()
.and_then(|map| map.get(&TypeId::of::<T>()))
.get(&TypeId::of::<T>())
.and_then(|boxed| (&**boxed as &(dyn Any + 'static)).downcast_ref())
}

/// Get a mutable reference to a type previously inserted on this `Extensions`.
pub(crate) fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.map
.as_mut()
.and_then(|map| map.get_mut(&TypeId::of::<T>()))
.get_mut(&TypeId::of::<T>())
.and_then(|boxed| (&mut **boxed as &mut (dyn Any + 'static)).downcast_mut())
}

/// Remove a type from this `Extensions`.
///
/// If a extension of this type existed, it will be returned.
pub(crate) fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> {
self.map
.as_mut()
.and_then(|map| map.remove(&TypeId::of::<T>()))
.and_then(|boxed| {
#[allow(warnings)]
{
(boxed as Box<Any + 'static>)
.downcast()
.ok()
.map(|boxed| *boxed)
}
})
self.map.remove(&TypeId::of::<T>()).and_then(|boxed| {
#[allow(warnings)]
{
(boxed as Box<Any + 'static>)
.downcast()
.ok()
.map(|boxed| *boxed)
}
})
}

pub(crate) fn clear(&mut self) {
self.map.clear();
}
}

Expand Down
Loading