Repository navigation
copy compiled rules instead of recompiling them when replacing the URL map - #70
Closed
alexrashed wants to merge 1 commit into
Closed
alexrashed wants to merge 1 commit into
alexrashed wants to merge 1 commit into
Conversation
…L map Router.add, add_rule and remove replace the URL map with a copy, and the copy compiled every existing rule again. Registering n rules one at a time therefore compiled n²/2 rules. The copies now take over the compiled state of the rules, with their own converters and URL builders, so they keep no reference to the replaced map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
Closing this one again, I feel like the complexity is not worth the runtime benefits for now. Maybe something to explore again in the future... |
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.
Motivation
Router.add,add_ruleandremovereplace the URL map with a copy, so thatdispatchnever sees a map while it is being changed. The copy is built withold_rule.empty()plusMap.add, which compiles every existing rule again: its matching parts, and two URL builder functions generated throughastandcompile. Registering n rules one at a time therefore compiles about n²/2 rules.In LocalStack this showed up as the biggest single cost of a cold start. Profiling the combined AWS and Azure runtime, rule compilation inside
_clone_map_with_rulestook about 1.9 s of a 4.3 s startup. Most of it came from routers that get a few dozen rules each, oneaddat a time.Changes
_clone_map_with_rulescopies each bound rule instead of compiling a fresh one, and adds the copy to the new map the wayMap.adddoes after binding (_matcher,_rules_by_endpoint,_remap)._remove_rulesuses the same copy, through a newexcludeparameter, so removing a rule no longer compiles all the remaining ones either.This relies on werkzeug internals (
Rule._converters,Rule._build,Rule._build_unknown,Map._matcher,Map._rules_by_endpoint). They have been stable since the state-machine matcher arrived in werkzeug 2.2, and rolo requires werkzeug>=3.0. The new tests fail if they change.Testing
tests/test_router.py:int,pathandportconverters, and every rule and converter is bound to the current map;ruffpass locally.🤖 Generated with Claude Code