Skip to content

Commit f73cda7

Browse files
committed
[topo] Switch Env from Arc to Rc, add benchmarks.
1 parent 9f97988 commit f73cda7

File tree

6 files changed

+132
-4
lines changed

6 files changed

+132
-4
lines changed

.cargo/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ watch --clear
4545
-x check-core
4646
-x test-core
4747
-x clippy-core
48+
-x bench-core
4849
"""

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
target/
22
**/*.rs.bk
33
Cargo.lock
44
*.log

CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@ This is a placeholder for if/when the project is ready to onboard more contribut
66

77
## Development environment
88

9+
### Requirements
10+
911
* [rustup](https://rustup.rs)
1012
* `rustup component add clippy rustfmt`
1113
* [cargo-watch](https://crates.io/crates/cargo-watch)
1214

15+
### Workflows
16+
17+
#### Core libraries
18+
19+
From the project root, this command will run the default development loop:
20+
21+
```shell
22+
$ cargo watch-core
23+
```
24+
25+
See [its definition](./.cargo/config) for details.
26+
1327
### moxie-dom dev env
1428

1529
* [node >= LTS](https://nodejs.org)

topo/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ im-rc = "13"
99
owning_ref = "0.4"
1010
tokio-trace = { version = "0.1", features = ["log"] }
1111
topo-macro = { path = "macro" }
12+
13+
[dev-dependencies]
14+
criterion = "0.2"
15+
16+
[[bench]]
17+
name = "simple"
18+
harness = false

topo/benches/simple.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#[macro_use]
2+
extern crate criterion;
3+
4+
use criterion::{black_box, Criterion, ParameterizedBenchmark};
5+
6+
fn empty_env(c: &mut Criterion) {
7+
c.bench_function("call empty env", |b| {
8+
b.iter(|| black_box(topo::call!(|| topo::Id::current())))
9+
});
10+
}
11+
12+
fn create_small_env(c: &mut Criterion) {
13+
c.bench_function("call create small env", |b| {
14+
b.iter(|| {
15+
black_box(topo::call!(|| topo::Id::current(), Env {
16+
u128 => 10
17+
}))
18+
});
19+
});
20+
}
21+
22+
#[allow(clippy::trivially_copy_pass_by_ref)]
23+
fn topo_bench(b: &mut criterion::Bencher, depth: &usize) {
24+
macro_rules! mk {
25+
(go $depth_spec:ident) => {
26+
topo::call!(|| {
27+
mk!(pass $depth_spec 0);
28+
}, Env { u128 => 10 });
29+
};
30+
(pass $depth_spec:ident $call_depth:expr) => {
31+
topo::call!(|| {
32+
mk!(cur $depth_spec ($call_depth + 1));
33+
});
34+
};
35+
(cur twelve $depth:expr) => {
36+
mk!(pass eleven $depth);
37+
};
38+
(cur eleven $depth:expr) => {
39+
mk!(pass ten $depth);
40+
};
41+
(cur ten $depth:expr) => {
42+
mk!(pass nine $depth);
43+
};
44+
(cur nine $depth:expr) => {
45+
mk!(pass eight $depth);
46+
};
47+
(cur eight $depth:expr) => {
48+
mk!(pass seven $depth);
49+
};
50+
(cur seven $depth:expr) => {
51+
mk!(pass six $depth);
52+
};
53+
(cur six $depth:expr) => {
54+
mk!(pass five $depth);
55+
};
56+
(cur five $depth:expr) => {
57+
mk!(pass four $depth);
58+
};
59+
(cur four $depth:expr) => {
60+
mk!(pass three $depth);
61+
};
62+
(cur three $depth:expr) => {
63+
mk!(pass two $depth);
64+
};
65+
(cur two $depth:expr) => {
66+
mk!(pass one $depth);
67+
};
68+
(cur one $depth:expr) => {
69+
mk!(pass zero ($depth + 1)); // lol what a cheater!
70+
};
71+
(cur zero $depth:expr) => {
72+
b.iter(|| {
73+
topo::call!(|| assert_eq!(10, *topo::from_env::<u128>().unwrap()))
74+
});
75+
};
76+
}
77+
match depth {
78+
12 => mk!(go twelve),
79+
11 => mk!(go eleven),
80+
10 => mk!(go ten),
81+
9 => mk!(go nine),
82+
8 => mk!(go eight),
83+
7 => mk!(go seven),
84+
6 => mk!(go six),
85+
5 => mk!(go five),
86+
4 => mk!(go four),
87+
3 => mk!(go three),
88+
2 => mk!(go two),
89+
1 => mk!(go one),
90+
_ => unimplemented!(),
91+
}
92+
}
93+
94+
fn enter_small_env(c: &mut Criterion) {
95+
c.bench(
96+
"topo fns",
97+
ParameterizedBenchmark::new(
98+
"call from_env within small env at depth",
99+
topo_bench,
100+
vec![1, 3, 9, 12],
101+
),
102+
);
103+
}
104+
105+
criterion_group!(benches, empty_env, create_small_env, enter_small_env);
106+
criterion_main!(benches);

topo/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use {
5656
collections::HashMap,
5757
hash::{Hash, Hasher},
5858
ops::Deref,
59-
sync::Arc,
59+
rc::Rc,
6060
},
6161
};
6262

@@ -237,7 +237,7 @@ impl Callsite {
237237
#[doc(hidden)]
238238
#[derive(Clone, Debug, Default)]
239239
pub struct Env {
240-
inner: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
240+
inner: HashMap<TypeId, Rc<dyn Any + Send + Sync>>,
241241
}
242242

243243
impl Env {
@@ -255,7 +255,7 @@ impl Env {
255255
where
256256
E: Any + Send + Sync + 'static,
257257
{
258-
self.inner.insert(TypeId::of::<E>(), Arc::new(e));
258+
self.inner.insert(TypeId::of::<E>(), Rc::new(e));
259259
}
260260
}
261261

0 commit comments

Comments
 (0)