Repository navigation
Skip unused work when the scanner extracts candidates - #20764
Open
RobinMalfait wants to merge 1 commit into
Open
RobinMalfait wants to merge 1 commit into
RobinMalfait wants to merge 1 commit into
Conversation
`scan()` called `extract_candidates()` and dropped its result. That result was a sorted `Vec<String>` of the new candidates, so each full scan cloned and sorted the new candidates for nothing, before it cloned and sorted all candidates again for its own result. `extract_candidates()` also removed the known candidates from the new ones with a loop over all known candidates. In watch mode, `scan_content()` runs for each change with a large set of known candidates, so this did one hash lookup per known candidate instead of one per extracted candidate. Now `extract_candidates()` only extracts. `scan()` adds the result to the known candidates directly, and `scan_content()` keeps the extracted candidates that are not known yet with `retain`, then sorts and returns them as before. The results of both methods stay the same: the known candidates are a set, so `extend` gives the same set as the old remove-then-extend, and `retain` keeps exactly the candidates that the old loop kept.
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 removes work that the Oxide
Scannerdid for nothing when it extracted candidates.scan()calledextract_candidates()and dropped its result. That result was a sortedVec<String>of the new candidates, so each full scan cloned and sorted the new candidates once for nothing. Thenscan()cloned and sorted all candidates again for its own result.extract_candidates()also removed the known candidates from the new ones with a loop over all known candidates:In watch mode,
scan_content()(scanFilesin the CLI) runs on each change while the scanner already knows thousands of candidates. That loop did one hash lookup per known candidate instead of one per extracted candidate.Now:
extract_candidates()only extracts candidates and CSS variables, and returns the set.scan()adds that set toself.candidatesdirectly. Nothing is cloned or sorted until it builds its result, as before.scan_content()keeps the extracted candidates that are not known yet withretain, adds them toself.candidates, and returns them sorted, as before.Why the cache keeps the same candidates
self.candidatesis anFxHashSet<String>:scan(), the old code removed the known candidates from the new set, then extended the known set with what was left.extendon a set ignores the duplicates anyway, so extending with the full new set gives the same set.scan_content(),retain(|c| !self.candidates.contains(c))keeps exactly the candidates that the old loop kept: those that are not inself.candidates. The returned list and the updated cache are the same.A new test checks that
scan_content()afterscan()returns only the new candidates, sorted, returns nothing for known candidates, and thatscan()still returns all of them.Test plan
cargo test -p tailwindcss-oxidepasses.I measured release builds of the napi module, before and after, on the Tailwind CSS website (13,647 candidates), with median times:
scan()scanFiles()with one changed file (CLI watch mode)scan()after one changed file (Vite/PostCSS rebuild)The gain is small, but no case gets slower, and the code is simpler.