Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip middleware code when there are no user middlewares installed #2629

Merged
merged 5 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2629.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes performance issue introduced by #2577. When there are no middlewares installed by the user, no additional and useless code is executed.
20 changes: 13 additions & 7 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,18 @@ def freeze(self):
return

self._frozen = True
self._middlewares = tuple(self._prepare_middleware())
self._middlewares.freeze()
self._router.freeze()
self._on_response_prepare.freeze()
self._on_startup.freeze()
self._on_shutdown.freeze()
self._on_cleanup.freeze()
self._run_middlewares = len(self.middlewares) > 0
self._middlewares_handlers = tuple(self._prepare_middleware())

for subapp in self._subapps:
subapp.freeze()
self._run_middlewares |= len(subapp._middlewares) > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line is cryptic a little.
Please replace with self._run_middlewares = self._run_middlewares or subapp._run_middlewares
The change respects middlewares from deep nested subapps: app -> subapp1 -> subapp2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, done. Added also a bit of internal documentation about this flag and the root cause of its usage.


@property
def debug(self):
Expand Down Expand Up @@ -241,6 +244,7 @@ def _prepare_middleware(self):
'see #2252'.format(m),
DeprecationWarning, stacklevel=2)
yield m, False

yield _fix_request_current_app(self), True

async def _handle(self, request):
Expand All @@ -260,12 +264,14 @@ async def _handle(self, request):

if resp is None:
handler = match_info.handler
for app in match_info.apps[::-1]:
for m, new_style in app._middlewares:
if new_style:
handler = partial(m, handler=handler)
else:
handler = await m(app, handler)

if self._run_middlewares:
for app in match_info.apps[::-1]:
for m, new_style in app._middlewares_handlers:
if new_style:
handler = partial(m, handler=handler)
else:
handler = await m(app, handler)

resp = await handler(request)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def test_app_delitem():
def test_app_freeze():
app = web.Application()
subapp = mock.Mock()
subapp._middlewares = ()
app._subapps.append(subapp)

app.freeze()
Expand All @@ -210,3 +211,28 @@ def test_equality():

assert app1 == app1
assert app1 != app2


def test_app_run_middlewares():

root = web.Application()
sub = web.Application()
root.add_subapp('/sub', sub)
root.freeze()
assert root._run_middlewares is False

@web.middleware
async def middleware(request, handler):
return await handler(request)

root = web.Application(middlewares=[middleware])
sub = web.Application()
root.add_subapp('/sub', sub)
root.freeze()
assert root._run_middlewares is True

root = web.Application()
sub = web.Application(middlewares=[middleware])
root.add_subapp('/sub', sub)
root.freeze()
assert root._run_middlewares is True