⚡️ Speed up method _Reranker.voyageai by 9%
#100
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.
📄 9% (0.09x) speedup for
_Reranker.voyageaiinweaviate/collections/classes/config.py⏱️ Runtime :
1.88 milliseconds→1.72 milliseconds(best of19runs)📝 Explanation and details
The optimization adds a conditional check to avoid passing the
modelparameter when it'sNone, which provides an 8% speedup by reducing Python's keyword argument overhead.Key changes:
if model is None:check to call_RerankerVoyageAIConfig()without argumentsmodel=modelwhen the value is notNoneWhy this is faster:
In CPython, when you pass keyword arguments, Python creates a dictionary to hold the key-value pairs before passing them to the function. By avoiding the
model=modelkwarg whenmodelisNone(which matches the default anyway), we eliminate this dictionary creation overhead. This is a micro-optimization that becomes measurable when the function is called frequently.Performance benefits by test case:
test_default_model_is_none,test_model_none_explicit,test_voyageai_stress_with_noneThe optimization is most effective when the function is frequently called with the default
Nonevalue, which appears common based on the test cases focusing on default behavior.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_testcollectiontest_batch_py_testcollectiontest_classes_generative_py_testcollectiontest_confi__replay_test_0.py::test_weaviate_collections_classes_config__Reranker_voyageaiTo edit these changes
git checkout codeflash/optimize-_Reranker.voyageai-mh355k5land push.