Speed up snapshot_by_id with an index, ~4.5x faster inspect.partitions() - #3725
Open
MatheusFreitas25 wants to merge 2 commits into
Open
Speed up snapshot_by_id with an index, ~4.5x faster inspect.partitions()#3725MatheusFreitas25 wants to merge 2 commits into
MatheusFreitas25 wants to merge 2 commits into
Conversation
snapshot_by_id did a linear scan over the snapshots list, and is called once per manifest entry, making inspect.partitions() O(data_files x snapshots). Memoize an id-to-snapshot index instead. A cached_property is not usable here: model_copy carries __dict__ over, so a copy replacing the snapshots would inherit a stale index. The index is tied to the list it was built from and recomputed whenever snapshots is a different list.
Member
|
Could you confirm the CI failure? |
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.
Rationale for this change
snapshot_by_iddoes a linear scan overself.snapshots. It is called once permanifest entry by
InspectTable._process_manifest, soinspect.partitions()endsup being O(data_files × snapshots) — the cost grows with the snapshot count, which
means every commit makes all later reads slower.
On a table with ~317k data files and ~14.5k snapshots, reading the partitions of a
single manifest went from 1.70s to 0.38s (~4.5x) with this change. A lookup-only
benchmark of 20k calls shows where the time goes:
This replaces the scan with a memoized id-to-snapshot index.
A plain
cached_propertyis not usable here:model_copycarries__dict__overto the new instance, so a copy that replaces
snapshotswould inherit a staleindex and fail to find newly added snapshots. Since the commit path does exactly
that (
snapshot_by_idon the base metadata, thenmodel_copy(update={"snapshots": ...})), the index is tied to the list it was built from and recomputed wheneversnapshotsis a different list.This follows the same pattern already used for
Schema._lazy_id_to_field.Are these changes tested?
Three tests added to
tests/table/test_metadata.py, using the existingexample_table_metadata_v2fixtureAre there any user-facing changes?
No.
snapshot_by_idreturns the sameSnapshotinstance andNonefor unknownids, exactly as before. No signature or API change.