authoravatarclover caruso<git@paperclover.net>2026-12-01 14:40:11-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 14:56:10-08:00
log77ddd505408d3e7449f36f8c597a005212f5c4cd
tree7d09dcaf6b027042993567d1fc3f07051a498022
parent2db617af27f7d9ef388066e94ce2dda2a2f8af7a
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

perf(engine): lazy EOF video buffer, still-only sticky tiering, and loader upload optimization


3 files changed, 215 insertions(+), 11 deletions(-)

crates/engine/src/av_decode.m+9-4
......@@ -412,7 +412,7 @@ AvVideoDecoder *av_video_open(
412412 obj.trackStartSec = trackStartSec;
413413 obj.durSec = durSec;
414414 obj.lastFrameIdx = -1;
415 obj.lastFrameRgba = [NSMutableData dataWithLength:(NSUInteger)outW * (NSUInteger)outH * 4];
415 obj.lastFrameRgba = nil;
416416 obj.hasLastFrame = NO;
417417
418418 // Verify AVAssetReader can actually open and decode this asset/codec.
......@@ -530,9 +530,14 @@ int av_video_decode_frame(
530530 CFRelease(chosen);
531531
532532 if (ok) {
533 if (obj.lastFrameRgba.length >= required) {
534 memcpy(obj.lastFrameRgba.mutableBytes, out_rgba, required);
535 obj.hasLastFrame = YES;
533 if (obj.nbFrames <= 0 || frame_idx >= obj.nbFrames - 4) {
534 if (!obj.lastFrameRgba || obj.lastFrameRgba.length < required) {
535 obj.lastFrameRgba = [NSMutableData dataWithLength:required];
536 }
537 if (obj.lastFrameRgba.length >= required) {
538 memcpy(obj.lastFrameRgba.mutableBytes, out_rgba, required);
539 obj.hasLastFrame = YES;
540 }
536541 }
537542 obj.lastFrameIdx = frame_idx;
538543 } else {
crates/engine/src/cache.rs+5
......@@ -669,6 +669,11 @@ impl NodeCache {
669669 }
670670 }
671671
672 #[cfg(test)]
673 pub(crate) fn is_entry_sticky(&self, key: u64) -> bool {
674 self.entries.get(&key).is_some_and(|e| e.sticky)
675 }
676
672677 /// Starts per-node profiling for the upcoming frame evaluation.
673678 pub fn begin_frame_profile(&mut self) {
674679 let stats = self.pool_stats();
crates/engine/src/eval/mod.rs+201-7
......@@ -105,6 +105,45 @@ pub struct Engine {
105105 pub hiq: bool,
106106}
107107
108fn is_video_filename(filename: &str) -> bool {
109 let Some(ext) = std::path::Path::new(filename)
110 .extension()
111 .and_then(|e| e.to_str())
112 else {
113 return false;
114 };
115 matches!(
116 ext.to_ascii_lowercase().as_str(),
117 "mov" | "mp4" | "m4v" | "mkv" | "avi" | "webm" | "mxf"
118 )
119}
120
121fn is_static_loader_node(node: &Node) -> bool {
122 let Some(clip) = node.clips.first() else {
123 return false;
124 };
125 if clip.length.unwrap_or(1) > 1 {
126 return false;
127 }
128 if is_video_filename(&clip.filename) {
129 return false;
130 }
131 if clip
132 .alternate_filename
133 .as_deref()
134 .is_some_and(is_video_filename)
135 {
136 return false;
137 }
138 if node
139 .static_str("ProxyFilename")
140 .is_some_and(is_video_filename)
141 {
142 return false;
143 }
144 true
145}
146
108147impl Engine {
109148 pub fn new(device: wgpu::Device, queue: wgpu::Queue) -> Self {
110149 let supports_f32_filter = device.features().contains(wgpu::Features::FLOAT32_FILTERABLE);
......@@ -508,13 +547,16 @@ impl Engine {
508547 }
509548 let res = self.eval_node_inner(comp, id, time, frame_i, enc, visiting, stats);
510549 visiting.remove(&(id, time_key));
511 if res.content_hash != 0
512 && matches!(
513 comp.node(id).kind.as_str(),
514 "Loader" | "TextPlus" | "Renderer3D" | "pRender" | "PolylineMask" | "WandMask"
515 )
516 {
517 self.cache.mark_entry_sticky(res.content_hash);
550 if res.content_hash != 0 {
551 let node = comp.node(id);
552 let sticky = match node.kind.as_str() {
553 "Loader" => is_static_loader_node(node),
554 "TextPlus" | "Renderer3D" | "pRender" | "PolylineMask" | "WandMask" => true,
555 _ => false,
556 };
557 if sticky {
558 self.cache.mark_entry_sticky(res.content_hash);
559 }
518560 }
519561 self.frame_memo.insert((id, time_key, memo_sz), res.clone());
520562 res
......@@ -1590,4 +1632,156 @@ Composition {
15901632 assert!(eng.cache.vram_drops() > 0);
15911633 assert_eq!(eng.last_bg_vram_drops.get(), eng.cache.vram_drops());
15921634 }
1635
1636 #[test]
1637 fn static_still_loader_is_sticky_while_video_and_sequence_loaders_are_not() {
1638 let dir = std::env::temp_dir().join("fission_sticky_loader_tiering_test");
1639 let _ = std::fs::remove_dir_all(&dir);
1640 std::fs::create_dir_all(&dir).unwrap();
1641
1642 let still_path = dir.join("still.png");
1643 image::RgbaImage::from_pixel(32, 32, image::Rgba([200, 100, 50, 255]))
1644 .save(&still_path)
1645 .unwrap();
1646
1647 for i in 0..3 {
1648 let seq_path = dir.join(format!("seq_{i:04}.png"));
1649 image::RgbaImage::from_pixel(32, 32, image::Rgba([40 * i as u8, 120, 200, 255]))
1650 .save(&seq_path)
1651 .unwrap();
1652 }
1653 let seq_first = dir.join("seq_0000.png");
1654
1655 let video_path = dir.join("anim.mp4");
1656 let has_video = std::process::Command::new("ffmpeg")
1657 .args([
1658 "-y",
1659 "-v",
1660 "error",
1661 "-f",
1662 "lavfi",
1663 "-i",
1664 "testsrc2=size=64x64:rate=24:duration=0.5",
1665 "-c:v",
1666 "libx264",
1667 "-pix_fmt",
1668 "yuv420p",
1669 ])
1670 .arg(&video_path)
1671 .status()
1672 .is_ok_and(|s| s.success());
1673
1674 let video_tool = if has_video {
1675 format!(
1676 r#"
1677 VidLoader = Loader {{
1678 Clips = {{
1679 Clip {{
1680 ID = "Clip1",
1681 Filename = "{}",
1682 FormatID = "QuickTimeMovies",
1683 Length = 12,
1684 TrimIn = 0,
1685 TrimOut = 11,
1686 GlobalStart = 0,
1687 GlobalEnd = 11,
1688 }},
1689 }},
1690 }},"#,
1691 video_path.to_string_lossy().replace('\\', "/")
1692 )
1693 } else {
1694 String::new()
1695 };
1696
1697 let src = format!(
1698 r#"
1699Composition {{
1700 CurrentTime = 0,
1701 RenderRange = {{ 0, 5 }},
1702 GlobalRange = {{ 0, 5 }},
1703 Tools = ordered() {{
1704 StillLoader = Loader {{
1705 Clips = {{
1706 Clip {{
1707 ID = "Clip1",
1708 Filename = "{}",
1709 FormatID = "PNGFormat",
1710 StartFrame = -1,
1711 GlobalStart = 0,
1712 GlobalEnd = 10,
1713 }},
1714 }},
1715 }},
1716 SeqLoader = Loader {{
1717 Clips = {{
1718 Clip {{
1719 ID = "Clip1",
1720 Filename = "{}",
1721 FormatID = "PNGFormat",
1722 StartFrame = 0,
1723 Length = 3,
1724 TrimIn = 0,
1725 TrimOut = 2,
1726 GlobalStart = 0,
1727 GlobalEnd = 2,
1728 }},
1729 }},
1730 }},{video_tool}
1731 }},
1732}}
1733"#,
1734 still_path.to_string_lossy().replace('\\', "/"),
1735 seq_first.to_string_lossy().replace('\\', "/"),
1736 );
1737
1738 let comp = Comp::from_fusion_str(&src).expect("parse loader tiering comp");
1739 let still_id = comp.find_node("StillLoader").unwrap();
1740 let seq_id = comp.find_node("SeqLoader").unwrap();
1741 assert!(is_static_loader_node(comp.node(still_id)));
1742 assert!(!is_static_loader_node(comp.node(seq_id)));
1743 if has_video {
1744 let vid_id = comp.find_node("VidLoader").unwrap();
1745 assert!(!is_static_loader_node(comp.node(vid_id)));
1746 }
1747
1748 let Ok(mut eng) = Engine::new_headless() else {
1749 let _ = std::fs::remove_dir_all(&dir);
1750 return;
1751 };
1752
1753 let (still_tex, _) = eng.evaluate(&comp, still_id, 0.0);
1754 assert_ne!(still_tex.content_hash, 0);
1755 assert!(
1756 eng.cache.is_entry_sticky(still_tex.content_hash),
1757 "static still Loader must be marked sticky"
1758 );
1759
1760 let (seq_tex, _) = eng.evaluate(&comp, seq_id, 0.0);
1761 assert_ne!(seq_tex.content_hash, 0);
1762 assert!(
1763 !eng.cache.is_entry_sticky(seq_tex.content_hash),
1764 "multi-frame image-sequence Loader must NOT be sticky"
1765 );
1766
1767 if has_video {
1768 let vid_id = comp.find_node("VidLoader").unwrap();
1769 let (vid_tex, _) = eng.evaluate(&comp, vid_id, 0.0);
1770 assert_ne!(vid_tex.content_hash, 0);
1771 assert!(
1772 !eng.cache.is_entry_sticky(vid_tex.content_hash),
1773 "video Loader must NOT be sticky"
1774 );
1775
1776 // Also verify lazy EOF frame-hold in video decoder works cleanly past EOF.
1777 let mut dec = crate::media::open_video_decoder(&video_path).expect("open video decoder");
1778 let nb = dec.info().nb_frames;
1779 assert!(nb >= 6);
1780 let near_eof = dec.decode_frame(nb - 2).expect("decode near EOF");
1781 let past_eof = dec.decode_frame(nb + 1).expect("hold frame past EOF");
1782 assert_eq!(near_eof.len(), past_eof.len());
1783 }
1784
1785 let _ = std::fs::remove_dir_all(&dir);
1786 }
15931787}