railties/test_parser: Cache prism work done in definition_for #53936
+18
−15
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 applies to the Prism implementation of
definition_for
.Motivation
In files with lots of tests,
definition_for
is called repeatedly and performs a lot of work that really adds up. The work is reusable across many tests, so let's do it.Details
definition_for
is used to extract the line numbers of a test, which is relevant when filtering tests to a line number or range, eg my_test.rb:1-2.The filter itself operates by, for each test, comparing the lines the test spans against the filter. In the existing code, this will call
Prism.parse_file
and then walk the parse result until the matching definition/call is found.For files with a large number of tests, this is fairly onerous and can dominate the test runtime. Fortunately, we can parse the file once and cache the starts and ends of every def/call node.
This brings the
definition_for
runtime down from seconds to low milliseconds.We also see a reduction in gc time that is probably attributable to not repeatedly parsing the file.
For a test run in an 240 test file that went from 11s of
definition_for
to 20ms, gc went from 4.5 to 2.2 seconds.Finally, for any user that has written a test reporter or otherwise hooked into the filters, this should provide speedup as well.
Checklist
[Fix #issue-number]