Make metrics evaluation stateless#3665
Open
dossett wants to merge 2 commits into
Open
Conversation
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.
I used CODEX to analyze this problem and create this PR. I've reviewed the code and tests and stand by them. This summary is written completely by a human (me) other than very light copy editing by an LLM.
Currently
_InclusiveMetricsEvaluatorobjects are created and used on a per-file basis during scan planning. Much of this work doesn't vary by file though, the same filter is rewritten and bound every time._StrictMetricsEvaluatoris sometime reused sequentially. Neither can be concurrently reused safely because using them (by evaluating a file) changes their state.This PR makes them stateless and moves the mutability into Visitor instances that are used on a per file basis and then discarded. This is both safer and more efficient. The benchmark-timings (see below) are sub-second but translate to real wall clock gains in the production workloads I ran with this change.
CODEX-generated summary follows:
Metrics filtering currently combines expensive, input-independent expression binding with mutable per-file metrics state.
mainconstructs a new_InclusiveMetricsEvaluatorfor every file because concurrent calls on one instance could mix bounds and counts from different files. That avoids shared mutable state, but repeats the same preparation for every file.This separates preparation from evaluation. The inclusive and strict evaluators retain only the bound expression and options, while each call creates a private visitor containing that file's metrics. A single prepared inclusive evaluator can therefore be shared across every manifest task in one planning call. Strict evaluation uses the same pattern so snapshot updates and other consumers do not retain the same latent concurrency hazard.
Summary
schema.as_struct()construction from inclusive evaluation.Performance
I compared
mainand this branch using 1,000 files, a 102-column schema, and a 15-leaf predicate modeling five(event_day range AND region_id)branches. The serial manifest executor isolates planning work from thread scheduling. Values are median times from seven samples.mainEager preparation adds approximately 0.278 ms when one manifest has every file rejected before metrics evaluation (
0.024 msonmainversus0.302 mshere). Across 1,000 fully pruned manifests, the total difference is approximately 0.026 ms (0.636 msversus0.662 ms).Testing
pytest tests/expressions/test_evaluator.py tests/table/test_metrics_evaluator_planning.py tests/table/test_init.py tests/table/test_delete_file_index.py tests/table/test_snapshots.py tests/table/test_validate.py(241 passed)pytest tests/benchmark/test_metrics_evaluator_benchmark.py -m benchmark(4 passed)UV_NO_CONFIG=1 .venv/bin/prek run -a