⚡️ Speed up function create_format_table by 32%
#134
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.
📄 32% (0.32x) speedup for
create_format_tableinsrc/bokeh/models/formatters.py⏱️ Runtime :
4.00 milliseconds→3.04 milliseconds(best of143runs)📝 Explanation and details
The optimized code achieves a 31% speedup by eliminating redundant attribute lookups and streamlining string operations. Here's what changed:
Key Optimizations:
Precomputed Field Values: The original code called
getattr(primary, f),getattr(secondary, f), andgetattr(tertiary, f)multiple times - once during length calculation (lenscomputation) and again during row construction. The optimization caches these values in afield_valueslist, reducing ~12,000+ attribute lookups to just ~3,000 in the 1000-field test case.Eliminated
extended_joinFunction: Replaced the nested helper function with direct string joining ('|'.join(...)and'+'.join(...)), removing function call overhead and simplifying the string construction logic.Streamlined Row Building: Instead of calling
add_row_itemfunction for each cell (which internally callsgetattrand formats), the optimization directly formats pre-cached values, eliminating both function calls and redundant attribute access.Performance Impact:
Why This Works:
In Python, attribute access (
getattr) and function calls have significant overhead. The original code'smap(lambda f: len(getattr(...)), fields)pattern created a lambda function for each field and calledgetattrtwice per field (once for length calculation, once for display). The optimization reduces this to a singlegetattrper field by caching results.The performance gains are most significant for large field sets, making this optimization valuable for formatting operations on extensive data structures.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_format_table-mhwh2dd8and push.