core(unused-css): clamp used bytes to avoid negative wasted bytes - #17131
Open
NickNojiri wants to merge 3 commits into
Open
core(unused-css): clamp used bytes to avoid negative wasted bytes#17131NickNojiri wants to merge 3 commits into
NickNojiri wants to merge 3 commits into
Conversation
computeUsage sums (endOffset - startOffset) across every used rule reported by CSS coverage, then derives wasted bytes from totalBytes - usedBytes. Coverage can report overlapping ranges (a nested rule lives inside its parent rule's range) or the same rule more than once, so the summed used bytes can exceed the stylesheet size, producing a negative wastedBytes/wastedPercent. A 0-length (empty inline) stylesheet also made percentUnused NaN. Clamp usedUncompressedBytes to the stylesheet size and guard the zero-length case so usage stays within [0, 100]%. Relates to the nested-rule range-overlap concern in GoogleChrome#14718. Add regression tests for overlapping ranges, duplicate used rules, and empty stylesheets.
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.
Summary
computeUsage in core/computed/unused-css.js sums each used rule's byte range with no clamping. CSS coverage can report overlapping ranges (a nested rule lives inside its parent's range) or the same rule twice, so summed used bytes can exceed the stylesheet size — producing negative wastedBytes/wastedPercent (verified: -4 for overlapping ranges, -8 for a duplicate rule) and NaN for empty stylesheets.
This is the overlap concern raised in #14718, complementing #17127: the smoke test proves current correctness; this makes the computation robust to a regression. The sibling unused-javascript-summary already avoids this via a per-byte bitmap.
Fix
Clamp usedUncompressedBytes to the stylesheet size and guard the zero-length case, keeping usage within [0, 100]%. Regression tests added for overlapping ranges, duplicate used rules, and empty stylesheets.
Testing