gallery/vendor/i-slint-core/unsafe_single_threaded.rs
Greg Pomerantz 2f4768f354 Fix 120Hz scroll jank end-to-end: render-ahead present + full-cache eviction off the UI thread
Three root causes, all fixed and verified on the Pixel 9 Pro:

1. Present phase (backend + renderer): VSYNC_LOCKED_RENDER defers the
   render to the vsync wake; RENDER_AHEAD presents the previous frame at
   the start of render() (phase = wake + ~1.1 ms) and holds the new
   frame one slot. Crash fix: a held wgpu frame must be PRESENTED, never
   merely dropped, before the next acquire (AlreadyAcquired panic at
   launch otherwise); discard_pending_frame presents too.

2. Full-res prefetch churn (src/lib.rs): the 8-slot full cache cycled
   through a ~20-cell viewport forever (137 decodes/20 s) and each
   eviction free()'d a 50 MB Vec on the UI thread (20-52 ms CPU spikes,
   ~7/s = the residual SLOWEV NonMain jank). Fix: 60 s tombstone
   cooldown on evicted paths (idle prefetch skips, urgent tap bypasses)
   + off-thread reaper for the freed blobs (insert: 21 ms -> 4 us).

3. (earlier) Skia final flush + Mailbox present mode (vendor, M3).

Instrumentation (all behind --features perf): OTHR/INVK/INVK_TAG/FLCK
lines for cross-thread closure timing and push-site tagging.

Warm measured swipe, 8580-file grid: cadence median 8.33 ms (exact
120 Hz), p99 15.5 ms, 5/552 gaps >16.7 ms (0.9%, max 28.3 ms, zero
>33 ms); PRES phase median ~994 us, zero late frames. Docs updated
(docs/BUGS/scroll-jank-120hz.md, full scorecard + line reference).

Also: vendored i-slint-core + wgpu-hal (vkshim interposition patch),
vkshim profiling tooling, backend made a non-optional dep, debuggable
flag for simpleperf (REMOVE before shipping).
2026-10-30 02:49:23 -04:00

89 lines
2.9 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//! Unsafe module that is only enabled when the `std` feature is off.
//! It re-implements the thread_local macro with statics
#![allow(unsafe_code)]
#[macro_export]
macro_rules! SLINT__thread_local_inner {
($(#[$($meta:tt)*])* $vis:vis $ident:ident $ty:ty $block:block) => {
$(#[$($meta)*])*
$vis static $ident: $crate::unsafe_single_threaded::FakeThreadStorage<$ty> = {
fn init() -> $ty $block
$crate::unsafe_single_threaded::FakeThreadStorage::new(init)
};
};
}
#[macro_export]
macro_rules! thread_local_ {
// Taken from stdlib!
// empty (base case for the recursion)
() => {};
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block; $($rest:tt)*) => (
$crate::SLINT__thread_local_inner!($(#[$attr])* $vis $name $t $init);
$crate::thread_local!($($rest)*);
);
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block) => (
$crate::SLINT__thread_local_inner!($(#[$attr])* $vis $name $t $init);
);
// process multiple declarations
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
$crate::SLINT__thread_local_inner!($(#[$attr])* $vis $name $t { $init });
$crate::thread_local!($($rest)*);
);
// handle a single declaration
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
$crate::SLINT__thread_local_inner!($(#[$attr])* $vis $name $t { $init });
);
}
pub struct FakeThreadStorage<T, F = fn() -> T>(once_cell::unsync::OnceCell<T>, F);
impl<T, F> FakeThreadStorage<T, F> {
pub const fn new(f: F) -> Self {
Self(once_cell::unsync::OnceCell::new(), f)
}
}
impl<T> FakeThreadStorage<T> {
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
f(self.0.get_or_init(self.1))
}
#[allow(clippy::result_unit_err)]
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Result<R, ()> {
Ok(self.with(f))
}
}
// Safety: the unsafe_single_threaded feature means we will only be called from a single thread
unsafe impl<T, F> Send for FakeThreadStorage<T, F> {}
unsafe impl<T, F> Sync for FakeThreadStorage<T, F> {}
pub use thread_local_ as thread_local;
pub struct OnceCell<T>(once_cell::unsync::OnceCell<T>);
impl<T> Default for OnceCell<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> OnceCell<T> {
pub const fn new() -> Self {
Self(once_cell::unsync::OnceCell::new())
}
pub fn get(&self) -> Option<&T> {
self.0.get()
}
pub fn set(&self, value: T) -> Result<(), T> {
self.0.set(value)
}
}
// Safety: the unsafe_single_threaded feature means we will only be called from a single thread
unsafe impl<T> Send for OnceCell<T> {}
unsafe impl<T> Sync for OnceCell<T> {}