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
Changes from 1 commit
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
Next Next commit
Skip middleware code when there are no user middlewares installed
Fixes performance issue introduced by #2577, boosting from 8K req/sec
to almost 9K req/sec.

If there are no middlewares installed by the user the attribute
`request._match_info.current_app` already looks at to the right app,
this is by design.
pfreixes committed Dec 28, 2017
commit bb4a60cdd57b90668099465917c02836cf7736f4
20 changes: 13 additions & 7 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
@@ -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):
@@ -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):
@@ -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)

25 changes: 25 additions & 0 deletions tests/test_web_app.py
Original file line number Diff line number Diff line change
@@ -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()
@@ -210,3 +211,27 @@ 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 == 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 == True

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