Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

SG4a: the shape cache lets go, in memory and on disk

shinyobjectz9d237cf06a35
diff --git a/core/shape/src/lib.rs b/core/shape/src/lib.rs
index ccda243a..9cd741d6 100644
--- a/core/shape/src/lib.rs
+++ b/core/shape/src/lib.rs
@@ -200,17 +200,38 @@ pub fn tris(m: &Manifold) -> Tris {
Tris { positions, normals }
}
+/// A shape made faster than this isn't kept on disk: a box is made in microseconds, and a part whose size changed each
+/// frame wrote a file a frame (2.4 million in 12 hours of long renders, 11-21); the disk is for the cuts that cost
+const KEEP_MS: f64 = 2.0;
+/// The bytes of triangles held in memory at most: past it they're let go and made (or read) again as asked, so a
+/// long render's shapes that change each frame don't grow the process for its whole length (SG4a)
+const HELD_BYTES: usize = 1 << 30;
+
+/// The shapes held, and their bytes.
+type Held = (HashMap<String, std::sync::Arc<Tris>>, usize);
+
+/// `t` held for `expr`, the held let go first when they'd pass HELD_BYTES.
+fn hold(cache: &Mutex<Held>, expr: &str, t: &std::sync::Arc<Tris>) {
+ let bytes = (t.positions.len() + t.normals.len()) * 12 + expr.len();
+ let mut c = cache.lock().unwrap();
+ if c.1 + bytes > HELD_BYTES {
+ *c = Default::default();
+ }
+ c.1 += bytes;
+ c.0.insert(expr.to_string(), t.clone());
+}
+
/// The mesh for an expression, built once per expression (a re-cut or a key that doesn't change the shape costs nothing).
pub fn cached(expr: &str) -> Result<std::sync::Arc<Tris>, String> {
- static CACHE: OnceLock<Mutex<HashMap<String, std::sync::Arc<Tris>>>> = OnceLock::new();
+ static CACHE: OnceLock<Mutex<Held>> = OnceLock::new();
let cache = CACHE.get_or_init(Default::default);
- if let Some(t) = cache.lock().unwrap().get(expr) {
+ if let Some(t) = cache.lock().unwrap().0.get(expr) {
return Ok(t.clone());
}
// a buffer mesh's own triangles (buffer.rs): data already, so nothing is kept on disk for it
if let Some(b) = expr.strip_prefix("buffer:") {
let t = std::sync::Arc::new(buffer_tris(b)?.0);
- cache.lock().unwrap().insert(expr.to_string(), t.clone());
+ hold(cache, expr, &t);
return Ok(t);
}
// made by this build before, on disk (kept.rs): read back as it was written
@@ -218,14 +239,15 @@ pub fn cached(expr: &str) -> Result<std::sync::Arc<Tris>, String> {
let t = match kept.as_deref().and_then(kept::read) {
Some(t) => std::sync::Arc::new(t),
None => {
+ let made = std::time::Instant::now();
let t = std::sync::Arc::new(tris(&solid(expr)?));
- if let Some(p) = &kept {
+ if let Some(p) = kept.as_ref().filter(|_| made.elapsed().as_secs_f64() * 1e3 >= KEEP_MS) {
kept::write(p, &t);
}
t
}
};
- cache.lock().unwrap().insert(expr.to_string(), t.clone());
+ hold(cache, expr, &t);
Ok(t)
}
diff --git a/docs/stage-guards.feature b/docs/stage-guards.feature
index 2d9a3e40..7fa357dc 100644
--- a/docs/stage-guards.feature
+++ b/docs/stage-guards.feature
@@ -168,6 +168,7 @@ Feature: Stage guards: a 3D render an agent can't break
| raea-sys-contact | systems/contact/src/lib.rs | 0 | the clipping check over spans; each pair's stretches kept as they come |
| raea-sys-contact | systems/contact/src/model.rs | +12 | forget: the files' triangles and the parts' solids let go |
| raea-runtime-export | runtime/export/src/scene.rs | +18 | told_taking: each frame's scene let go as its parts are made; forget and the memory marks after each end step |
+ | raea-core-shape | core/shape/src/lib.rs | +25 | cached: shapes held to 1 GB in memory, and only those that took 2 ms or more kept on disk |
Then a 13,000-frame 3D render's memory stays within 10% of its first minute's
# Status 11-21 (branch stage-guards-8): the film (13,175 frames, 7:19) renders whole with every end check on, at
# a 9.4 to 13.6 GB peak. Before, memguard killed it past 24 GB (28 to 33 GB) after its last frame. Four causes,
@@ -190,6 +191,12 @@ Feature: Stage guards: a 3D render an agent can't break
# GB to 9.3 GB, its 435 errors, 699 warns and 47 notes the same (the clipping check's in order; it keeps each
# pair's stretches as they come, not every frame's hit). Still not met: memory grows by about 40% after the first
# minute (the kept scenes), and the solids of every part are made at once, 3.8 GB on the film.
+ # Branch stage-guards-16: raea-core-shape's cache of each shape expression's triangles held every one for the
+ # process, in memory and on disk, with nothing let go. A part whose size changes each frame is a new expression
+ # each frame, so a long render grew by one mesh a part a frame, and the disk cache gained 2.46 million files
+ # (9.7 GB, 2.2 million of them boxes) in 12 hours of renders on 11-21, filling the disk to 1 GB free. Memory is
+ # now held to 1 GB of triangles (let go past it, made or read again as asked), and only a shape that took 2 ms
+ # or more to make is kept on disk (the boolean cuts it was for; a box takes microseconds).
Rule: The SDK stops allowing it