⚡️ Speed up function has_none_primitives_in_list by 67%
#463
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.
📄 67% (0.67x) speedup for
has_none_primitives_in_listinnvflare/tool/job/config/config_indexer.py⏱️ Runtime :
892 microseconds→533 microseconds(best of257runs)📝 Explanation and details
The optimization achieves a 67% speedup through two key changes:
1. Efficient Type Checking in
is_primitive():isinstance()calls (isinstance(value, int) or isinstance(value, float) or ...)isinstance(value, _PRIMITIVE_TYPES)call with a pre-defined tuple of typesisinstance()with a tuple of types is implemented in C and checks all types in one operation, avoiding multiple function calls and boolean evaluations2. Early Termination Loop in
has_none_primitives_in_list():any(not is_primitive(x) for x in values)which creates a generator and processes all elementsforloop with immediatereturn Trueon first non-primitive foundPerformance Benefits by Test Case:
Nonevalues see dramatic improvements (up to 329% faster) due to the optimized None check (value is None)The optimization is particularly effective for large lists where non-primitives appear early, and maintains consistent performance improvements across all primitive-only scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-has_none_primitives_in_list-mhe3ktm5and push.