Skip to content

Commit 49d6720

Browse files
committed
fix: optimize scopes
1 parent 8166f10 commit 49d6720

File tree

6 files changed

+425
-23
lines changed

6 files changed

+425
-23
lines changed

Cargo.lock

Lines changed: 72 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ divan = "0.1.17"
2626
rayon = "1.10.0"
2727
chili = "0.2"
2828
bevy_tasks = { version = "0.16.1", features = [ "multi_threaded" ] }
29+
# Used for some benchmarks
30+
dashmap = "6.1.0"
2931
# Used for A/B perf testing
3032
criterion = { version = "0.5" }
3133

@@ -36,7 +38,7 @@ shuttle = ["dep:shuttle"]
3638
debug = true
3739

3840
[profile.bench]
39-
debug = true
41+
opt-level = 3
4042

4143
# Custom profile for shuttle tests: enable release optimizations so that the shuttle
4244
# tests are less slow, but don't disable debug assertions.
@@ -75,3 +77,11 @@ harness = false
7577
[[bench]]
7678
name = "bevy_tasks"
7779
harness = false
80+
81+
[[bench]]
82+
name = "flood_fill"
83+
harness = false
84+
85+
[[bench]]
86+
name = "flat_scope"
87+
harness = false

benches/flat_scope.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! A benchmark for fork-join workloads adapted from `chili`.
2+
3+
use std::hash::{DefaultHasher, Hash, Hasher};
4+
5+
use criterion::black_box;
6+
use divan::Bencher;
7+
use tracing_subscriber::fmt;
8+
use tracing_subscriber::layer::SubscriberExt;
9+
use tracing_subscriber::util::SubscriberInitExt;
10+
11+
const SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4012, 8196];
12+
13+
fn sizes() -> impl Iterator<Item = usize> {
14+
SIZES.iter().cloned()
15+
}
16+
17+
// -----------------------------------------------------------------------------
18+
// Benchmark
19+
20+
#[divan::bench(args = sizes(), threads = false)]
21+
fn baseline(bencher: Bencher, size: usize) {
22+
bencher.bench_local(move || {
23+
for i in 0..size {
24+
for j in 0..200 {
25+
let mut s = DefaultHasher::new();
26+
i.hash(&mut s);
27+
j.hash(&mut s);
28+
black_box(s.finish());
29+
}
30+
}
31+
});
32+
}
33+
34+
static COMPUTE: forte::ThreadPool = forte::ThreadPool::new();
35+
36+
#[divan::bench(args = sizes(), threads = false)]
37+
fn forte(bencher: Bencher, size: usize) {
38+
use forte::Worker;
39+
40+
COMPUTE.with_worker(|worker| {
41+
bencher.bench_local(|| {
42+
worker.scope(|scope| {
43+
for i in 0..size {
44+
scope.spawn_on(worker, move |_: &Worker| {
45+
for j in 0..200 {
46+
let mut s = DefaultHasher::new();
47+
i.hash(&mut s);
48+
j.hash(&mut s);
49+
black_box(s.finish());
50+
}
51+
});
52+
}
53+
});
54+
});
55+
});
56+
}
57+
58+
#[divan::bench(args = sizes(), threads = false)]
59+
fn rayon(bencher: Bencher, size: usize) {
60+
use rayon::scope;
61+
62+
bencher.bench_local(|| {
63+
scope(|scope| {
64+
for i in 0..size {
65+
scope.spawn(move |_| {
66+
for j in 0..200 {
67+
let mut s = DefaultHasher::new();
68+
i.hash(&mut s);
69+
j.hash(&mut s);
70+
black_box(s.finish());
71+
}
72+
});
73+
}
74+
});
75+
});
76+
}
77+
78+
fn main() {
79+
let fmt_layer = fmt::layer()
80+
.without_time()
81+
.with_target(false)
82+
.with_thread_names(true)
83+
.compact();
84+
85+
tracing_subscriber::registry().with(fmt_layer).init();
86+
87+
COMPUTE.resize_to_available();
88+
89+
divan::main();
90+
}

0 commit comments

Comments
 (0)