From 9a5bbb2a0f0153ecf5d25c79deff11fc85cdf773 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 19 Sep 2024 14:15:05 +0200 Subject: [PATCH 1/3] Improve middleware performance The contextmanager in cff60ebb65e35a28b481969a8e91b72e95a9f649 added quite a bit of overhead and was only used for fixing the current app when using middleware. I did a github code search and did not find usage of set_current_app outside of aiohttp so I think its safe to remove as its unlikely to be used outside of aiohttp. --- aiohttp/web_middlewares.py | 7 ++++++- aiohttp/web_urldispatcher.py | 10 ++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/aiohttp/web_middlewares.py b/aiohttp/web_middlewares.py index 922dee3f7a1..22e63f872cf 100644 --- a/aiohttp/web_middlewares.py +++ b/aiohttp/web_middlewares.py @@ -115,7 +115,12 @@ async def impl(request: Request, handler: Handler) -> StreamResponse: def _fix_request_current_app(app: "Application") -> Middleware: async def impl(request: Request, handler: Handler) -> StreamResponse: - with request.match_info.set_current_app(app): + match_info = request.match_info + prev = match_info.current_app + match_info.current_app = app + try: return await handler(request) + finally: + match_info.current_app = prev return impl diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index c0ef3ac7969..ce6f17e7360 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -8,7 +8,6 @@ import os import re import sys -from contextlib import contextmanager from pathlib import Path from types import MappingProxyType from typing import ( @@ -271,8 +270,8 @@ def current_app(self) -> "Application": assert app is not None return app - @contextmanager - def set_current_app(self, app: "Application") -> Generator[None, None, None]: + @current_app.setter + def current_app(self, app: "Application") -> None: if DEBUG: # pragma: no cover if app not in self._apps: raise RuntimeError( @@ -280,12 +279,7 @@ def set_current_app(self, app: "Application") -> Generator[None, None, None]: self._apps, app ) ) - prev = self._current_app self._current_app = app - try: - yield - finally: - self._current_app = prev def freeze(self) -> None: self._frozen = True From 69b1e20253355ab0e12bc3a9178677f998cc261c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 19 Sep 2024 14:18:58 +0200 Subject: [PATCH 2/3] changelog --- CHANGES/9200.breaking.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGES/9200.breaking.rst diff --git a/CHANGES/9200.breaking.rst b/CHANGES/9200.breaking.rst new file mode 100644 index 00000000000..246873fcbe6 --- /dev/null +++ b/CHANGES/9200.breaking.rst @@ -0,0 +1,3 @@ +Improved middleware performance -- by :user:`bdraco`. + +The ``set_current_app`` method was removed from :class:`UrlMappingMatchInfo` because it is no longer used, and it was unlikely external caller would ever use it. From 9804f7a22b5b5b5b369b1947fd93e2e2f1f05973 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 19 Sep 2024 14:53:13 +0200 Subject: [PATCH 3/3] lint --- CHANGES/9200.breaking.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/9200.breaking.rst b/CHANGES/9200.breaking.rst index 246873fcbe6..0282e165c41 100644 --- a/CHANGES/9200.breaking.rst +++ b/CHANGES/9200.breaking.rst @@ -1,3 +1,3 @@ Improved middleware performance -- by :user:`bdraco`. -The ``set_current_app`` method was removed from :class:`UrlMappingMatchInfo` because it is no longer used, and it was unlikely external caller would ever use it. +The ``set_current_app`` method was removed from ``UrlMappingMatchInfo`` because it is no longer used, and it was unlikely external caller would ever use it.