Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 85 additions & 11 deletions crates/project/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
io::Write,
ops::{Add, Div, Mul, Sub, SubAssign},
path::Path,
sync::LazyLock,
sync::{Arc, LazyLock, Mutex, Weak},
};

use cap_cursor_info::CursorFamily;
Expand Down Expand Up @@ -2748,6 +2748,68 @@ impl Default for ProjectConfiguration {
}
}

#[derive(Debug, Default)]
pub struct ProjectStyleCache {
cached: Mutex<Option<CachedStyle>>,
}

#[derive(Debug)]
struct CachedStyle {
source: Weak<ProjectConfiguration>,
styles: [Option<usize>; 3],
project: Arc<ProjectConfiguration>,
}

impl CachedStyle {
fn matches(&self, source: &Weak<ProjectConfiguration>, styles: [Option<usize>; 3]) -> bool {
self.source.ptr_eq(source) && self.styles == styles
}
}

impl ProjectStyleCache {
pub fn resolve(
&self,
project: &Arc<ProjectConfiguration>,
time: f64,
) -> Arc<ProjectConfiguration> {
let styles = project.style_indices_at(time);
if styles == [None; 3] {
return Arc::clone(project);
}

// A Weak keeps the allocation identity alive and makes Arc::make_mut
// detach before editing, even when the source has only one strong owner.
let source = Arc::downgrade(project);
{
let cached = self.cached.lock().unwrap();
if let Some(cached) = cached
.as_ref()
.filter(|cached| cached.matches(&source, styles))
{
return Arc::clone(&cached.project);
}
}

let resolved = Arc::new(project.style_at(time).into_owned());
let previous = {
let mut cached = self.cached.lock().unwrap();
if let Some(cached) = cached
.as_ref()
.filter(|cached| cached.matches(&source, styles))
{
return Arc::clone(&cached.project);
}
cached.replace(CachedStyle {
source,
styles,
project: Arc::clone(&resolved),
})
};
drop(previous);
resolved
}
}

impl ProjectConfiguration {
pub fn resolved_overlay_order(&self, available: &[OverlayTrack]) -> Vec<OverlayTrack> {
let mut ordered = Vec::with_capacity(available.len());
Expand Down Expand Up @@ -2839,12 +2901,12 @@ impl ProjectConfiguration {
.unwrap_or(&self.cursor)
}

pub fn style_at(&self, time: f64) -> Cow<'_, Self> {
fn style_indices_at(&self, time: f64) -> [Option<usize>; 3] {
let mut selected: [Option<usize>; 3] = [None; 3];
let Some(timeline) = &self.timeline else {
return Cow::Borrowed(self);
return selected;
};
let mut selected: [Option<&StyleSegment>; 3] = [None; 3];
for segment in &timeline.style_segments {
for (index, segment) in timeline.style_segments.iter().enumerate() {
if !segment.is_active_at(time) {
continue;
}
Expand All @@ -2855,25 +2917,37 @@ impl ProjectConfiguration {
]) {
if present
&& current.is_none_or(|previous| {
let previous = &timeline.style_segments[previous];
segment.track > previous.track
|| (segment.track == previous.track && segment.start >= previous.start)
})
{
*current = Some(segment);
*current = Some(index);
}
}
}
let [background, camera, cursor] = selected;
selected
}

pub fn style_at(&self, time: f64) -> Cow<'_, Self> {
let Some(timeline) = &self.timeline else {
return Cow::Borrowed(self);
};
let [background, camera, cursor] = self.style_indices_at(time);
let mut configuration = Cow::Borrowed(self);
if let Some(background) =
background.and_then(|segment| segment.overrides.background.as_ref())
if let Some(background) = background
.and_then(|index| timeline.style_segments[index].overrides.background.as_ref())
{
configuration.to_mut().background = background.clone();
}
if let Some(camera) = camera.and_then(|segment| segment.overrides.camera.as_ref()) {
if let Some(camera) =
camera.and_then(|index| timeline.style_segments[index].overrides.camera.as_ref())
{
configuration.to_mut().camera = camera.clone();
}
if let Some(cursor) = cursor.and_then(|segment| segment.overrides.cursor.as_ref()) {
if let Some(cursor) =
cursor.and_then(|index| timeline.style_segments[index].overrides.cursor.as_ref())
{
configuration.to_mut().cursor = cursor.clone();
}
configuration
Expand Down
90 changes: 87 additions & 3 deletions crates/project/src/configuration/style_resolution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ fn one_pass_styles_match_independent_group_selection() {
timeline.style_segments[index].end = end;
}
let original = serde_json::to_value(&project).unwrap();
for time in [
let project = Arc::new(project);
let cache = ProjectStyleCache::default();
let times = [
-1.0,
-0.0,
0.0,
Expand All @@ -93,7 +95,8 @@ fn one_pass_styles_match_independent_group_selection() {
f64::NAN,
f64::NEG_INFINITY,
f64::INFINITY,
] {
];
for time in times.into_iter().chain(times.into_iter().rev()) {
let expected = baseline_style_at(&project, time);
let actual = project.style_at(time);
assert_eq!(
Expand All @@ -104,12 +107,93 @@ fn one_pass_styles_match_independent_group_selection() {
serde_json::to_value(actual.as_ref()).unwrap(),
serde_json::to_value(expected.as_ref()).unwrap()
);
let cached = cache.resolve(&project, time);
assert_eq!(
serde_json::to_value(cached.as_ref()).unwrap(),
serde_json::to_value(expected.as_ref()).unwrap()
);
assert_eq!(
Arc::ptr_eq(&cached, &project),
matches!(expected, Cow::Borrowed(_))
);
}
assert_eq!(serde_json::to_value(&project).unwrap(), original);
assert_eq!(serde_json::to_value(project.as_ref()).unwrap(), original);
let project = ProjectConfiguration::default();
assert!(matches!(project.style_at(1.0), Cow::Borrowed(_)));
}

#[test]
fn cached_styles_follow_edits_and_keep_old_frames_unchanged() {
let cache = ProjectStyleCache::default();
let mut source = Arc::new(fixture(1, false));
let first = cache.resolve(&source, 0.25);
assert!(Arc::ptr_eq(&first, &cache.resolve(&source, 0.75)));
assert!(Arc::get_mut(&mut source).is_none());
let mut frame = Arc::clone(&first);
Arc::make_mut(&mut frame).background.padding = 99.0;
assert_eq!(cache.resolve(&source, 0.5).background.padding, 0.0);
Arc::make_mut(&mut source).hidden_text_segments.push(17);
let updated = cache.resolve(&source, 0.5);
assert_eq!(updated.hidden_text_segments, [17]);
assert!(first.hidden_text_segments.is_empty());
assert!(!Arc::ptr_eq(&first, &updated));
assert!(Arc::ptr_eq(&cache.resolve(&source, 1.0), &source));
let old = Arc::downgrade(&updated);
drop(updated);
let replacement = Arc::new(fixture(1, false));
assert!(
cache
.resolve(&replacement, 0.5)
.hidden_text_segments
.is_empty()
);
assert!(old.upgrade().is_none());
}

#[test]
fn concurrent_frames_share_the_cached_snapshot() {
let source = Arc::new(fixture(1, false));
let cache = ProjectStyleCache::default();
let barrier = std::sync::Barrier::new(8);
let frames = std::thread::scope(|scope| {
(0..8)
.map(|_| {
scope.spawn(|| {
barrier.wait();
cache.resolve(&source, 0.5)
})
})
.collect::<Vec<_>>()
.into_iter()
.map(|thread| thread.join().unwrap())
.collect::<Vec<_>>()
});
assert!(frames.iter().all(|frame| Arc::ptr_eq(frame, &frames[0])));
}

#[test]
fn concurrent_projects_never_exchange_cached_snapshots() {
let cache = ProjectStyleCache::default();
std::thread::scope(|scope| {
let threads: Vec<_> = (0..4)
.map(|worker| {
let cache = &cache;
scope.spawn(move || {
let mut project = fixture(1, false);
project.hidden_text_segments.push(worker);
let source = Arc::new(project);
for _ in 0..200 {
assert_eq!(cache.resolve(&source, 0.5).hidden_text_segments, [worker]);
}
})
})
.collect();
for thread in threads {
thread.join().unwrap();
}
});
}

#[test]
#[ignore]
fn benchmark_style_resolution() {
Expand Down
Loading
Loading