From 65327545a72156252ed69b6c569d300c0d29ab4f Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 20:49:51 +0900 Subject: [PATCH 1/2] perf(binding): reuse a serialized sheet until it changes getCss and the CSS a transform returns serialized the whole sheet on every call. Between two extractions a build reads the same sheet several times (the transform's CSS, the shared base it writes next, each stylesheet module), and a source extracted again, as a prewarm and its transform do, changes nothing. Keep each serialization keyed by bucket, the base import and the effective atom mode, and forget them when the sheet is borrowed mutably, when a commit reports a change or freezes the atom plan, and when an atom-hoisting setting changes. A hit is exactly what create_css returned for the same state, so the output is byte-identical. Co-Authored-By: Claude Opus 5.5 --- ...hangepack_log_css_serialization_cache.json | 8 + Cargo.lock | 1 + libs/binding/Cargo.toml | 3 + libs/binding/src/css_cache.rs | 113 ++++++++++++ libs/binding/src/lib.rs | 44 ++++- libs/binding/src/output.rs | 20 +- libs/binding/tests/css_cache.rs | 172 ++++++++++++++++++ 7 files changed, 350 insertions(+), 11 deletions(-) create mode 100644 .changepacks/changepack_log_css_serialization_cache.json create mode 100644 libs/binding/src/css_cache.rs create mode 100644 libs/binding/tests/css_cache.rs diff --git a/.changepacks/changepack_log_css_serialization_cache.json b/.changepacks/changepack_log_css_serialization_cache.json new file mode 100644 index 000000000..ec393a34b --- /dev/null +++ b/.changepacks/changepack_log_css_serialization_cache.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-binding/package.json": "Patch", + "bindings/devup-ui-wasm/package.json": "Patch" + }, + "note": "Reuse a serialized stylesheet until the sheet or the atom-hoisting settings it reads change: getCss and the CSS a transform returns no longer serialize the same unchanged sheet again. The output is byte-identical.", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/Cargo.lock b/Cargo.lock index fc7ea0d27..e36f2f33a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,6 +598,7 @@ dependencies = [ "css", "extractor", "serde_json", + "serial_test", "sheet", ] diff --git a/libs/binding/Cargo.toml b/libs/binding/Cargo.toml index aa50e11f4..bf580965a 100644 --- a/libs/binding/Cargo.toml +++ b/libs/binding/Cargo.toml @@ -18,3 +18,6 @@ sheet = { path = "../sheet" } css = { path = "../css" } serde_json = "1.0.154" bimap = { version = "0.6.3", features = ["serde"] } + +[dev-dependencies] +serial_test = "4.0.1" diff --git a/libs/binding/src/css_cache.rs b/libs/binding/src/css_cache.rs new file mode 100644 index 000000000..61e005049 --- /dev/null +++ b/libs/binding/src/css_cache.rs @@ -0,0 +1,113 @@ +//! Serialized stylesheets, reused until the sheet or a setting they read changes. +//! +//! A build serializes the same sheet many times between two extractions: the +//! transform that changed it, the shared base the host writes next, and every +//! stylesheet module the bundler loads. Each entry is exactly what +//! `StyleSheet::create_css` returned for its key, so a hit is byte-identical. + +use sheet::StyleSheet; +use std::collections::HashMap; +use std::ops::{Deref, DerefMut}; +use std::sync::{LazyLock, LockResult, Mutex, MutexGuard, PoisonError}; + +/// What a serialization reads besides the sheet: the bucket, whether a file +/// stylesheet imports the shared base, and whether atom hoisting is in effect. +type Key = (Option, bool, bool); + +static CSS_CACHE: LazyLock>> = LazyLock::new(Mutex::default); + +fn cache() -> MutexGuard<'static, HashMap> { + CSS_CACHE.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// The key of a serialization; only a file stylesheet imports the shared base. +fn key(bucket: Option<&str>, import_main_css: bool) -> Key { + ( + bucket.map(str::to_string), + bucket.is_some() && import_main_css, + css::atom_hoist::is_atom_hoist(), + ) +} + +/// Serialize `bucket` (the shared base when `None`), reusing an unchanged result. +pub(crate) fn cached_css( + sheet: &StyleSheet, + bucket: Option<&str>, + import_main_css: bool, +) -> String { + cache() + .entry(key(bucket, import_main_css)) + .or_insert_with(|| sheet.create_css(bucket, import_main_css)) + .clone() +} + +/// Forget every serialization; called whenever the sheet or a setting it reads changes. +pub(crate) fn invalidate_css() { + cache().clear(); +} + +/// A stylesheet behind a mutex whose every mutable borrow forgets its serializations. +#[derive(Default)] +pub struct SheetCell(Mutex); + +/// A locked stylesheet; borrowing it mutably forgets its serializations. +pub struct SheetGuard<'a>(MutexGuard<'a, StyleSheet>); + +impl SheetCell { + /// Lock the stylesheet as `Mutex::lock` does. + /// + /// # Errors + /// The stylesheet, still usable, when a holder of the lock panicked. + pub fn lock(&self) -> LockResult> { + self.0 + .lock() + .map(SheetGuard) + .map_err(|poisoned| PoisonError::new(SheetGuard(poisoned.into_inner()))) + } +} + +impl SheetGuard<'_> { + /// Borrow the sheet mutably, keeping its serializations for the caller to forget. + pub(crate) fn untracked(&mut self) -> &mut StyleSheet { + &mut self.0 + } +} + +impl Deref for SheetGuard<'_> { + type Target = StyleSheet; + + fn deref(&self) -> &StyleSheet { + &self.0 + } +} + +impl DerefMut for SheetGuard<'_> { + fn deref_mut(&mut self) -> &mut StyleSheet { + invalidate_css(); + &mut self.0 + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn only_file_stylesheets_import_the_shared_base() { + assert_eq!(key(None, true), key(None, false)); + assert_ne!(key(Some("a.tsx"), true), key(Some("a.tsx"), false)); + } + + #[test] + fn a_poisoned_sheet_stays_usable() { + let cell = SheetCell::default(); + let _ = std::panic::catch_unwind(|| { + let _guard = cell.lock(); + panic!("poison the sheet"); + }); + let mut sheet = cell.lock().err().map(PoisonError::into_inner).unwrap(); + sheet.add_import("a.tsx", "b.css"); + assert_eq!(sheet.imports.len(), 1); + } +} diff --git a/libs/binding/src/lib.rs b/libs/binding/src/lib.rs index cc89b9a04..8d62f10d1 100644 --- a/libs/binding/src/lib.rs +++ b/libs/binding/src/lib.rs @@ -1,14 +1,14 @@ //! Host-neutral binding orchestration; JavaScript values belong to the host adapter. +mod css_cache; mod output; mod reservation; mod resolver_state; -pub use css::atom_hoist::set_atom_hoist; pub use css::debug::{is_debug, set_debug}; -pub use css::file_routes::set_file_routes as import_file_routes_internal; pub use css::slot_reservation::unreserved_slot_count as get_unreserved_class_slot_count; pub use css::{get_prefix, set_custom_shorthands as register_shorthands_internal, set_prefix}; +pub use css_cache::{SheetCell, SheetGuard}; pub use extractor::{has_devup_ui, has_devup_ui_with}; pub use output::{CssOptions, ExtractionMode, Output, SourceMapMode, code_extract}; pub use reservation::{ReservationInput, reserve_class_slots_internal}; @@ -16,13 +16,14 @@ pub use resolver_state::{ResolverRequest, ResolverState}; use css::class_map::{set_class_map, with_class_map}; use css::file_map::{set_canonical_map, set_file_map, with_canonical_map, with_file_map}; +use css_cache::{cached_css, invalidate_css}; use sheet::StyleSheet; -use std::collections::{BTreeMap, HashMap}; -use std::sync::{LazyLock, Mutex}; +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::sync::LazyLock; /// The single stylesheet shared by extraction, reservation and host test seams. -pub static GLOBAL_STYLE_SHEET: LazyLock> = - LazyLock::new(|| Mutex::new(StyleSheet::default())); +/// Every mutable borrow of it forgets the serializations of the sheet before. +pub static GLOBAL_STYLE_SHEET: LazyLock = LazyLock::new(SheetCell::default); /// Borrow the live stylesheet, recovering its contents after an unwind. pub fn with_style_sheet(f: F) -> R @@ -46,6 +47,30 @@ where f(&mut guard) } +/// Mutably borrow the live stylesheet for a change that forgets the +/// serializations itself, and only when it changed the sheet. +fn with_style_sheet_untracked(f: F) -> R +where + F: FnOnce(&mut StyleSheet) -> R, +{ + let mut guard = GLOBAL_STYLE_SHEET + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + f(guard.untracked()) +} + +/// Hoist atoms shared by `threshold` routes, or stop hoisting with `None`. +pub fn set_atom_hoist(threshold: Option) { + css::atom_hoist::set_atom_hoist(threshold); + invalidate_css(); +} + +/// Restore the routes each file reaches, which plan atom hoisting. +pub fn import_file_routes_internal(map: HashMap>) { + css::file_routes::set_file_routes(map); + invalidate_css(); +} + /// The text of an error the internal exports return fn error_text(error: impl std::fmt::Display) -> String { error.to_string() @@ -96,6 +121,7 @@ pub fn export_file_map_internal() -> Result { /// Restore canonical bucket identities decoded by a host adapter. pub fn import_canonical_map_internal(map: HashMap) { set_canonical_map(map); + invalidate_css(); } /// Export canonical bucket identities in path order. @@ -132,6 +158,7 @@ pub fn reset_state_internal(clear_resolver: impl FnOnce()) { clear_resolver(); css::test_state::reset_state_for_testing(); extractor::test_state::reset_caches_for_testing(); + invalidate_css(); } /// Register a decoded theme in the live stylesheet. @@ -149,13 +176,14 @@ pub fn get_css(file_num: Option, import_main_css: bool) -> String { with_style_sheet(|sheet| { if let Some(file_num) = file_num { with_file_map(|map| { - sheet.create_css( + cached_css( + sheet, map.get_by_right(&file_num).map(String::as_str), import_main_css, ) }) } else { - sheet.create_css(None, import_main_css) + cached_css(sheet, None, import_main_css) } }) } diff --git a/libs/binding/src/output.rs b/libs/binding/src/output.rs index 284113ca4..bdc3466da 100644 --- a/libs/binding/src/output.rs +++ b/libs/binding/src/output.rs @@ -4,7 +4,8 @@ use extractor::{ extract_without_source_map, }; -use crate::with_style_sheet_mut; +use crate::css_cache::{cached_css, invalidate_css}; +use crate::with_style_sheet_untracked; /// Whether extraction generates a source map. #[derive(Clone, Copy)] @@ -63,11 +64,23 @@ impl Output { let _atom_mode = css::atom_hoist::AtomModeGuard::new(options.single_css); let canonical_filename = canonical(&filename); let global = options.single_css || is_global(&filename); - with_style_sheet_mut(|sheet| { + with_style_sheet_untracked(|sheet| { + let planned = sheet.atom_plan.is_some(); // Global CSS belongs to the raw SOURCE file, never its collapsed bucket. let default_collected = sheet.rm_global_css(&filename, global); let (collected, updated_base_style) = sheet.update_styles_in_bucket(&output.styles, &canonical_filename, global); + // The commit reports every style, global rule, import and font face it + // added or removed, but not the atom plan its first commit freezes into + // the sheet. A source extracted again changes neither: what was + // serialized before still holds. + if collected + || updated_base_style + || default_collected + || planned != sheet.atom_plan.is_some() + { + invalidate_css(); + } Self { code: output.code, map: output.map, @@ -78,7 +91,8 @@ impl Output { if !collected && !default_collected { None } else { - Some(sheet.create_css( + Some(cached_css( + sheet, if global { None } else { diff --git a/libs/binding/tests/css_cache.rs b/libs/binding/tests/css_cache.rs new file mode 100644 index 000000000..71dff1ae7 --- /dev/null +++ b/libs/binding/tests/css_cache.rs @@ -0,0 +1,172 @@ +use std::collections::{HashMap, HashSet}; + +use devup_ui_binding_core::{ + ExtractionMode, GLOBAL_STYLE_SHEET, Output, SourceMapMode, code_extract, get_css, + import_canonical_map_internal, import_file_map_internal, import_file_routes_internal, + reset_state_internal, set_atom_hoist, with_style_sheet, +}; +use extractor::ExtractOption; +use serial_test::serial; + +fn extract(filename: &str, code: &str, single_css: bool) -> Result { + code_extract( + (filename, code), + ExtractOption { + package: "@devup-ui/react".to_string(), + css_dir: "df".to_string(), + single_css, + ..ExtractOption::default() + }, + ExtractionMode { + source_map: SourceMapMode::Skip, + resolver: None, + resolver_check: None, + import_main_css_in_css: false, + }, + ) +} + +#[test] +#[serial] +fn css_is_reused_until_an_extraction_changes_the_sheet() -> Result<(), String> { + // Given: a transform that added a style, and the base read once. + reset_state_internal(|| {}); + let red = extract( + "red.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + true, + )?; + let first = get_css(None, false); + + // When: the base is read again, then another transform adds a style. + let again = get_css(None, true); + extract( + "blue.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + true, + )?; + let changed = get_css(None, false); + + // Then: the unchanged sheet reads the same, the changed one holds both. + assert_eq!(red.css.as_deref(), Some(first.as_str())); + assert_eq!(again, first); + assert!(!first.contains("blue")); + assert!(changed.contains("red") && changed.contains("blue")); + Ok(()) +} + +#[test] +#[serial] +fn css_survives_a_source_extracted_again_unchanged() -> Result<(), String> { + // Given: a source extracted once, with a global rule, and its base read. + reset_state_internal(|| {}); + let source = "import { Box, globalCss } from '@devup-ui/react'; globalCss({ body: { m: 0 } }); export const view = ;"; + extract("page.tsx", source, true)?; + let first = get_css(None, false); + + // When: the same source is extracted again, as a prewarm and its transform do. + let again = extract("page.tsx", source, true)?; + + // Then: the base reads the same as from a fresh serialization. + assert_eq!(get_css(None, false), first); + assert!(again.updated_base_style); + assert!(first.contains("red")); + Ok(()) +} + +#[test] +#[serial] +fn css_is_forgotten_when_the_sheet_is_borrowed_mutably() { + // Given: the base of an empty sheet, read once. + reset_state_internal(|| {}); + let empty = get_css(None, false); + + // When: a host test seam edits the locked sheet directly. + GLOBAL_STYLE_SHEET + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .add_import("page.tsx", "theme.css"); + + // Then: the next read serializes the edited sheet. + let edited = get_css(None, false); + assert_ne!(edited, empty); + assert!(edited.contains("@import \"theme.css\";")); +} + +/// The file stylesheet and the base, as a fresh serialization writes them now. +fn fresh() -> (String, String) { + with_style_sheet(|sheet| { + ( + sheet.create_css(Some("shared.tsx"), false), + sheet.create_css(None, false), + ) + }) +} + +/// The file stylesheet and the base, as the cached reads return them now. +fn read() -> (String, String) { + (get_css(Some(0), false), get_css(None, false)) +} + +#[test] +#[serial] +fn css_follows_every_atom_hoisting_setting() -> Result<(), String> { + // Given: a source extracted before atom hoisting, read once. + reset_state_internal(|| {}); + import_file_map_internal(std::iter::once(("shared.tsx".to_string(), 0)).collect()); + extract( + "shared.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + false, + )?; + assert_eq!(read(), fresh()); + + // When: each setting the serialization reads changes after a read. + import_file_routes_internal(HashMap::from([( + "shared.tsx".to_string(), + HashSet::from([0, 1]), + )])); + let routed = (read(), fresh()); + import_canonical_map_internal(HashMap::new()); + let canonical = (read(), fresh()); + set_atom_hoist(Some(2)); + let hoisted = (read(), fresh()); + set_atom_hoist(None); + let plain = (read(), fresh()); + + // Then: every read is what a fresh serialization writes at that point. + assert_eq!(routed.0, routed.1); + assert_eq!(canonical.0, canonical.1); + assert_eq!(hoisted.0, hoisted.1); + assert_eq!(plain.0, plain.1); + reset_state_internal(|| {}); + Ok(()) +} + +#[test] +#[serial] +fn css_is_forgotten_when_a_commit_freezes_the_atom_plan() -> Result<(), String> { + // Given: atom hoisting over two routes, the empty sheet read once. + reset_state_internal(|| {}); + import_file_map_internal(std::iter::once(("shared.tsx".to_string(), 0)).collect()); + import_file_routes_internal(HashMap::from([( + "shared.tsx".to_string(), + HashSet::from([0, 1]), + )])); + set_atom_hoist(Some(2)); + read(); + + // When: a source without styles commits, freezing the plan into the sheet. + let output = extract( + "shared.tsx", + "import { Box } from '@devup-ui/react'; export const view = 1;", + false, + )?; + + // Then: nothing was collected, yet the read follows the frozen plan. + assert!(output.css.is_none()); + assert!(with_style_sheet(|sheet| sheet.atom_plan.is_some())); + assert_eq!(read(), fresh()); + reset_state_internal(|| {}); + Ok(()) +} From 4bb852e938422246f1de7242a7e7b576326b62c9 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 21:12:55 +0900 Subject: [PATCH 2/2] perf(binding): serialize a transform's CSS only when it is read Every transform that added a style serialized its bucket, the whole shared base in single-CSS mode, even though the Next.js loader and prewarm never read it and the Webpack loader reads it only when it writes it. Over a single-CSS build that is quadratic in the sheet. The output now holds the request and serializes on the first read. It still describes the sheet as its transform left it: the latest unread result is serialized before the sheet is borrowed mutably, before the next commit, and before an atom-hoisting setting changes, under the atom mode its transform committed in (AtomModeGuard::exact). A result released unread costs nothing. The Next.js loader and prewarm stop reading it, and the Webpack loader reads it only outside watch mode when it writes the file. Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_lazy_transform_css.json | 10 ++ bindings/devup-ui-binding/src/output.rs | 3 +- bindings/devup-ui-wasm/src/lib.rs | 4 +- .../devup-ui-wasm/src/reservation/tests.rs | 2 +- libs/binding/src/css_cache.rs | 1 + libs/binding/src/lazy_css.rs | 77 ++++++++++++ libs/binding/src/lib.rs | 41 ++++--- libs/binding/src/output.rs | 13 +- libs/binding/tests/css.rs | 2 +- libs/binding/tests/css_cache.rs | 2 +- libs/binding/tests/lazy_css.rs | 116 ++++++++++++++++++ libs/css/src/atom_hoist.rs | 30 ++++- .../next-plugin/src/__tests__/loader.test.ts | 36 ++++++ packages/next-plugin/src/coordinator.ts | 9 +- packages/next-plugin/src/loader.ts | 1 + packages/next-plugin/src/plugin.ts | 1 + packages/webpack-plugin/src/loader.ts | 7 +- 17 files changed, 322 insertions(+), 33 deletions(-) create mode 100644 .changepacks/changepack_log_lazy_transform_css.json create mode 100644 libs/binding/src/lazy_css.rs create mode 100644 libs/binding/tests/lazy_css.rs diff --git a/.changepacks/changepack_log_lazy_transform_css.json b/.changepacks/changepack_log_lazy_transform_css.json new file mode 100644 index 000000000..290df71ac --- /dev/null +++ b/.changepacks/changepack_log_lazy_transform_css.json @@ -0,0 +1,10 @@ +{ + "changes": { + "bindings/devup-ui-binding/package.json": "Patch", + "bindings/devup-ui-wasm/package.json": "Patch", + "packages/next-plugin/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch" + }, + "note": "Serialize the CSS a transform returns only when it is read: a read after later transforms or setting changes still returns the sheet as that transform left it. The Next.js loader and prewarm never read it, and the Webpack loader reads it only when it writes it, so single-CSS builds no longer serialize the whole sheet after every module. The output is byte-identical.", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/bindings/devup-ui-binding/src/output.rs b/bindings/devup-ui-binding/src/output.rs index 48a85f9ac..2c46a7c42 100644 --- a/bindings/devup-ui-binding/src/output.rs +++ b/bindings/devup-ui-binding/src/output.rs @@ -3,6 +3,7 @@ use napi::{Env, Result}; use napi_derive::napi; use crate::value::binding_error; +use devup_ui_binding_core::LazyCss; /// Materialized native output with an explicitly consumable payload. #[napi(js_name = "Output")] @@ -42,7 +43,7 @@ impl NativeOutput { /// Copy changed CSS, using undefined for absence. #[napi(getter)] pub fn css(&self, env: Env) -> Result> { - Ok(self.live(env)?.css.clone().into()) + Ok(self.live(env)?.css.as_ref().map(LazyCss::get).into()) } /// Copy the optional stylesheet filename. #[napi(getter, js_name = "cssFile")] diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index 6c2e7ed92..efbb58d9b 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -52,7 +52,7 @@ pub struct Output { map: Option, css_file: Option, updated_base_style: bool, - css: Option, + css: Option, dependencies: Vec, } @@ -130,7 +130,7 @@ impl Output { #[wasm_bindgen(getter, js_name = "css")] #[must_use] pub fn css(&self) -> Option { - self.css.clone() + self.css.as_ref().map(binding_core::LazyCss::get) } /// Files read through the module resolver, for the bundler to watch diff --git a/bindings/devup-ui-wasm/src/reservation/tests.rs b/bindings/devup-ui-wasm/src/reservation/tests.rs index 18a9c6b97..668abe192 100644 --- a/bindings/devup-ui-wasm/src/reservation/tests.rs +++ b/bindings/devup-ui-wasm/src/reservation/tests.rs @@ -375,7 +375,7 @@ fn resolved_token_keys_are_reserved_before_real_output() -> TestResult { ), &option, )?; - let css = output.css.ok_or("expected token CSS")?; + let css = output.css.ok_or("expected token CSS")?.get(); assert!(css.contains("width:8px")); assert!(css.contains("box-shadow:0 0 1px red")); assert!(!css.contains("$size")); diff --git a/libs/binding/src/css_cache.rs b/libs/binding/src/css_cache.rs index 61e005049..c6fdda54f 100644 --- a/libs/binding/src/css_cache.rs +++ b/libs/binding/src/css_cache.rs @@ -83,6 +83,7 @@ impl Deref for SheetGuard<'_> { impl DerefMut for SheetGuard<'_> { fn deref_mut(&mut self) -> &mut StyleSheet { + crate::lazy_css::settle(&self.0); invalidate_css(); &mut self.0 } diff --git a/libs/binding/src/lazy_css.rs b/libs/binding/src/lazy_css.rs new file mode 100644 index 000000000..a4ac7fdec --- /dev/null +++ b/libs/binding/src/lazy_css.rs @@ -0,0 +1,77 @@ +//! The CSS a transform returns, serialized only when the host reads it. +//! +//! Most hosts never read it, and serializing the shared base after every +//! transform is quadratic over a single-CSS build. The value is still the sheet +//! as that transform left it: the sheet, and every setting a serialization +//! reads, changes only after the one unread result has been serialized. + +use css::atom_hoist::{AtomModeGuard, is_suppressed}; +use sheet::StyleSheet; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak}; + +use crate::css_cache::cached_css; +use crate::with_style_sheet; + +struct Pending { + bucket: Option, + import_main_css: bool, + /// Whether the transform suppressed atom mode while it committed. + suppressed: bool, + value: Mutex>, +} + +/// The latest transform result, which alone can describe the current sheet. +static PENDING: Mutex> = Mutex::new(Weak::new()); + +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +impl Pending { + /// The value, serializing `sheet` under the transform's atom mode on the first call. + fn value(&self, sheet: &StyleSheet) -> String { + lock(&self.value) + .get_or_insert_with(|| { + let _mode = AtomModeGuard::exact(self.suppressed); + cached_css(sheet, self.bucket.as_deref(), self.import_main_css) + }) + .clone() + } +} + +/// CSS of a transform, serialized on its first read. +pub struct LazyCss(Arc); + +impl LazyCss { + /// Defer serializing `bucket` (the shared base when `None`) of the sheet as it stands. + pub(crate) fn defer(bucket: Option<&str>, import_main_css: bool) -> Self { + let pending = Arc::new(Pending { + bucket: bucket.map(str::to_string), + import_main_css, + suppressed: is_suppressed(), + value: Mutex::default(), + }); + *lock(&PENDING) = Arc::downgrade(&pending); + Self(pending) + } + + /// The CSS of the sheet as the transform left it. + #[must_use] + pub fn get(&self) -> String { + with_style_sheet(|sheet| self.0.value(sheet)) + } +} + +/// Serialize the unread result of the latest transform before `sheet`, or a +/// setting its serialization reads, changes. A dropped result costs nothing. +pub(crate) fn settle(sheet: &StyleSheet) { + let pending = std::mem::take(&mut *lock(&PENDING)).upgrade(); + if let Some(pending) = pending { + pending.value(sheet); + } +} + +/// Settle before a setting that serializations read changes. +pub(crate) fn settle_live() { + with_style_sheet(settle); +} diff --git a/libs/binding/src/lib.rs b/libs/binding/src/lib.rs index 8d62f10d1..16c91744f 100644 --- a/libs/binding/src/lib.rs +++ b/libs/binding/src/lib.rs @@ -1,6 +1,7 @@ //! Host-neutral binding orchestration; JavaScript values belong to the host adapter. mod css_cache; +mod lazy_css; mod output; mod reservation; mod resolver_state; @@ -10,6 +11,7 @@ pub use css::slot_reservation::unreserved_slot_count as get_unreserved_class_slo pub use css::{get_prefix, set_custom_shorthands as register_shorthands_internal, set_prefix}; pub use css_cache::{SheetCell, SheetGuard}; pub use extractor::{has_devup_ui, has_devup_ui_with}; +pub use lazy_css::LazyCss; pub use output::{CssOptions, ExtractionMode, Output, SourceMapMode, code_extract}; pub use reservation::{ReservationInput, reserve_class_slots_internal}; pub use resolver_state::{ResolverRequest, ResolverState}; @@ -47,8 +49,8 @@ where f(&mut guard) } -/// Mutably borrow the live stylesheet for a change that forgets the -/// serializations itself, and only when it changed the sheet. +/// Mutably borrow the live stylesheet for a change that settles the unread CSS +/// and forgets the serializations itself, and only when it changed the sheet. fn with_style_sheet_untracked(f: F) -> R where F: FnOnce(&mut StyleSheet) -> R, @@ -61,13 +63,19 @@ where /// Hoist atoms shared by `threshold` routes, or stop hoisting with `None`. pub fn set_atom_hoist(threshold: Option) { - css::atom_hoist::set_atom_hoist(threshold); - invalidate_css(); + change_setting(|| css::atom_hoist::set_atom_hoist(threshold)); } /// Restore the routes each file reaches, which plan atom hoisting. pub fn import_file_routes_internal(map: HashMap>) { - css::file_routes::set_file_routes(map); + change_setting(|| css::file_routes::set_file_routes(map)); +} + +/// Change a setting that serializations read: the unread CSS of the latest +/// transform is serialized before, and every serialization forgotten after. +fn change_setting(change: impl FnOnce()) { + lazy_css::settle_live(); + change(); invalidate_css(); } @@ -78,7 +86,7 @@ fn error_text(error: impl std::fmt::Display) -> String { /// Restore the sheet and its frozen atom plan together. pub fn import_sheet_internal(sheet: StyleSheet) { - css::atom_hoist::restore_atom_plan(sheet.atom_plan.clone()); + change_setting(|| css::atom_hoist::restore_atom_plan(sheet.atom_plan.clone())); with_style_sheet_mut(|global_sheet| *global_sheet = sheet); } @@ -120,8 +128,7 @@ pub fn export_file_map_internal() -> Result { /// Restore canonical bucket identities decoded by a host adapter. pub fn import_canonical_map_internal(map: HashMap) { - set_canonical_map(map); - invalidate_css(); + change_setting(|| set_canonical_map(map)); } /// Export canonical bucket identities in path order. @@ -141,14 +148,16 @@ pub fn seed_file_map(files: &[String]) { /// Clear production build state, retaining theme registries, shorthands and debug. /// The host adapter separately clears its resolver handle. pub fn reset_build_state_internal() { - css::slot_reservation::reset(); - css::class_map::reset_class_map(); - css::file_map::reset_file_map(); - css::file_map::reset_canonical_map(); - css::file_routes::set_file_routes(HashMap::new()); - css::atom_hoist::set_atom_hoist(None); - css::atom_hoist::restore_atom_plan(None); - css::set_prefix(None); + change_setting(|| { + css::slot_reservation::reset(); + css::class_map::reset_class_map(); + css::file_map::reset_file_map(); + css::file_map::reset_canonical_map(); + css::file_routes::set_file_routes(HashMap::new()); + css::atom_hoist::set_atom_hoist(None); + css::atom_hoist::restore_atom_plan(None); + css::set_prefix(None); + }); with_style_sheet_mut(|sheet| *sheet = StyleSheet::default()); } diff --git a/libs/binding/src/output.rs b/libs/binding/src/output.rs index bdc3466da..8533eb798 100644 --- a/libs/binding/src/output.rs +++ b/libs/binding/src/output.rs @@ -4,7 +4,8 @@ use extractor::{ extract_without_source_map, }; -use crate::css_cache::{cached_css, invalidate_css}; +use crate::css_cache::invalidate_css; +use crate::lazy_css::{LazyCss, settle}; use crate::with_style_sheet_untracked; /// Whether extraction generates a source map. @@ -47,14 +48,14 @@ pub struct Output { pub css_file: Option, /// Whether shared base CSS changed. pub updated_base_style: bool, - /// Changed CSS, absent when the stylesheet is unchanged. - pub css: Option, + /// Changed CSS, serialized on its first read; absent when the stylesheet is unchanged. + pub css: Option, /// Files read through the module resolver. pub dependencies: Vec, } impl Output { - /// Commit extracted styles to their existing bucket and materialize changed CSS. + /// Commit extracted styles to their existing bucket, deferring the changed CSS. pub fn new(output: ExtractOutput, filename: String, options: CssOptions) -> Self { // Use the bucket identity (single-importer collapse) so the sheet's CSS // naming + property bucket + emitted chunk match the canonical class names @@ -65,6 +66,7 @@ impl Output { let canonical_filename = canonical(&filename); let global = options.single_css || is_global(&filename); with_style_sheet_untracked(|sheet| { + settle(sheet); let planned = sheet.atom_plan.is_some(); // Global CSS belongs to the raw SOURCE file, never its collapsed bucket. let default_collected = sheet.rm_global_css(&filename, global); @@ -91,8 +93,7 @@ impl Output { if !collected && !default_collected { None } else { - Some(cached_css( - sheet, + Some(LazyCss::defer( if global { None } else { diff --git a/libs/binding/tests/css.rs b/libs/binding/tests/css.rs index fd9f2d9a2..54b7e88f5 100644 --- a/libs/binding/tests/css.rs +++ b/libs/binding/tests/css.rs @@ -42,6 +42,6 @@ fn numbered_css_selects_its_file_when_other_files_exist() -> Result<(), String> let selected = devup_ui_binding_core::get_css(Some(7), false); // Then: only the same file CSS returned by that transform is selected. - assert_eq!(Some(selected), red.css); + assert_eq!(Some(selected), red.css.map(|css| css.get())); Ok(()) } diff --git a/libs/binding/tests/css_cache.rs b/libs/binding/tests/css_cache.rs index 71dff1ae7..6d5a9f8c8 100644 --- a/libs/binding/tests/css_cache.rs +++ b/libs/binding/tests/css_cache.rs @@ -48,7 +48,7 @@ fn css_is_reused_until_an_extraction_changes_the_sheet() -> Result<(), String> { let changed = get_css(None, false); // Then: the unchanged sheet reads the same, the changed one holds both. - assert_eq!(red.css.as_deref(), Some(first.as_str())); + assert_eq!(red.css.map(|css| css.get()), Some(first.clone())); assert_eq!(again, first); assert!(!first.contains("blue")); assert!(changed.contains("red") && changed.contains("blue")); diff --git a/libs/binding/tests/lazy_css.rs b/libs/binding/tests/lazy_css.rs new file mode 100644 index 000000000..a7760b2ae --- /dev/null +++ b/libs/binding/tests/lazy_css.rs @@ -0,0 +1,116 @@ +use std::collections::{HashMap, HashSet}; + +use css::atom_hoist::AtomModeGuard; +use devup_ui_binding_core::{ + ExtractionMode, Output, SourceMapMode, code_extract, import_file_routes_internal, + reset_state_internal, set_atom_hoist, with_style_sheet, +}; +use extractor::ExtractOption; +use serial_test::serial; + +fn extract(filename: &str, code: &str, single_css: bool) -> Result { + code_extract( + (filename, code), + ExtractOption { + package: "@devup-ui/react".to_string(), + css_dir: "df".to_string(), + single_css, + ..ExtractOption::default() + }, + ExtractionMode { + source_map: SourceMapMode::Skip, + resolver: None, + resolver_check: None, + import_main_css_in_css: false, + }, + ) +} + +fn view(color: &str) -> String { + format!( + "import {{ Box }} from '@devup-ui/react'; export const view = ;" + ) +} + +/// The base as a fresh serialization writes it now, under the given atom mode. +fn fresh_base(single_css: bool) -> String { + let _mode = AtomModeGuard::new(single_css); + with_style_sheet(|sheet| sheet.create_css(None, false)) +} + +#[test] +#[serial] +fn css_read_at_once_is_the_sheet_the_transform_left() -> Result<(), String> { + // Given: a transform that added a style. + reset_state_internal(|| {}); + let output = extract("red.tsx", &view("red"), true)?; + let expected = fresh_base(true); + + // When: its CSS is read twice before anything else changes. + let css = output.css.ok_or("expected CSS")?; + + // Then: both reads are the serialization of that sheet. + assert_eq!(css.get(), expected); + assert_eq!(css.get(), expected); + Ok(()) +} + +#[test] +#[serial] +fn css_read_after_another_transform_is_the_sheet_its_transform_left() -> Result<(), String> { + // Given: a transform whose CSS is not read yet. + reset_state_internal(|| {}); + let red = extract("red.tsx", &view("red"), true)?; + let expected = fresh_base(true); + + // When: another transform changes the sheet before the read. + let blue = extract("blue.tsx", &view("blue"), true)?; + let red = red.css.ok_or("expected CSS")?.get(); + + // Then: the late read still holds only what the first transform saw. + assert_eq!(red, expected); + assert!(!red.contains("blue")); + assert!(blue.css.ok_or("expected CSS")?.get().contains("blue")); + Ok(()) +} + +#[test] +#[serial] +fn a_dropped_result_is_never_serialized() -> Result<(), String> { + // Given: a transform whose result is released unread. + reset_state_internal(|| {}); + drop(extract("red.tsx", &view("red"), true)?); + + // When: another transform changes the sheet. + let blue = extract("blue.tsx", &view("blue"), true)?; + + // Then: its own CSS holds both styles. + let css = blue.css.ok_or("expected CSS")?.get(); + assert!(css.contains("red") && css.contains("blue")); + Ok(()) +} + +#[test] +#[serial] +fn css_read_after_an_atom_setting_changes_keeps_the_transform_mode() -> Result<(), String> { + // Given: atom hoisting over two routes, and a per-file transform unread. + reset_state_internal(|| {}); + import_file_routes_internal(HashMap::from([( + "shared.tsx".to_string(), + HashSet::from([0, 1]), + )])); + set_atom_hoist(Some(2)); + let shared = extract("shared.tsx", &view("red"), false)?; + let expected = with_style_sheet(|sheet| sheet.create_css(Some("shared.tsx"), false)); + + // When: a single-CSS transform suppresses atom mode, then hoisting stops. + let single = extract("single.tsx", &view("blue"), true)?; + let single_expected = fresh_base(true); + set_atom_hoist(None); + + // Then: each late read is serialized in the mode of its own transform. + assert_eq!(shared.css.ok_or("expected CSS")?.get(), expected); + assert_eq!(single.css.ok_or("expected CSS")?.get(), single_expected); + reset_state_internal(|| {}); + Ok(()) +} diff --git a/libs/css/src/atom_hoist.rs b/libs/css/src/atom_hoist.rs index 940eb6e38..16195344f 100644 --- a/libs/css/src/atom_hoist.rs +++ b/libs/css/src/atom_hoist.rs @@ -22,7 +22,12 @@ pub struct AtomModeGuard { impl AtomModeGuard { pub fn new(single_css: bool) -> Self { - let previous = SUPPRESSED.with(|state| state.replace(state.get() || single_css)); + Self::exact(is_suppressed() || single_css) + } + + /// Suppress atom mode exactly when `suppressed`, whatever encloses the guard. + pub fn exact(suppressed: bool) -> Self { + let previous = SUPPRESSED.with(|state| state.replace(suppressed)); Self { previous, thread_bound: PhantomData, @@ -30,6 +35,11 @@ impl AtomModeGuard { } } +/// Whether a guard suppresses atom mode on this thread. +pub fn is_suppressed() -> bool { + SUPPRESSED.with(Cell::get) +} + impl Drop for AtomModeGuard { fn drop(&mut self) { SUPPRESSED.with(|state| state.set(self.previous)); @@ -158,6 +168,24 @@ mod tests { restore_atom_plan(None); } + #[test] + #[serial] + fn exact_suppression_ignores_the_enclosing_mode() { + // Given + set_atom_hoist(Some(2)); + let _outer = AtomModeGuard::new(true); + // When + { + let _inner = AtomModeGuard::exact(false); + // Then + assert!(!is_suppressed()); + assert!(is_atom_hoist()); + } + assert!(is_suppressed()); + assert!(!is_atom_hoist()); + set_atom_hoist(None); + } + #[test] #[serial] fn suppression_restores_after_error_and_unwind_without_freezing() { diff --git a/packages/next-plugin/src/__tests__/loader.test.ts b/packages/next-plugin/src/__tests__/loader.test.ts index 6eb97ec17..eb07eb42e 100644 --- a/packages/next-plugin/src/__tests__/loader.test.ts +++ b/packages/next-plugin/src/__tests__/loader.test.ts @@ -305,6 +305,42 @@ describe('devupUILoader', () => { }) }) + it('never reads the CSS the engine serializes on demand', async () => { + const asyncCallback = mock() + const readCss = mock(() => 'css') + const t = { + getOptions: () => ({ + package: 'package', + cssDir: 'cssFile', + watch: false, + singleCss: true, + defaultClassMap: {}, + defaultFileMap: {}, + defaultSheet: {}, + }), + async: mock().mockReturnValue(asyncCallback), + resourcePath: 'index.tsx', + addDependency: mock(), + } + codeExtractSpy.mockReturnValue({ + code: 'code', + get css() { + return readCss() + }, + free: mock(), + map: undefined, + cssFile: 'devup-ui.css', + updatedBaseStyle: true, + [Symbol.dispose]: mock(), + }) + devupUILoader.bind(asLoaderContext(t))(Buffer.from('code'), 'index.tsx') + + await waitFor(() => { + expect(asyncCallback).toHaveBeenCalledWith(null, 'code', null) + }) + expect(readCss).not.toHaveBeenCalled() + }) + it('extracts a file that reaches Devup UI only through a project module', async () => { const source = `import { Box } from './ui' export const a = ` diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 892f8287d..df8ad3613 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -83,14 +83,19 @@ interface ExtractOutputSnapshot extends Omit { css?: string } -/** Copy every WASM-backed getter once, then release its Rust allocation. */ +/** + * Copy every WASM-backed getter once, then release its Rust allocation. The + * engine serializes `css` only when it is read, so a caller that never uses it + * passes `withCss = false`. + */ export function takeExtractOutput( output: ReturnType, + withCss = true, ): ExtractOutputSnapshot { try { return { code: output.code, - css: output.css, + css: withCss ? output.css : undefined, cssFile: output.cssFile, map: output.map, updatedBaseStyle: output.updatedBaseStyle, diff --git a/packages/next-plugin/src/loader.ts b/packages/next-plugin/src/loader.ts index 917b43c87..d4d0b444c 100644 --- a/packages/next-plugin/src/loader.ts +++ b/packages/next-plugin/src/loader.ts @@ -286,6 +286,7 @@ const devupUILoader: RawLoaderDefinitionFunction = true, importAliases, ), + false, ) const { code, diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index aa92f663e..e33e7869a 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -428,6 +428,7 @@ export function DevupUI( true, importAliases as unknown as Record, ), + false, ) if (extractStartedAt !== undefined) { prewarmExtractMs += performance.now() - extractStartedAt diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index ee4db747f..3b7cbfa33 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -102,11 +102,14 @@ const devupUILoader: RawLoaderDefinitionFunction = 'code' | 'css' | 'map' | 'cssFile' | 'updatedBaseStyle' | 'dependencies' > try { + const cssFile = output.cssFile snapshot = { code: output.code, - css: output.css, + // The engine serializes the CSS only when it is read; watch mode + // never writes it + css: cssFile && !watch ? output.css : undefined, map: output.map, - cssFile: output.cssFile, + cssFile, updatedBaseStyle: output.updatedBaseStyle, dependencies: output.dependencies, }