⚡️ Speed up method StaticHandler.append_version by 107%
#82
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.
📄 107% (1.07x) speedup for
StaticHandler.append_versioninsrc/bokeh/server/views/static_handler.py⏱️ Runtime :
7.89 milliseconds→3.81 milliseconds(best of80runs)📝 Explanation and details
The optimization introduces class-level caching to eliminate repeated dictionary creation and filesystem path resolution in the
append_versionmethod.Key Change:
_static_path_cachethat stores thedict(static_path=settings.bokehjs_path())on first usehasattr()and only created once per classWhy This Improves Performance:
dict(static_path=settings.bokehjs_path())on every call toappend_version(), which involves dictionary allocation and key-value assignmentsettings.bokehjs_path()performs filesystem operations and path resolution - the profiler shows this dropped from 1035 calls to just 16 callsPerformance Impact:
The line profiler shows the
bokehjs_path()function calls dropped dramatically (1035 → 16 calls), indicating successful caching. The optimization provides 107% speedup overall, with individual test cases showing 70-110% improvements.Best For: Applications that frequently serve static files in production mode, where
append_version()is called repeatedly for the same static path configuration. The cache has no impact in dev mode since the method returns early.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-StaticHandler.append_version-mhbkp7tdand push.