⚡️ Speed up method _FilterToGRPC.convert by 42%
#111
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.
📄 42% (0.42x) speedup for
_FilterToGRPC.convertinweaviate/collections/filters.py⏱️ Runtime :
11.5 microseconds→8.09 microseconds(best of10runs)📝 Explanation and details
The optimization replaces
isinstance()calls with fastertype()comparisons in two key locations:Primary optimization in
convert()method:isinstance(weav_filter, _FilterValue)totype(weav_filter) is _FilterValueSecondary optimization in
__and_or_not_filter()method:isinstance()calls withtype(weav_filter) in (_FilterAnd, _FilterOr, _FilterNot)type()with tuple membership testing is more efficient than chainedisinstance()callsWhy this works:
isinstance()performs inheritance hierarchy traversal and multiple internal checkstype()returns the exact type directly without inheritance checkinginis optimized at the C level in Pythontype()is the optimal choiceThe optimization is particularly effective for filter-heavy workloads where type checking occurs frequently, as evidenced by the 42% overall speedup and consistent improvements across test cases ranging from 20-51% faster.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_FilterToGRPC.convert-mh3c1qhdand push.