-
Notifications
You must be signed in to change notification settings - Fork 480
Feat cache residual evaluator #2695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,9 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import math | ||
| import threading | ||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Callable | ||
| from collections.abc import Callable, Hashable | ||
| from functools import singledispatch | ||
| from typing import ( | ||
| Any, | ||
|
|
@@ -25,6 +26,9 @@ | |
| TypeVar, | ||
| ) | ||
|
|
||
| from cachetools import LRUCache, cached | ||
| from cachetools.keys import hashkey | ||
|
|
||
| from pyiceberg.conversions import from_bytes | ||
| from pyiceberg.expressions import ( | ||
| AlwaysFalse, | ||
|
|
@@ -1970,6 +1974,20 @@ def residual_for(self, partition_data: Record) -> BooleanExpression: | |
| return self.expr | ||
|
|
||
|
|
||
| _DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE = 128 | ||
|
|
||
|
|
||
| def _residual_evaluator_cache_key( | ||
| spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not pass in |
||
| ) -> tuple[Hashable, ...]: | ||
| return hashkey(spec.spec_id, repr(expr), case_sensitive, schema.schema_id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building the |
||
|
|
||
|
|
||
| @cached( | ||
| cache=LRUCache(maxsize=_DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE), | ||
| key=_residual_evaluator_cache_key, | ||
| lock=threading.RLock(), | ||
| ) | ||
| def residual_evaluator_of( | ||
| spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema | ||
| ) -> ResidualEvaluator: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 128? I think this is pretty high, and would probably go a bit lower (32?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably should have put this in my initial PR reasoning; my guiding star here was that this feature should lean toward performance safety rather than super-tight memory tuning.
Residual evaluators are on the hot path for pruning, so if we miss the cache we end up rebinding expressions — and that’s relatively expensive in Python. A cache size of 128 lines up with common LRU defaults, and in practice it helps cut down query time and unnecessary I/O.
In my own experience, PyIceberg usually runs on instances with plenty of RAM (multiple GBs), so using a bit more memory to get more predictable performance feels like a good trade-off. I’ll also acknowledge that this comes from my experience, so there may be some bias there — but I think it’s a reasonable default for most real-world workloads. I'm happy to adjust if you feel strongly, but maybe we go with 64?