Skip to content

Commit 4a24f4a

Browse files
committed
feature: part out slab allocator into craballoc
1 parent 9208b9e commit 4a24f4a

File tree

9 files changed

+1473
-7
lines changed

9 files changed

+1473
-7
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[workspace]
2-
members = [
2+
members = [
3+
"crates/craballoc",
34
"crates/example",
45
"crates/example-culling",
56
"crates/example-wasm",
67
"crates/loading-bytes",
78
"crates/renderling",
89
"crates/renderling-ui",
9-
"crates/sandbox"
10+
"crates/sandbox",
1011
]
1112

1213
exclude = ["./shaders"]
@@ -34,6 +35,7 @@ serde_json = "1.0.117"
3435
send_wrapper = "0.6.0"
3536
snafu = "0.7"
3637
syn = { version = "2.0.49", features = ["full", "extra-traits", "parsing"] }
38+
tracing = "0.1.41"
3739
wasm-bindgen = "0.2"
3840
wasm-bindgen-futures = "0.4"
3941
web-sys = "0.3"

crates/craballoc/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "craballoc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[features]
7+
default = ["wgpu"]
8+
wgpu = ["dep:wgpu"]
9+
10+
[dependencies]
11+
async-channel.workspace = true
12+
bytemuck.workspace = true
13+
crabslab.workspace = true
14+
log.workspace = true
15+
rustc-hash.workspace = true
16+
snafu.workspace = true
17+
tracing.workspace = true
18+
wgpu = { workspace = true, optional = true }

crates/craballoc/src/lib.rs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//! GPU and CPU slab allocation.
2+
//!
3+
//! Re-exports [`Array`], [`Id`], [`Slab`] and [`SlabItem`] from
4+
//! [`crabslab`](https://docs.rs/crabslab/latest/crabslab/).
5+
//!
6+
//! User types can automatically derive `SlabItem` in most cases. It is
7+
//! required that your type's fields all implement `SlabItem` and `crabslab`
8+
//! must be in scope.
9+
//!
10+
//! ```
11+
//! use renderling::slab::SlabItem;
12+
//!
13+
//! #[derive(Clone, Copy, SlabItem)]
14+
//! struct UserData {
15+
//! pos: (f32, f32, f32),
16+
//! acc: (f32, f32, f32),
17+
//! }
18+
//! ```
19+
20+
pub mod range;
21+
pub mod runtime;
22+
pub mod slab;
23+
pub mod value;
24+
25+
pub mod prelude {
26+
//! Easy-include prelude module.
27+
28+
pub use super::runtime::*;
29+
pub use super::slab::*;
30+
pub use super::value::*;
31+
}
32+
33+
// #[cfg(feature = "wgpu")]
34+
// mod wgpu_slab;
35+
// #[cfg(feature = "wgpu")]
36+
// pub use wgpu_slab::*;
37+
38+
// #[cfg(test)]
39+
// mod test {
40+
// pub use crabslab::{Array, Id, Slab, SlabItem};
41+
42+
// use crate::slab::SlabAllocator;
43+
44+
// #[test]
45+
// fn mngr_updates_count_sanity() {
46+
// let mngr = SlabAllocator::<Mutex<Vec<u32>>>::default();
47+
// {
48+
// let value = mngr.new_value(666u32);
49+
// assert_eq!(
50+
// 1,
51+
// value.ref_count(),
52+
// "slab should not retain a count on value"
53+
// );
54+
// }
55+
// let _ = mngr.upkeep(());
56+
// assert_eq!(
57+
// 0,
58+
// mngr.update_sources.read().unwrap().len(),
59+
// "value should have dropped with no refs"
60+
// );
61+
// {
62+
// let values = mngr.new_array([666u32, 420u32]);
63+
// assert_eq!(
64+
// 1,
65+
// values.ref_count(),
66+
// "slab should not retain a count on array"
67+
// );
68+
// }
69+
// let _ = mngr.upkeep(());
70+
// assert_eq!(
71+
// 0,
72+
// mngr.update_sources.read().unwrap().len(),
73+
// "array should have dropped with no refs"
74+
// );
75+
// }
76+
77+
// #[test]
78+
// fn range_sanity() {
79+
// let a = Range {
80+
// first_index: 1,
81+
// last_index: 2,
82+
// };
83+
// let b = Range {
84+
// first_index: 0,
85+
// last_index: 0,
86+
// };
87+
// assert!(!a.intersects(&b));
88+
// assert!(!b.intersects(&a));
89+
// }
90+
91+
// #[test]
92+
// fn slab_manager_sanity() {
93+
// let m = SlabAllocator::<Mutex<Vec<u32>>>::default();
94+
// log::info!("allocating 4 unused u32 slots");
95+
// let _ = m.allocate::<u32>();
96+
// let _ = m.allocate::<u32>();
97+
// let _ = m.allocate::<u32>();
98+
// let _ = m.allocate::<u32>();
99+
100+
// log::info!("creating 4 update sources");
101+
// let h4 = m.new_value(0u32);
102+
// let h5 = m.new_value(0u32);
103+
// let h6 = m.new_value(0u32);
104+
// let h7 = m.new_value(0u32);
105+
// log::info!("running upkeep");
106+
// let _ = m.upkeep(());
107+
// assert!(m.recycles.read().unwrap().ranges.is_empty());
108+
// assert_eq!(4, m.update_sources.read().unwrap().len());
109+
// let k = m.update_k.load(Ordering::Relaxed);
110+
// assert_eq!(4, k);
111+
112+
// log::info!("dropping 4 update sources");
113+
// drop(h4);
114+
// drop(h5);
115+
// drop(h6);
116+
// drop(h7);
117+
// let _ = m.upkeep(());
118+
// assert_eq!(1, m.recycles.read().unwrap().ranges.len());
119+
// assert!(m.update_sources.read().unwrap().is_empty());
120+
121+
// log::info!("creating 4 update sources, round two");
122+
// let h4 = m.new_value(0u32);
123+
// let h5 = m.new_value(0u32);
124+
// let h6 = m.new_value(0u32);
125+
// let h7 = m.new_value(0u32);
126+
// assert!(m.recycles.read().unwrap().ranges.is_empty());
127+
// assert_eq!(4, m.update_sources.read().unwrap().len());
128+
// let k = m.update_k.load(Ordering::Relaxed);
129+
// // MAYBE_TODO: recycle "update_k"s instead of incrementing for each new source
130+
// assert_eq!(8, k);
131+
132+
// log::info!("creating one more update source, immediately dropping it and two others");
133+
// let h8 = m.new_value(0u32);
134+
// drop(h8);
135+
// drop(h4);
136+
// drop(h6);
137+
// let _ = m.upkeep(());
138+
// assert_eq!(3, m.recycles.read().unwrap().ranges.len());
139+
// assert_eq!(2, m.update_sources.read().unwrap().len());
140+
// assert_eq!(9, m.update_k.load(Ordering::Relaxed));
141+
142+
// drop(h7);
143+
// drop(h5);
144+
// let _ = m.upkeep(());
145+
// m.defrag();
146+
// assert_eq!(
147+
// 1,
148+
// m.recycles.read().unwrap().ranges.len(),
149+
// "ranges: {:#?}",
150+
// m.recycles.read().unwrap().ranges
151+
// );
152+
// }
153+
// }

0 commit comments

Comments
 (0)