| ... | @@ -106,6 +106,37 @@ pub fn default_ram_cache_budget_mb() -> u64 { | ... | @@ -106,6 +106,37 @@ pub fn default_ram_cache_budget_mb() -> u64 { |
| 106 | ram_cache_budget_mb_for_total_bytes(system_memory_bytes()) | 106 | ram_cache_budget_mb_for_total_bytes(system_memory_bytes()) |
| 107 | } | 107 | } |
| 108 | | 108 | |
| | 109 | fn scaled_prefetch_budget_mb(total_bytes: u64, fallback_mb: u64) -> u64 { |
| | 110 | let gib = total_bytes / (1024 * 1024 * 1024); |
| | 111 | if gib >= 128 { |
| | 112 | 2048 |
| | 113 | } else if gib >= 64 { |
| | 114 | 1024 |
| | 115 | } else if gib >= 32 { |
| | 116 | 512 |
| | 117 | } else { |
| | 118 | fallback_mb |
| | 119 | } |
| | 120 | } |
| | 121 | |
| | 122 | pub(crate) fn prefetch_vram_budget_mb_for_total_bytes(total_bytes: u64) -> u64 { |
| | 123 | scaled_prefetch_budget_mb(total_bytes, DEFAULT_PREFETCH_VRAM_MB) |
| | 124 | } |
| | 125 | |
| | 126 | pub(crate) fn prefetch_media_budget_mb_for_total_bytes(total_bytes: u64) -> u64 { |
| | 127 | scaled_prefetch_budget_mb(total_bytes, DEFAULT_PREFETCH_MEDIA_MB) |
| | 128 | } |
| | 129 | |
| | 130 | /// Returns the default background prefetcher VRAM budget in MiB scaled to host physical RAM. |
| | 131 | pub fn default_prefetch_vram_budget_mb() -> u64 { |
| | 132 | prefetch_vram_budget_mb_for_total_bytes(system_memory_bytes()) |
| | 133 | } |
| | 134 | |
| | 135 | /// Returns the default background prefetcher media decode cache budget in MiB scaled to host physical RAM. |
| | 136 | pub fn default_prefetch_media_budget_mb() -> u64 { |
| | 137 | prefetch_media_budget_mb_for_total_bytes(system_memory_bytes()) |
| | 138 | } |
| | 139 | |
| 109 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 140 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 110 | pub struct RamFrameKey { | 141 | pub struct RamFrameKey { |
| 111 | pub target: NodeId, | 142 | pub target: NodeId, |
| ... | @@ -144,7 +175,8 @@ struct RamCacheInner { | ... | @@ -144,7 +175,8 @@ struct RamCacheInner { |
| 144 | } | 175 | } |
| 145 | | 176 | |
| 146 | impl RamCacheInner { | 177 | impl RamCacheInner { |
| 147 | fn evict_over_budget(&mut self) { | 178 | fn evict_over_budget(&mut self) -> usize { |
| | 179 | let mut evicted = 0; |
| 148 | while self.total_bytes > self.budget_bytes && !self.entries.is_empty() { | 180 | while self.total_bytes > self.budget_bytes && !self.entries.is_empty() { |
| 149 | let Some((&oldest_key, _)) = self.entries.iter().min_by_key(|(_, e)| e.last_used) else { | 181 | let Some((&oldest_key, _)) = self.entries.iter().min_by_key(|(_, e)| e.last_used) else { |
| 150 | break; | 182 | break; |
| ... | @@ -152,8 +184,10 @@ impl RamCacheInner { | ... | @@ -152,8 +184,10 @@ impl RamCacheInner { |
| 152 | if let Some(removed) = self.entries.remove(&oldest_key) { | 184 | if let Some(removed) = self.entries.remove(&oldest_key) { |
| 153 | self.total_bytes = self.total_bytes.saturating_sub(removed.frame.byte_size()); | 185 | self.total_bytes = self.total_bytes.saturating_sub(removed.frame.byte_size()); |
| 154 | self.evictions += 1; | 186 | self.evictions += 1; |
| | 187 | evicted += 1; |
| 155 | } | 188 | } |
| 156 | } | 189 | } |
| | 190 | evicted |
| 157 | } | 191 | } |
| 158 | } | 192 | } |
| 159 | | 193 | |
| ... | @@ -162,6 +196,7 @@ impl RamCacheInner { | ... | @@ -162,6 +196,7 @@ impl RamCacheInner { |
| 162 | pub struct RamFrameCache { | 196 | pub struct RamFrameCache { |
| 163 | inner: Arc<Mutex<RamCacheInner>>, | 197 | inner: Arc<Mutex<RamCacheInner>>, |
| 164 | cache_epoch: Arc<AtomicU64>, | 198 | cache_epoch: Arc<AtomicU64>, |
| | 199 | change_gen: Arc<AtomicU64>, |
| 165 | } | 200 | } |
| 166 | | 201 | |
| 167 | impl Default for RamFrameCache { | 202 | impl Default for RamFrameCache { |
| ... | @@ -187,6 +222,7 @@ impl RamFrameCache { | ... | @@ -187,6 +222,7 @@ impl RamFrameCache { |
| 187 | evictions: 0, | 222 | evictions: 0, |
| 188 | })), | 223 | })), |
| 189 | cache_epoch: Arc::new(AtomicU64::new(1)), | 224 | cache_epoch: Arc::new(AtomicU64::new(1)), |
| | 225 | change_gen: Arc::new(AtomicU64::new(1)), |
| 190 | } | 226 | } |
| 191 | } | 227 | } |
| 192 | | 228 | |
| ... | @@ -198,6 +234,12 @@ impl RamFrameCache { | ... | @@ -198,6 +234,12 @@ impl RamFrameCache { |
| 198 | self.cache_epoch.load(Ordering::Acquire) | 234 | self.cache_epoch.load(Ordering::Acquire) |
| 199 | } | 235 | } |
| 200 | | 236 | |
| | 237 | /// Monotonic counter incremented on cache entry insertion, eviction, clear, or invalidation |
| | 238 | /// (unchanged on `get` hits), allowing UI callers to skip re-collecting cached frame ranges. |
| | 239 | pub fn change_generation(&self) -> u64 { |
| | 240 | self.change_gen.load(Ordering::Acquire) |
| | 241 | } |
| | 242 | |
| 201 | #[cfg(not(target_arch = "wasm32"))] | 243 | #[cfg(not(target_arch = "wasm32"))] |
| 202 | pub(crate) fn epoch_arc(&self) -> Arc<AtomicU64> { | 244 | pub(crate) fn epoch_arc(&self) -> Arc<AtomicU64> { |
| 203 | Arc::clone(&self.cache_epoch) | 245 | Arc::clone(&self.cache_epoch) |
| ... | @@ -206,15 +248,18 @@ impl RamFrameCache { | ... | @@ -206,15 +248,18 @@ impl RamFrameCache { |
| 206 | pub fn set_budget_mb(&self, budget_mb: u64) { | 248 | pub fn set_budget_mb(&self, budget_mb: u64) { |
| 207 | if let Ok(mut guard) = self.inner.lock() { | 249 | if let Ok(mut guard) = self.inner.lock() { |
| 208 | guard.budget_bytes = budget_mb.saturating_mul(1024 * 1024); | 250 | guard.budget_bytes = budget_mb.saturating_mul(1024 * 1024); |
| 209 | guard.evict_over_budget(); | 251 | if guard.evict_over_budget() > 0 { |
| | 252 | self.change_gen.fetch_add(1, Ordering::AcqRel); |
| | 253 | } |
| 210 | } | 254 | } |
| 211 | } | 255 | } |
| 212 | | 256 | |
| | 257 | pub fn budget_bytes(&self) -> u64 { |
| | 258 | self.inner.lock().map(|g| g.budget_bytes).unwrap_or(0) |
| | 259 | } |
| | 260 | |
| 213 | pub fn budget_mb(&self) -> u64 { | 261 | pub fn budget_mb(&self) -> u64 { |
| 214 | self.inner | 262 | self.budget_bytes() / (1024 * 1024) |
| 215 | .lock() | | |
| 216 | .map(|g| g.budget_bytes / (1024 * 1024)) | | |
| 217 | .unwrap_or(0) | | |
| 218 | } | 263 | } |
| 219 | | 264 | |
| 220 | pub fn get(&self, key: RamFrameKey) -> Option<RamFrame> { | 265 | pub fn get(&self, key: RamFrameKey) -> Option<RamFrame> { |
| ... | @@ -237,7 +282,9 @@ impl RamFrameCache { | ... | @@ -237,7 +282,9 @@ impl RamFrameCache { |
| 237 | let Ok(mut guard) = self.inner.lock() else { | 282 | let Ok(mut guard) = self.inner.lock() else { |
| 238 | return; | 283 | return; |
| 239 | }; | 284 | }; |
| 240 | Self::insert_locked(&mut guard, key, frame); | 285 | if Self::insert_locked(&mut guard, key, frame) { |
| | 286 | self.change_gen.fetch_add(1, Ordering::AcqRel); |
| | 287 | } |
| 241 | } | 288 | } |
| 242 | | 289 | |
| 243 | pub fn insert_if_epoch(&self, expected_epoch: u64, key: RamFrameKey, frame: RamFrame) -> bool { | 290 | pub fn insert_if_epoch(&self, expected_epoch: u64, key: RamFrameKey, frame: RamFrame) -> bool { |
| ... | @@ -247,14 +294,16 @@ impl RamFrameCache { | ... | @@ -247,14 +294,16 @@ impl RamFrameCache { |
| 247 | if self.cache_epoch.load(Ordering::Acquire) != expected_epoch { | 294 | if self.cache_epoch.load(Ordering::Acquire) != expected_epoch { |
| 248 | return false; | 295 | return false; |
| 249 | } | 296 | } |
| 250 | Self::insert_locked(&mut guard, key, frame); | 297 | if Self::insert_locked(&mut guard, key, frame) { |
| | 298 | self.change_gen.fetch_add(1, Ordering::AcqRel); |
| | 299 | } |
| 251 | true | 300 | true |
| 252 | } | 301 | } |
| 253 | | 302 | |
| 254 | fn insert_locked(guard: &mut RamCacheInner, key: RamFrameKey, frame: RamFrame) { | 303 | fn insert_locked(guard: &mut RamCacheInner, key: RamFrameKey, frame: RamFrame) -> bool { |
| 255 | let bytes = frame.byte_size(); | 304 | let bytes = frame.byte_size(); |
| 256 | if guard.budget_bytes == 0 || bytes > guard.budget_bytes { | 305 | if guard.budget_bytes == 0 || bytes > guard.budget_bytes { |
| 257 | return; | 306 | return false; |
| 258 | } | 307 | } |
| 259 | guard.tick += 1; | 308 | guard.tick += 1; |
| 260 | let tick = guard.tick; | 309 | let tick = guard.tick; |
| ... | @@ -263,7 +312,8 @@ impl RamFrameCache { | ... | @@ -263,7 +312,8 @@ impl RamFrameCache { |
| 263 | } | 312 | } |
| 264 | guard.total_bytes += bytes; | 313 | guard.total_bytes += bytes; |
| 265 | guard.entries.insert(key, RamEntry { frame, last_used: tick }); | 314 | guard.entries.insert(key, RamEntry { frame, last_used: tick }); |
| 266 | guard.evict_over_budget(); | 315 | let _ = guard.evict_over_budget(); |
| | 316 | true |
| 267 | } | 317 | } |
| 268 | | 318 | |
| 269 | pub fn clear(&self) { | 319 | pub fn clear(&self) { |
| ... | @@ -271,6 +321,7 @@ impl RamFrameCache { | ... | @@ -271,6 +321,7 @@ impl RamFrameCache { |
| 271 | guard.entries.clear(); | 321 | guard.entries.clear(); |
| 272 | guard.total_bytes = 0; | 322 | guard.total_bytes = 0; |
| 273 | self.cache_epoch.fetch_add(1, Ordering::AcqRel); | 323 | self.cache_epoch.fetch_add(1, Ordering::AcqRel); |
| | 324 | self.change_gen.fetch_add(1, Ordering::AcqRel); |
| 274 | } | 325 | } |
| 275 | } | 326 | } |
| 276 | | 327 | |
| ... | @@ -291,6 +342,9 @@ impl RamFrameCache { | ... | @@ -291,6 +342,9 @@ impl RamFrameCache { |
| 291 | guard.total_bytes = guard.total_bytes.saturating_sub(removed.frame.byte_size()); | 342 | guard.total_bytes = guard.total_bytes.saturating_sub(removed.frame.byte_size()); |
| 292 | } | 343 | } |
| 293 | } | 344 | } |
| | 345 | if count > 0 { |
| | 346 | self.change_gen.fetch_add(1, Ordering::AcqRel); |
| | 347 | } |
| 294 | count | 348 | count |
| 295 | } | 349 | } |
| 296 | | 350 | |
| ... | @@ -400,11 +454,16 @@ pub struct RamPrefetcher { | ... | @@ -400,11 +454,16 @@ pub struct RamPrefetcher { |
| 400 | #[cfg(not(target_arch = "wasm32"))] | 454 | #[cfg(not(target_arch = "wasm32"))] |
| 401 | impl RamPrefetcher { | 455 | impl RamPrefetcher { |
| 402 | pub fn new(cache: RamFrameCache, worker_vram_budget_mb: u64) -> Self { | 456 | pub fn new(cache: RamFrameCache, worker_vram_budget_mb: u64) -> Self { |
| 403 | Self::init(None, cache, worker_vram_budget_mb.max(128), 512) | 457 | Self::init( |
| | 458 | None, |
| | 459 | cache, |
| | 460 | worker_vram_budget_mb.max(128), |
| | 461 | default_prefetch_media_budget_mb().max(512), |
| | 462 | ) |
| 404 | } | 463 | } |
| 405 | | 464 | |
| 406 | /// Creates a `RamPrefetcher` whose worker `Engine` shares an existing `wgpu::Device` | 465 | /// Creates a `RamPrefetcher` whose worker `Engine` shares an existing `wgpu::Device` |
| 407 | /// and `wgpu::Queue` with modest budgets (256 MiB VRAM, 128 MiB media). | 466 | /// and `wgpu::Queue` with RAM-scaled VRAM and media budgets. |
| 408 | pub fn new_with_device( | 467 | pub fn new_with_device( |
| 409 | device: Arc<wgpu::Device>, | 468 | device: Arc<wgpu::Device>, |
| 410 | queue: Arc<wgpu::Queue>, | 469 | queue: Arc<wgpu::Queue>, |
| ... | @@ -413,8 +472,8 @@ impl RamPrefetcher { | ... | @@ -413,8 +472,8 @@ impl RamPrefetcher { |
| 413 | Self::init( | 472 | Self::init( |
| 414 | Some((device, queue)), | 473 | Some((device, queue)), |
| 415 | cache.into(), | 474 | cache.into(), |
| 416 | DEFAULT_PREFETCH_VRAM_MB, | 475 | default_prefetch_vram_budget_mb(), |
| 417 | DEFAULT_PREFETCH_MEDIA_MB, | 476 | default_prefetch_media_budget_mb(), |
| 418 | ) | 477 | ) |
| 419 | } | 478 | } |
| 420 | | 479 | |
| ... | @@ -552,6 +611,7 @@ impl RamPrefetcher { | ... | @@ -552,6 +611,7 @@ impl RamPrefetcher { |
| 552 | match Engine::new_headless() { | 611 | match Engine::new_headless() { |
| 553 | Ok(mut eng) => { | 612 | Ok(mut eng) => { |
| 554 | eng.set_vram_budget_mb(vram_mb); | 613 | eng.set_vram_budget_mb(vram_mb); |
| | 614 | eng.media.set_budget_mb(media_mb); |
| 555 | engine_opt = Some(eng); | 615 | engine_opt = Some(eng); |
| 556 | } | 616 | } |
| 557 | Err(_) => { | 617 | Err(_) => { |
| ... | @@ -585,49 +645,80 @@ impl RamPrefetcher { | ... | @@ -585,49 +645,80 @@ impl RamPrefetcher { |
| 585 | let span = (end - start + 1).max(1); | 645 | let span = (end - start + 1).max(1); |
| 586 | let count = job.lookahead.max(0).min(span); | 646 | let count = job.lookahead.max(0).min(span); |
| 587 | | 647 | |
| 588 | for offset in 1..=count { | 648 | const PREFETCH_MEDIA_CHUNK: i64 = 4; |
| | 649 | let mut chunk_start = 1i64; |
| | 650 | 'prefetch: while chunk_start <= count { |
| 589 | if generation.load(Ordering::Acquire) != job_gen | 651 | if generation.load(Ordering::Acquire) != job_gen |
| 590 | || cache_epoch.load(Ordering::Acquire) != job_epoch | 652 | || cache_epoch.load(Ordering::Acquire) != job_epoch |
| 591 | { | 653 | { |
| 592 | break; | 654 | break 'prefetch; |
| 593 | } | 655 | } |
| 594 | let f = start + (job.center_frame - start + offset).rem_euclid(span); | 656 | let chunk_end = (chunk_start + PREFETCH_MEDIA_CHUNK - 1).min(count); |
| 595 | let key = RamFrameKey { | 657 | let mut chunk_items = Vec::with_capacity((chunk_end - chunk_start + 1) as usize); |
| 596 | target: job.target, | 658 | let mut batch_frames = Vec::with_capacity((chunk_end - chunk_start + 1) as usize); |
| 597 | revision: job.comp.revision, | 659 | for offset in chunk_start..=chunk_end { |
| 598 | frame: f, | 660 | let f = start + (job.center_frame - start + offset).rem_euclid(span); |
| 599 | proxy_scale: engine.proxy_scale, | 661 | let key = RamFrameKey { |
| 600 | hiq: engine.hiq, | 662 | target: job.target, |
| 601 | use_proxy_clips: engine.use_proxy_clips, | 663 | revision: job.comp.revision, |
| 602 | }; | 664 | frame: f, |
| 603 | if cache.contains(key) { | 665 | proxy_scale: engine.proxy_scale, |
| 604 | continue; | 666 | hiq: engine.hiq, |
| | 667 | use_proxy_clips: engine.use_proxy_clips, |
| | 668 | }; |
| | 669 | if !cache.contains(key) { |
| | 670 | chunk_items.push((f, key)); |
| | 671 | batch_frames.push(f as f64); |
| | 672 | } |
| 605 | } | 673 | } |
| 606 | let (tex, stats) = engine.evaluate(&job.comp, job.target, f as f64); | 674 | |
| 607 | if generation.load(Ordering::Acquire) != job_gen | 675 | if !batch_frames.is_empty() { |
| 608 | || cache_epoch.load(Ordering::Acquire) != job_epoch | 676 | engine.media.prefetch_frames_with_proxy( |
| 609 | { | 677 | &job.comp, |
| 610 | break; | 678 | job.target, |
| | 679 | &batch_frames, |
| | 680 | &engine.expr, |
| | 681 | job.use_proxy_clips, |
| | 682 | ); |
| 611 | } | 683 | } |
| 612 | if let Ok(rgba) = engine.readback_rgba(&tex) { | 684 | |
| 613 | // Re-check generation immediately before inserting after readback completes. | 685 | for (f, key) in chunk_items { |
| 614 | if generation.load(Ordering::Acquire) != job_gen { | 686 | if generation.load(Ordering::Acquire) != job_gen |
| 615 | break; | 687 | || cache_epoch.load(Ordering::Acquire) != job_epoch |
| | 688 | { |
| | 689 | break 'prefetch; |
| | 690 | } |
| | 691 | if cache.contains(key) { |
| | 692 | continue; |
| 616 | } | 693 | } |
| 617 | if cache.insert_if_epoch( | 694 | let (tex, stats) = engine.evaluate(&job.comp, job.target, f as f64); |
| 618 | job_epoch, | 695 | if generation.load(Ordering::Acquire) != job_gen |
| 619 | key, | 696 | || cache_epoch.load(Ordering::Acquire) != job_epoch |
| 620 | RamFrame { | 697 | { |
| 621 | size: tex.size, | 698 | break 'prefetch; |
| 622 | rgba: Arc::new(rgba), | 699 | } |
| 623 | eval_ms: stats.eval_ms, | 700 | if let Ok(rgba) = engine.readback_rgba(&tex) { |
| 624 | }, | 701 | // Re-check generation immediately before inserting after readback completes. |
| 625 | ) { | 702 | if generation.load(Ordering::Acquire) != job_gen { |
| 626 | completed.fetch_add(1, Ordering::AcqRel); | 703 | break 'prefetch; |
| 627 | } else { | 704 | } |
| 628 | break; | 705 | if cache.insert_if_epoch( |
| | 706 | job_epoch, |
| | 707 | key, |
| | 708 | RamFrame { |
| | 709 | size: tex.size, |
| | 710 | rgba: Arc::new(rgba), |
| | 711 | eval_ms: stats.eval_ms, |
| | 712 | }, |
| | 713 | ) { |
| | 714 | completed.fetch_add(1, Ordering::AcqRel); |
| | 715 | } else { |
| | 716 | break 'prefetch; |
| | 717 | } |
| 629 | } | 718 | } |
| 630 | } | 719 | } |
| | 720 | |
| | 721 | chunk_start = chunk_end + 1; |
| 631 | } | 722 | } |
| 632 | busy.store(false, Ordering::Release); | 723 | busy.store(false, Ordering::Release); |
| 633 | } | 724 | } |
| ... | @@ -913,8 +1004,8 @@ Composition { | ... | @@ -913,8 +1004,8 @@ Composition { |
| 913 | host_engine.queue_arc(), | 1004 | host_engine.queue_arc(), |
| 914 | cache.clone(), | 1005 | cache.clone(), |
| 915 | ); | 1006 | ); |
| 916 | assert_eq!(prefetcher.vram_budget_mb, DEFAULT_PREFETCH_VRAM_MB); | 1007 | assert_eq!(prefetcher.vram_budget_mb, default_prefetch_vram_budget_mb()); |
| 917 | assert_eq!(prefetcher.media_budget_mb, DEFAULT_PREFETCH_MEDIA_MB); | 1008 | assert_eq!(prefetcher.media_budget_mb, default_prefetch_media_budget_mb()); |
| 918 | | 1009 | |
| 919 | prefetcher.request_lookahead(PrefetchJob { | 1010 | prefetcher.request_lookahead(PrefetchJob { |
| 920 | comp: comp.clone(), | 1011 | comp: comp.clone(), |
| ... | @@ -957,7 +1048,73 @@ Composition { | ... | @@ -957,7 +1048,73 @@ Composition { |
| 957 | | 1048 | |
| 958 | let explicit = RamFrameCache::with_budget_mb(32); | 1049 | let explicit = RamFrameCache::with_budget_mb(32); |
| 959 | assert_eq!(explicit.budget_mb(), 32); | 1050 | assert_eq!(explicit.budget_mb(), 32); |
| | 1051 | assert_eq!(explicit.budget_bytes(), 32 * 1024 * 1024); |
| 960 | explicit.set_budget_mb(16); | 1052 | explicit.set_budget_mb(16); |
| 961 | assert_eq!(explicit.budget_mb(), 16); | 1053 | assert_eq!(explicit.budget_mb(), 16); |
| | 1054 | assert_eq!(explicit.budget_bytes(), 16 * 1024 * 1024); |
| | 1055 | } |
| | 1056 | |
| | 1057 | #[test] |
| | 1058 | fn prefetch_vram_and_media_budget_scaling() { |
| | 1059 | const GIB: u64 = 1024 * 1024 * 1024; |
| | 1060 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(8 * GIB), DEFAULT_PREFETCH_VRAM_MB); |
| | 1061 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(8 * GIB), DEFAULT_PREFETCH_MEDIA_MB); |
| | 1062 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(16 * GIB), 256); |
| | 1063 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(16 * GIB), 128); |
| | 1064 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(32 * GIB), 512); |
| | 1065 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(32 * GIB), 512); |
| | 1066 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(64 * GIB), 1024); |
| | 1067 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(64 * GIB), 1024); |
| | 1068 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(128 * GIB), 2048); |
| | 1069 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(128 * GIB), 2048); |
| | 1070 | assert_eq!(prefetch_vram_budget_mb_for_total_bytes(256 * GIB), 2048); |
| | 1071 | assert_eq!(prefetch_media_budget_mb_for_total_bytes(256 * GIB), 2048); |
| | 1072 | assert_eq!( |
| | 1073 | default_prefetch_vram_budget_mb(), |
| | 1074 | prefetch_vram_budget_mb_for_total_bytes(system_memory_bytes()) |
| | 1075 | ); |
| | 1076 | assert_eq!( |
| | 1077 | default_prefetch_media_budget_mb(), |
| | 1078 | prefetch_media_budget_mb_for_total_bytes(system_memory_bytes()) |
| | 1079 | ); |
| | 1080 | } |
| | 1081 | |
| | 1082 | #[test] |
| | 1083 | fn change_generation_tracks_mutations_but_not_reads() { |
| | 1084 | let cache = RamFrameCache::new(2); |
| | 1085 | let key = |rev, frame| RamFrameKey { |
| | 1086 | target: NodeId(0), |
| | 1087 | revision: rev, |
| | 1088 | frame, |
| | 1089 | proxy_scale: 1, |
| | 1090 | hiq: true, |
| | 1091 | use_proxy_clips: false, |
| | 1092 | }; |
| | 1093 | let g0 = cache.change_generation(); |
| | 1094 | cache.insert(key(1, 0), dummy_frame(512, 512)); |
| | 1095 | let g1 = cache.change_generation(); |
| | 1096 | assert!(g1 > g0); |
| | 1097 | |
| | 1098 | // `get` hits update LRU tick only and must NOT bump change_generation. |
| | 1099 | assert!(cache.get(key(1, 0)).is_some()); |
| | 1100 | assert_eq!(cache.change_generation(), g1); |
| | 1101 | |
| | 1102 | // Eviction via set_budget_mb bumps change_generation. |
| | 1103 | cache.insert(key(1, 1), dummy_frame(512, 512)); |
| | 1104 | let g2 = cache.change_generation(); |
| | 1105 | assert!(g2 > g1); |
| | 1106 | cache.set_budget_mb(1); |
| | 1107 | let g3 = cache.change_generation(); |
| | 1108 | assert!(g3 > g2); |
| | 1109 | |
| | 1110 | // Invalidation only bumps when stale entries are actually removed. |
| | 1111 | assert_eq!(cache.invalidate_other_revisions(1), 0); |
| | 1112 | assert_eq!(cache.change_generation(), g3); |
| | 1113 | assert!(cache.invalidate_other_revisions(2) > 0); |
| | 1114 | let g4 = cache.change_generation(); |
| | 1115 | assert!(g4 > g3); |
| | 1116 | |
| | 1117 | cache.clear(); |
| | 1118 | assert!(cache.change_generation() > g4); |
| 962 | } | 1119 | } |
| 963 | } | 1120 | } |