lgui-core 0.2.0

Platform-neutral runtime and UI primitives for LGUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::{
    sync::{
        atomic::{AtomicU64, AtomicUsize, Ordering},
        Arc, Mutex,
    },
    time::{Duration, Instant},
};

use super::registry::RegistryState;
use super::{
    CacheDomain, CacheRegistration, CacheScope, CacheUsage, DomainInstanceId, DomainRegistration,
    DomainSnapshot, MemoryAction, MemoryEvent, MemoryOptions, MemorySnapshot, TrimReason,
    TrimRequest, TrimSnapshot,
};

#[cfg(feature = "persistent-cache")]
use super::PersistentCacheStore;

const FRAME_BUDGET_CHECK_INTERVAL: Duration = Duration::from_millis(250);

#[derive(Clone)]
pub struct MemoryGovernor {
    inner: Arc<MemoryGovernorInner>,
}

struct MemoryGovernorInner {
    options: Mutex<MemoryOptions>,
    registry: Arc<Mutex<RegistryState>>,
    epoch: AtomicU64,
    transient_reserved: Arc<AtomicUsize>,
    large_tasks_in_flight: Arc<AtomicUsize>,
    last_trim: Mutex<Option<TrimSnapshot>>,
    last_frame_budget_check: Mutex<Option<Instant>>,
    #[cfg(feature = "persistent-cache")]
    persistent: Option<Arc<dyn PersistentCacheStore>>,
}

impl MemoryGovernor {
    pub fn new(options: MemoryOptions) -> Self {
        Self::with_store(
            options,
            #[cfg(feature = "persistent-cache")]
            None,
        )
    }

    pub(crate) fn with_store(
        options: MemoryOptions,
        #[cfg(feature = "persistent-cache")] persistent: Option<Arc<dyn PersistentCacheStore>>,
    ) -> Self {
        options
            .validate()
            .expect("invalid application memory policy");
        Self {
            inner: Arc::new(MemoryGovernorInner {
                options: Mutex::new(options),
                registry: Arc::new(Mutex::new(RegistryState::default())),
                epoch: AtomicU64::new(1),
                transient_reserved: Arc::new(AtomicUsize::new(0)),
                large_tasks_in_flight: Arc::new(AtomicUsize::new(0)),
                last_trim: Mutex::new(None),
                last_frame_budget_check: Mutex::new(None),
                #[cfg(feature = "persistent-cache")]
                persistent,
            }),
        }
    }

    pub fn options(&self) -> MemoryOptions {
        *self.inner.options.lock().expect("memory options poisoned")
    }

    pub fn set_options(&self, options: MemoryOptions) {
        options
            .validate()
            .expect("invalid application memory policy");
        *self.inner.options.lock().expect("memory options poisoned") = options;
        self.bump_epoch();
        self.rebalance_budgets();
        self.enforce_budget(true);
        #[cfg(feature = "persistent-cache")]
        if let Some(store) = self.persistent_cache() {
            let _ = store.trim_to(options.budget.persistent_bytes);
        }
    }

    pub fn next_instance_id(&self) -> DomainInstanceId {
        let mut registry = self
            .inner
            .registry
            .lock()
            .expect("memory registry poisoned");
        let id = registry.next_instance;
        registry.next_instance = registry.next_instance.wrapping_add(1).max(1);
        DomainInstanceId(id)
    }

    pub fn register(&self, registration: DomainRegistration) -> CacheRegistration {
        let mut registry = self
            .inner
            .registry
            .lock()
            .expect("memory registry poisoned");
        let id = registry.next_registration;
        registry.next_registration = registry.next_registration.wrapping_add(1).max(1);
        registry.entries.insert(id, registration);
        drop(registry);
        self.rebalance_budgets();
        self.bump_epoch();
        let governor = self.clone();
        CacheRegistration::new(id, Arc::downgrade(&self.inner.registry), move || {
            governor.rebalance_budgets();
            governor.bump_epoch();
        })
    }

    pub fn snapshot(&self) -> MemorySnapshot {
        let epoch = self.inner.epoch.load(Ordering::Acquire);
        let entries = self.registered_entries();
        let mut usage = CacheUsage::default();
        let mut domains = Vec::with_capacity(entries.len());
        for (id, registration) in entries {
            let domain_usage = registration.adapter.usage();
            usage.add_assign(domain_usage);
            domains.push(DomainSnapshot {
                registration_id: id,
                domain: registration.domain,
                instance: registration.instance,
                owner: registration.owner,
                usage: domain_usage,
            });
        }
        let options = self.options();
        let cache_soft = options.budget.cache_soft_bytes;
        MemorySnapshot {
            epoch,
            options,
            transient_reserved_bytes: self.inner.transient_reserved.load(Ordering::Acquire),
            large_tasks_in_flight: self.inner.large_tasks_in_flight.load(Ordering::Acquire),
            pinned_overflow_bytes: usage.pinned_bytes.saturating_sub(cache_soft),
            usage,
            domains,
            last_trim: *self
                .inner
                .last_trim
                .lock()
                .expect("memory trim state poisoned"),
        }
    }

    pub fn trim(&self, reason: TrimReason, scope: CacheScope, target_bytes: usize) -> usize {
        let epoch = self.bump_epoch();
        let started = Instant::now();
        let entries = if scope == CacheScope::Persistent {
            Vec::new()
        } else {
            self.registered_entries()
        };
        let assignments = entries
            .iter()
            .filter(|(_, registration)| {
                registration.domain != CacheDomain::Persistent
                    && (scope == CacheScope::AllRebuildable
                        || registration.domain != CacheDomain::HostScene)
            })
            .map(|(_, registration)| {
                (
                    registration,
                    self.assigned_budget(registration.domain, &entries),
                )
            })
            .collect::<Vec<_>>();
        let assigned_total = assignments
            .iter()
            .map(|(_, assigned)| *assigned as u128)
            .sum::<u128>();
        let mut released = 0usize;
        for (registration, assigned) in assignments {
            let domain_target = if target_bytes == 0 {
                0
            } else if target_bytes == usize::MAX {
                assigned
            } else if assigned_total == 0 {
                0
            } else {
                let proportional =
                    (assigned as u128).saturating_mul(target_bytes as u128) / assigned_total;
                usize::try_from(proportional).unwrap_or(usize::MAX)
            };
            let result = registration.adapter.trim(TrimRequest {
                reason,
                scope,
                target_bytes: domain_target,
            });
            released = released.saturating_add(result.released_bytes());
        }
        #[cfg(feature = "persistent-cache")]
        if scope == CacheScope::Persistent {
            if let Some(store) = self.inner.persistent.as_ref() {
                let target = u64::try_from(target_bytes).unwrap_or(u64::MAX);
                if let (Ok(before), Ok(after)) = (store.stats(), store.trim_to(target)) {
                    released = released.saturating_add(
                        usize::try_from(before.bytes.saturating_sub(after.bytes))
                            .unwrap_or(usize::MAX),
                    );
                }
            }
        }
        let duration_micros = started.elapsed().as_micros().min(u64::MAX as u128) as u64;
        *self
            .inner
            .last_trim
            .lock()
            .expect("memory trim state poisoned") = Some(TrimSnapshot {
            reason,
            requested_at_epoch: epoch,
            released_bytes: released,
            duration_micros,
        });
        released
    }

    pub fn invalidate_domain(&self, domain: CacheDomain) -> usize {
        let entries = self.registered_entries();
        let mut released = 0usize;
        for (_, registration) in entries {
            if registration.domain == domain {
                released = released.saturating_add(
                    registration
                        .adapter
                        .trim(TrimRequest {
                            reason: TrimReason::Explicit,
                            scope: CacheScope::Memory,
                            target_bytes: 0,
                        })
                        .released_bytes(),
                );
            }
        }
        self.bump_epoch();
        released
    }

    pub fn notify(&self, event: MemoryEvent) {
        match self.options().event_action(event) {
            MemoryAction::None => {}
            MemoryAction::EnforceBudget => {
                if event == MemoryEvent::FrameCommitted {
                    if self.begin_frame_budget_check() {
                        self.finish_frame_budget_check();
                    }
                } else {
                    self.enforce_budget(true);
                }
            }
            MemoryAction::Trim {
                scope,
                target_bytes,
            } => {
                self.trim(event.trim_reason(), scope, target_bytes);
            }
        }
    }

    pub fn try_reserve(&self, bytes: usize) -> Option<MemoryReservation> {
        let hard = self.options().budget.transient_hard_bytes;
        let reserved = &self.inner.transient_reserved;
        if hard == usize::MAX {
            return Some(MemoryReservation {
                bytes,
                accounted_bytes: 0,
                reserved: Arc::clone(reserved),
            });
        }
        let mut current = reserved.load(Ordering::Acquire);
        loop {
            let next = current.checked_add(bytes)?;
            if next > hard {
                self.trim(TrimReason::HardBudget, CacheScope::Memory, 0);
                return None;
            }
            match reserved.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire)
            {
                Ok(_) => {
                    return Some(MemoryReservation {
                        bytes,
                        accounted_bytes: bytes,
                        reserved: Arc::clone(reserved),
                    })
                }
                Err(actual) => current = actual,
            }
        }
    }

    pub fn try_reserve_task(&self, bytes: usize) -> Option<MemoryTaskReservation> {
        let limit = self.options().budget.max_parallel_large_tasks;
        let in_flight = &self.inner.large_tasks_in_flight;
        let mut current = in_flight.load(Ordering::Acquire);
        loop {
            if current >= limit {
                return None;
            }
            match in_flight.compare_exchange_weak(
                current,
                current + 1,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => break,
                Err(actual) => current = actual,
            }
        }
        let Some(reservation) = self.try_reserve(bytes) else {
            in_flight.fetch_sub(1, Ordering::AcqRel);
            return None;
        };
        Some(MemoryTaskReservation {
            _reservation: reservation,
            in_flight: Arc::clone(in_flight),
        })
    }

    #[cfg(feature = "persistent-cache")]
    pub fn persistent_cache(&self) -> Option<Arc<dyn PersistentCacheStore>> {
        self.options()
            .persistent_cache_enabled
            .then(|| self.inner.persistent.as_ref().map(Arc::clone))
            .flatten()
    }

    #[cfg(feature = "persistent-cache")]
    pub fn persistent_cache_stats(
        &self,
    ) -> Result<super::PersistentCacheStats, super::CacheStoreError> {
        self.inner.persistent.as_ref().map_or_else(
            || Ok(super::PersistentCacheStats::default()),
            |store| store.stats(),
        )
    }

    #[cfg(feature = "persistent-cache")]
    pub fn clear_persistent_cache(
        &self,
        namespace: Option<&str>,
    ) -> Result<super::PersistentCacheStats, super::CacheStoreError> {
        if let Some(store) = self.inner.persistent.as_ref() {
            store.clear(namespace)?;
            self.bump_epoch();
            return store.stats();
        }
        Ok(super::PersistentCacheStats::default())
    }

    fn enforce_budget(&self, include_soft_limit: bool) {
        let options = self.options();
        let usage = self.budget_usage();
        let evictable = usage.managed_bytes.saturating_sub(usage.protected_bytes);
        let reason = if evictable > options.budget.cache_hard_bytes {
            Some(TrimReason::HardBudget)
        } else if include_soft_limit && evictable > options.budget.cache_soft_bytes {
            Some(TrimReason::SoftBudget)
        } else {
            None
        };
        if let Some(reason) = reason {
            self.trim(reason, CacheScope::Memory, options.budget.cache_soft_bytes);
        }
    }

    pub(crate) fn begin_frame_budget_check(&self) -> bool {
        if self.options().event_action(MemoryEvent::FrameCommitted) != MemoryAction::EnforceBudget {
            return false;
        }
        let now = Instant::now();
        let mut last = self
            .inner
            .last_frame_budget_check
            .lock()
            .expect("memory budget check state poisoned");
        if last.is_some_and(|previous| {
            now.saturating_duration_since(previous) < FRAME_BUDGET_CHECK_INTERVAL
        }) {
            return false;
        }
        *last = Some(now);
        true
    }

    pub(crate) fn finish_frame_budget_check(&self) {
        self.enforce_budget(false);
    }

    fn budget_usage(&self) -> BudgetUsage {
        let probes = self.registered_usage_probes();
        let mut result = BudgetUsage::default();
        for (domain, adapter) in probes {
            let usage = adapter.usage();
            let managed = usage.managed_bytes();
            result.managed_bytes = result.managed_bytes.saturating_add(managed);
            let protected = if domain == CacheDomain::HostScene {
                managed
            } else {
                usage.pinned_bytes.min(managed)
            };
            result.protected_bytes = result.protected_bytes.saturating_add(protected);
        }
        result
    }

    fn registered_usage_probes(&self) -> Vec<(CacheDomain, super::CacheAdapter)> {
        self.inner
            .registry
            .lock()
            .expect("memory registry poisoned")
            .entries
            .values()
            .map(|registration| (registration.domain, registration.adapter.clone()))
            .collect()
    }

    fn registered_entries(&self) -> Vec<(u64, DomainRegistration)> {
        self.inner
            .registry
            .lock()
            .expect("memory registry poisoned")
            .entries
            .iter()
            .map(|(id, registration)| (*id, registration.clone()))
            .collect()
    }

    fn rebalance_budgets(&self) {
        let entries = self.registered_entries();
        for (_, registration) in &entries {
            registration
                .adapter
                .set_budget(self.assigned_budget(registration.domain, &entries));
        }
    }

    fn assigned_budget(&self, domain: CacheDomain, entries: &[(u64, DomainRegistration)]) -> usize {
        let options = self.options();
        if domain == CacheDomain::Persistent {
            return options.budget.persistent_bytes.min(usize::MAX as u64) as usize;
        }
        let instances = entries
            .iter()
            .filter(|(_, registration)| registration.domain == domain)
            .count()
            .max(1);
        options.domain_budget(domain).saturating_div(instances)
    }

    fn bump_epoch(&self) -> u64 {
        self.inner.epoch.fetch_add(1, Ordering::AcqRel) + 1
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct BudgetUsage {
    managed_bytes: usize,
    protected_bytes: usize,
}

pub struct MemoryReservation {
    bytes: usize,
    accounted_bytes: usize,
    reserved: Arc<AtomicUsize>,
}

pub struct MemoryTaskReservation {
    _reservation: MemoryReservation,
    in_flight: Arc<AtomicUsize>,
}

impl MemoryTaskReservation {
    pub const fn bytes(&self) -> usize {
        self._reservation.bytes()
    }
}

impl Drop for MemoryTaskReservation {
    fn drop(&mut self) {
        self.in_flight.fetch_sub(1, Ordering::AcqRel);
    }
}

impl MemoryReservation {
    pub const fn bytes(&self) -> usize {
        self.bytes
    }
}

impl Drop for MemoryReservation {
    fn drop(&mut self) {
        self.reserved
            .fetch_sub(self.accounted_bytes, Ordering::AcqRel);
    }
}