⚡️ Speed up method QdrantConfig.check_host_port_or_path by 13%
#16
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.
📄 13% (0.13x) speedup for
QdrantConfig.check_host_port_or_pathinmem0/configs/vector_stores/qdrant.py⏱️ Runtime :
20.7 microseconds→18.4 microseconds(best of35runs)📝 Explanation and details
The optimized code achieves a 12% speedup by replacing tuple unpacking with individual variable assignments and simplifying the conditional logic.
Key optimizations:
Eliminated tuple unpacking: Changed from
host, port, path, url, api_key = (values.get("host"), ...)to individual assignments likehost = values.get("host"). This avoids creating a temporary tuple object and the overhead of unpacking it.Streamlined conditional logic: Replaced the original
not path and not (host and port) and not (url and api_key)with explicit None checks usingis None. This reduces the number of boolean operations and makes the logic more direct.Reduced branching complexity: The single combined condition eliminates multiple branch evaluations that would occur with the original nested boolean expressions.
Why this is faster:
is Noneare faster than relying on truthiness evaluationImpact on workloads:
This optimization particularly benefits scenarios where the validator is called frequently during Qdrant configuration instantiation. Since this is a Pydantic model validator that runs during object creation, applications that create many QdrantConfig instances will see cumulative performance gains. The 12% improvement, while modest per call, can add up significantly in configuration-heavy workloads or when this validator is in initialization hot paths.
✅ Correctness verification report:
⏪ Replay Tests and Runtime
test_pytest_testsconfigstest_prompts_py_testsvector_storestest_weaviate_py_testsllmstest_deepseek_py_test__replay_test_0.py::test_mem0_configs_vector_stores_qdrant_QdrantConfig_check_host_port_or_pathtest_pytest_testsvector_storestest_opensearch_py_testsvector_storestest_upstash_vector_py_testsllmstest_l__replay_test_0.py::test_mem0_configs_vector_stores_qdrant_QdrantConfig_check_host_port_or_pathTo edit these changes
git checkout codeflash/optimize-QdrantConfig.check_host_port_or_path-mhl814t8and push.