Repository navigation
perf: read each test file once when detecting browser tests - #305
Open
blakedeckard wants to merge 1 commit into
Open
blakedeckard wants to merge 1 commit into
blakedeckard wants to merge 1 commit into
Conversation
BrowserTestIdentifier::usesFunction() runs twice for every test as it is registered, once for "visit" and once for "debug". Each call re-read the whole test file with file() and tokenized the test's source, so a file with N tests was read 2N times, even in suites that never use the browser. - Cache the lines of the test file currently being registered. Tests are registered file by file, so one file is enough and memory stays at one file's worth. - Skip tokenizing when the test's source does not contain the function name at all. The token check below already requires that name to appear (case-insensitively), so this can't change the result. The search is done as UTF-8 so it doesn't depend on mb_internal_encoding(). The class is no longer readonly, since readonly classes can't declare static properties. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What:
Description:
BrowserTestIdentifier::usesFunction()runs twice for every test as it is registered: once forvisitand once fordebug. Each call re-reads the whole test file withfile()and tokenizes the test's source. A file with N tests is read 2N times. This happens in every project that has the plugin installed, even ones with no browser tests.This PR:
mb_stripos(..., encoding: 'UTF-8'), so the result doesn't depend onmb_internal_encoding(). The token check that follows already requires that name to appear, compared case-insensitively, so detection results can't change.The class is no longer
readonly, because readonly classes can't declare static properties.Benchmark:
usesFunction()forvisitanddebugon 6,000 test closures across 60 files, with no browser tests, on PHP 8.4. The time is three runs each. Memory was measured withmemory_get_usage()andmemory_get_peak_usage()around the loop; the test source totals about 590 KB.The saving grows with longer test files, because the old code re-read the whole file for each test.
Related:
#238 changes a different line in the same method (it accepts
(beforevisit). This PR applies cleanly on top of it, and the two can merge in either order.