fix: register setUp only once, eliminating O(N²) accumulation. ~2x faster tests that can scale in 1 file#175
Open
jpohhhh wants to merge 2 commits intoBetterment:mainfrom
Open
fix: register setUp only once, eliminating O(N²) accumulation. ~2x faster tests that can scale in 1 file#175jpohhhh wants to merge 2 commits intoBetterment:mainfrom
jpohhhh wants to merge 2 commits intoBetterment:mainfrom
Conversation
Each call to goldenTest() was calling goldenTestAdapter.setUp(_setUpGoldenTests), appending a new setUp callback to the current test group every time. With N golden tests, all N callbacks ran before each of the 2N variant runs, producing 2N² total setUp executions (e.g. 20,000 for 100 tests). The fix guards the registration with a boolean so it happens exactly once. Benchmarked against a real project (442 golden tests, 63 files, --concurrency=1): | Metric | Before (0.13.0) | After (fix) | Improvement | |----------------|-----------------|-------------|-------------| | Peak RAM | 18.1 GB | 3.2 GB | 5.6× | | Wall time | 212 s (3:33) | 127 s (2:07)| 1.7× | | Test execution | 3:08 | 2:02 | 1.5× | | User CPU | 180.8 s | 85.8 s | 2.1× | | System CPU | 46.1 s | 17.7 s | 2.6× | | Page reclaims | 12.7M | 4.9M | 2.6× | The system CPU drop (46s → 18s) reflects kernel time spent on memory management (page faults, VM pressure) servicing the pathological allocation pattern. The O(N²) rapid-fire async setUp calls were starving the GC of idle time, preventing it from finalizing native rendering resources (Pictures, Images, surfaces) between test runs. A regression test is included that intercepts setUpFn/testWidgetsFn to prove the accumulation and its quadratic effect.
There was a problem hiding this comment.
Pull request overview
Addresses a test registration performance/memory issue where goldenTest() repeatedly registered a setUp callback, causing quadratic setUp executions as golden tests scaled within a file.
Changes:
- Adds a guard in
goldenTest()intended to register_setUpGoldenTestsviasetUponly once. - Adds a regression test that intercepts
setUpFn/testWidgetsFnto detect callback accumulation and assert linear scaling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/src/golden_test.dart | Introduces a global “register setUp once” guard around goldenTestAdapter.setUp(_setUpGoldenTests). |
| test/src/golden_test_set_up_accumulation_test.dart | Adds a regression test that records setUp registrations and simulates runtime executions to validate non-quadratic behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+172
to
+175
| if (!_goldenTestSetUpRegistered) { | ||
| _goldenTestSetUpRegistered = true; | ||
| goldenTestAdapter.setUp(_setUpGoldenTests); | ||
| } |
|
|
||
| final Set<String> _loadedFontFamilies = {}; | ||
| bool _goldenTestSetUpRegistered = false; | ||
|
|
Comment on lines
+100
to
+106
| // FIXED: only one setUp callback should be registered regardless | ||
| // of how many goldenTest() calls are made. | ||
| expect( | ||
| registeredSetUpCallbacks, | ||
| hasLength(1), | ||
| reason: 'goldenTest should register setUp at most once', | ||
| ); |
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.
Story
Description
Each call to goldenTest() was calling goldenTestAdapter.setUp(_setUpGoldenTests), appending a new setUp callback to the current test group every time. With N golden tests, all N callbacks ran before each of the 2N variant runs, producing 2N² total setUp executions (e.g. 20,000 for 100 tests).
The fix guards the registration with a boolean so it happens exactly once.
Benchmarked against a real project (442 golden tests, 63 files, --concurrency=1):
The system CPU drop (46s → 18s) reflects kernel time spent on memory management (page faults, VM pressure) servicing the pathological allocation pattern.
A regression test is included that intercepts setUpFn/testWidgetsFn to prove the accumulation and its quadratic effect.
Type of Change