mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(compose): serialize effect identity cache misses
Keep effect-identity memo lookup, pure hashing and insertion atomic across concurrent render callers. Preserve existing identities and add a contention regression with eight synchronized callers.
This commit is contained in:
1 parent
df055f3dcb
commit
4e7a17cd5d
2 files changed
+30
-6
No files matched your search
@@ -563,14 +563,14 @@ impl LayerKeys {
|
||||
fn identity(&self, layer: &Layer, doc: Option<&Document>) -> u64 {
|
||||
let addr = std::ptr::from_ref(layer) as usize;
|
||||
let in_doc = doc.is_some_and(|d| self.in_doc.get_or_init(|| doc_layer_addresses(d)).contains(&addr));
|
||||
if in_doc && let Some(id) = self.ids.lock().unwrap_or_else(|e| e.into_inner()).get(&addr) {
|
||||
return *id;
|
||||
}
|
||||
let id = self.compute(layer);
|
||||
if in_doc {
|
||||
self.ids.lock().unwrap_or_else(|e| e.into_inner()).insert(addr, id);
|
||||
let mut ids = self.ids.lock().unwrap_or_else(|e| e.into_inner());
|
||||
// Computing an identity only hashes immutable layer data; it does not run Rayon
|
||||
// work or re-enter this memo. Keep the miss and insertion atomic across callers.
|
||||
*ids.entry(addr).or_insert_with(|| self.compute(layer))
|
||||
} else {
|
||||
self.compute(layer)
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
fn compute(&self, layer: &Layer) -> u64 {
|
||||
|
||||
@@ -3190,3 +3190,27 @@ fn effect_cache_keeps_no_pixels_of_a_dropped_layer() {
|
||||
assert_eq!(std::sync::Arc::strong_count(&own), 1);
|
||||
assert_eq!(std::sync::Arc::strong_count(&child), 1);
|
||||
}
|
||||
|
||||
// Concurrent callers share one memoized effect identity.
|
||||
#[test]
|
||||
fn concurrent_effect_identity_misses_compute_once() {
|
||||
let mut doc = identity_doc(SampleType::U8);
|
||||
// Make the existing pure identity hash long enough to expose overlapping misses.
|
||||
doc.layers[1].effects.items = vec![Effect::DropShadow(shadow(4.0, 120.0)); 512];
|
||||
let layer = &doc.layers[1];
|
||||
let keys = LayerKeys::default();
|
||||
let start = std::sync::Barrier::new(8);
|
||||
std::thread::scope(|scope| {
|
||||
let handles: Vec<_> = (0..8)
|
||||
.map(|_| {
|
||||
scope.spawn(|| {
|
||||
start.wait();
|
||||
keys.identity(layer, Some(&doc))
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let ids: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
|
||||
assert!(ids.iter().all(|id| *id == ids[0]));
|
||||
});
|
||||
assert_eq!(keys.built.load(std::sync::atomic::Ordering::Relaxed), 1, "concurrent cache misses must compute one memoized identity");
|
||||
}
|
||||
Reference in new issue
Block a user