Skip to content

Reduce dispatch_request processing time by 8%#5939

Closed
dalekube wants to merge 5 commits into
pallets:mainfrom
dalekube:dale-ensure-sync
Closed

Reduce dispatch_request processing time by 8%#5939
dalekube wants to merge 5 commits into
pallets:mainfrom
dalekube:dale-ensure-sync

Conversation

@dalekube
Copy link
Copy Markdown

@dalekube dalekube commented Mar 2, 2026

  1. Initialized Shadow Dictionary: Added self._sync_view_functions dictionary to the Flask class's init method in src/flask/app.py.
  2. Optimized dispatch_request: Modified the request dispatching lifecycle to check the _sync_view_functions cache first. On the first request to an endpoint, it evaluates iscoroutinefunction (via ensure_sync) and caches the result. Subsequent requests to that endpoint hit the dictionary cache, entirely bypassing the CPU overhead of introspecting the function's async nature.

This approach safely handles view_functions regardless of how they were registered (via add_url_rule, Blueprint registrations, or direct mutation) while achieving the performance goals of reducing hot-path introspection. The iscoroutinefunction method requires unwrapping decorators (using inspect.unwrap) and checking internal code flags, and it is relatively expensive in Python.

Current Estimated Performance
Total time: 9.5344 seconds (for 100,000 iterations)
Performance: 10,488.30 requests / second
Time/Req: 95.34 microseconds

New Estimated Performance
Total time: 8.7554 seconds (for 100,000 iterations)
Performance: 11,421.47 requests / second
Time/Req: 87.55 microseconds

The optimization caches the results of app.ensure_sync (which otherwise checks asyncio.iscoroutinefunction repeatedly), the difference in these numbers represents the CPU overhead eliminated during the dispatch phase.

@davidism davidism closed this Mar 2, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants