perf: memoise DataSerializer.read_file results #543
Merged
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.
Description
This applies the
functools.lru_cache
memoising decorator to the low levelamber.DataSerializer.read_file
method, which dramatically improves performance by avoiding re-reading a single snapshot file dozens of times, thus eliminating the quadratic behaviour observed in #539 in the common case of read-only interaction with amber snapshots.For the test in #539, with a single snapshot test parametrised with
SIZE
instances (note that doubling the size now doubles the time):For our real-world test suite, with 453 snapshots across 25 files:
This has to be on this
DataSerializer
class method, rather than the higher level methods inAmberSnapshotExtension
because the methods there are instance methods, with aself
argument, and it's called on different instances (and thus end up as differentlru_cache
entries).This optimisation is limited to only reading the amber file types, but I think images (and other single-file snapshots) will not have the same quadratic issues, and writing is arguably much less common than reading.
Related Issues
Checklist
Additional Comments
No additional comments.