Skip to content

Skip unused work when the scanner extracts candidates - #20764

Open
RobinMalfait wants to merge 1 commit into
mainfrom
robin/open-pr-skip-unused-work-in-scanner-extract-candidates
Open

RobinMalfait wants to merge 1 commit into
mainfrom
robin/open-pr-skip-unused-work-in-scanner-extract-candidates

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

This PR removes work that the Oxide Scanner did for nothing when it extracted candidates.

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 once for nothing. Then scan() 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:

for existing in self.candidates.iter() {
    new_candidates.remove(existing);
}

In watch mode, scan_content() (scanFiles in 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 to self.candidates directly. Nothing is cloned or sorted until it builds its result, as before.
  • scan_content() keeps the extracted candidates that are not known yet with retain, adds them to self.candidates, and returns them sorted, as before.

Why the cache keeps the same candidates

self.candidates is an FxHashSet<String>:

  • In scan(), the old code removed the known candidates from the new set, then extended the known set with what was left. extend on a set ignores the duplicates anyway, so extending with the full new set gives the same set.
  • In scan_content(), retain(|c| !self.candidates.contains(c)) keeps exactly the candidates that the old loop kept: those that are not in self.candidates. The returned list and the updated cache are the same.

A new test checks that scan_content() after scan() returns only the new candidates, sorted, returns nothing for known candidates, and that scan() still returns all of them.

Test plan

  • cargo test -p tailwindcss-oxide passes.

  • I measured release builds of the napi module, before and after, on the Tailwind CSS website (13,647 candidates), with median times:

    Case Before After
    Full scan() 18.5 ms 17.4 ms
    scanFiles() with one changed file (CLI watch mode) ~0.5 ms ~0.28 ms
    scan() after one changed file (Vite/PostCSS rebuild) ~8.0 ms ~8.0 ms

    The gain is small, but no case gets slower, and the code is simpler.

`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.
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 25, 2026 14:14
@RobinMalfait RobinMalfait self-assigned this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant