⚡️ Speed up function _sphinx_type by 1,262%#151
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization replaces an expensive O(n) lookup pattern with O(1) dictionary lookups by precomputing a reverse mapping.
**Key optimization:** The original code uses `obj._enum in enums.__dict__.values()` which iterates through all enum values for every call, followed by another O(n) loop to find the matching name. This creates O(n²) behavior when called repeatedly.
**What changed:**
- **Precomputed reverse mapping**: `_enum_id_to_name = {id(value): name for name, value in enums.__dict__.items()}` creates a one-time mapping from object IDs to names
- **Fast set lookup**: `_enum_ids = set(_enum_id_to_name.keys())` enables O(1) membership testing
- **Replaced expensive operations**: `obj._enum in enums.__dict__.values()` becomes `id(obj._enum) in _enum_ids` (O(1) instead of O(n))
- **Direct name retrieval**: `_enum_id_to_name[enum_id]` replaces the O(n) loop that searched for the matching name
**Why it's faster:** The line profiler shows the original `if obj._enum in enums.__dict__.values()` took 102ms (91.8% of total time). The optimized version reduces this to just 0.77ms (7.1% of total time) - a **13x speedup** on that single line.
**Performance characteristics:** The optimization is most effective for:
- **Repeated calls** with known enums (1300%+ speedups in bulk tests)
- **Mixed workloads** with both known/unknown enums (1300%+ speedups)
- **Any scenario** where the same enum types are processed multiple times
The 1262% overall speedup demonstrates this is particularly valuable for documentation generation or validation workflows that process many enum properties.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 1,262% (12.62x) speedup for
_sphinx_typeinsrc/bokeh/core/property/enum.py⏱️ Runtime :
22.3 milliseconds→1.64 milliseconds(best of112runs)📝 Explanation and details
The optimization replaces an expensive O(n) lookup pattern with O(1) dictionary lookups by precomputing a reverse mapping.
Key optimization: The original code uses
obj._enum in enums.__dict__.values()which iterates through all enum values for every call, followed by another O(n) loop to find the matching name. This creates O(n²) behavior when called repeatedly.What changed:
_enum_id_to_name = {id(value): name for name, value in enums.__dict__.items()}creates a one-time mapping from object IDs to names_enum_ids = set(_enum_id_to_name.keys())enables O(1) membership testingobj._enum in enums.__dict__.values()becomesid(obj._enum) in _enum_ids(O(1) instead of O(n))_enum_id_to_name[enum_id]replaces the O(n) loop that searched for the matching nameWhy it's faster: The line profiler shows the original
if obj._enum in enums.__dict__.values()took 102ms (91.8% of total time). The optimized version reduces this to just 0.77ms (7.1% of total time) - a 13x speedup on that single line.Performance characteristics: The optimization is most effective for:
The 1262% overall speedup demonstrates this is particularly valuable for documentation generation or validation workflows that process many enum properties.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_sphinx_type-mhwsoksyand push.