Merged
Conversation
added 11 commits
April 7, 2026 00:01
…ectness on debug builds)
… vec allocation and sorting O(n log n)
Collaborator
|
This looks good to me. I wish there was a more automatic way to do content hash but I do think that this is better than allocating/hashing a string. |
Collaborator
|
@charlescgs do you mean for this to still be a draft? |
Contributor
Author
|
No, added is as a draft so can gather feedback |
Collaborator
|
Thanks for doing this! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR re-enables and fixes the style's cache, and adds few optimizations to reduce allocations and redundant work on the style hot path. Result is ~10-29% improvements on style benchmarks with no regressions.
Changes
1.
apply_muttakes&Styleinstead ofStyleEvery call site was cloning
Stylejust to pass ownership toapply_mut, which then only reads it. Changed the signature to eliminate ~15 unnecessaryStyleclones across the codebase2. Cached
StyleSelectorsbitmask onStyleselectors()previously traversed the entire style map to check which selector types were present — called repeatedly duringresolve_selectors. Added acached_selectors: StyleSelectorsfield incrementally updated when selectors are added viaapply_iter,set_selector,set_structural_selector,set_responsive_selector. Reduces selector presence checks from O(n) map traversal to O(1) bitmask read. (Correctness is being checkeddebug_assert!against the slow path in debug builds)3. Per-frame timestamp
Instant::now()was called per-view during the style pass. Captured once at frame start and shared viaWindowState::frame_start4.
FxHashMapReplaced HashMap with FxHashMap for style cache
5. Content-based
content_hash()implementationsThe default
StylePropValue::content_hash()usedformat!("{:?}", self)— allocating aStringper property per hash. Added manual implementations for all ~40 concrete types usingFxHasherwithto_bits()for floats andstd::mem::discriminant()for enums. Zero-allocation hashing throughout6. Eq via
PartialEqinstead of hash comparisonStyleMapValuecomparison was hashing both sides and comparing hashes. Changed to directa == bvia the existingPartialEqimpl7. XOR order-independent
content_hash()onStyleThe style-level
content_hash()collected entries into aVec, sorted by key pointer, then hashed sequentially with allocation. Replaced with XOR of independently-hashed entries: each entry gets its ownFxHasher, and results are XOR'd together. O(n) -zero allocation (order-independent by construction)8. Re-enabled style cache with fixes
compute_combined()outputs:combined_style,has_style_selectors,post_compute_combined_interactionRc<Style>wrapping with directStylestorage (the Rc indirection added overhead without benefit)window_width_bitsto cache key for responsive selectors with exact pixel thresholdsis_focus_withinto interaction state bits:first-child,:nth-child) or context-dependent values are excludedmap_ptr()(inner Rc pointer) instead of outerRc::as_ptr()9. Cache-hit path avoids
Style::clone()On cache hit, the previous implementation cloned the full
Styleto build the cache key, then discarded it. Restructured the hot path:StyleStackcaches thecontent_hashas a side-effect ofstyle()recomputationensure_clean()/content_hash()/is_cacheable()methods onStyleStackstyle_content_hash()/style_is_cacheable()forwarding onViewStateStyleCacheKey::new_from_hash()— noStylereference neededViewStatefields, bypassingcompute_combined()entirely10. More tests coverage
Benchmark results (vs current main)
@jrmoulton can you test this yourself?