Skip to content

copy compiled rules instead of recompiling them when replacing the URL map - #70

Closed
alexrashed wants to merge 1 commit into
mainfrom
router-copy-compiled-rules
Closed

alexrashed wants to merge 1 commit into
mainfrom
router-copy-compiled-rules

Conversation

@alexrashed

@alexrashed alexrashed commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Motivation

Router.add, add_rule and remove replace the URL map with a copy, so that dispatch never sees a map while it is being changed. The copy is built with old_rule.empty() plus Map.add, which compiles every existing rule again: its matching parts, and two URL builder functions generated through ast and compile. 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_rules took about 1.9 s of a 4.3 s startup. Most of it came from routers that get a few dozen rules each, one add at a time.

Changes

  • _clone_map_with_rules copies each bound rule instead of compiling a fresh one, and adds the copy to the new map the way Map.add does after binding (_matcher, _rules_by_endpoint, _remap).
  • Each copy gets its own converters and its own URL builders (the builders are methods bound to the original rule), all pointing to the new map. A copy keeps no reference to the replaced map, so replaced maps can still be garbage-collected. Without that, a router that keeps adding rules at runtime would keep every earlier map alive.
  • _remove_rules uses the same copy, through a new exclude parameter, so removing a rule no longer compiles all the remaining ones either.
  • If a werkzeug version lacks the compiled state that gets copied, the clone falls back to compiling a fresh rule, as before.

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

  • New tests in tests/test_router.py:
    • adding and removing rules compiles each rule exactly once;
    • copied rules still match and build URLs, including the int, path and port converters, and every rule and converter is bound to the current map;
    • a replaced map is garbage-collected.
  • The full test suite and ruff pass locally.

🤖 Generated with Claude Code

…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>
@alexrashed

Copy link
Copy Markdown
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...

@alexrashed alexrashed closed this Nov 27, 2026
@alexrashed
alexrashed deleted the router-copy-compiled-rules branch November 27, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant