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

Micro-optimization #3095

Merged
merged 22 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions aiohttp/base_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


class BaseProtocol(asyncio.Protocol):
__slots__ = ('_loop', '_paused', '_drain_waiter',
'_connection_lost', 'transport')

def __init__(self, loop=None):
if loop is None:
self._loop = asyncio.get_event_loop()
Expand Down
13 changes: 11 additions & 2 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,17 @@ class RequestHandler(BaseProtocol):
:param int max_headers: Optional maximum header size

"""
_request_count = 0
_keepalive = False # keep transport open
KEEPALIVE_RESCHEDULE_DELAY = 1

__slots__ = ('_request_count', '_keep_alive', '_manager',
'_request_handler', '_request_factory', '_tcp_keepalive',
'_keepalive_time', '_keepalive_handle', '_keepalive_timeout',
'_lingering_time', '_messages', '_message_tail',
'_waiter', '_error_handler', '_task_handler',
'_upgrade', '_payload_parser', '_request_parser',
'_reading_paused', 'logger', 'debug', 'access_log',
'access_logger', '_close', '_force_close')
Copy link
Member

Choose a reason for hiding this comment

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

May be have it one-item-per-line and sorted? Would simplify management and new items addition if that happens.

    __slots__ = (
        '_close',
        '_error_handler',
        '_force_close',
        '_keep_alive',
        '_keepalive_handle',
        '_keepalive_time',
        '_keepalive_timeout',
        '_lingering_time',
        '_manager',
        '_message_tail',
        '_messages',
        '_payload_parser',
        '_reading_paused',
        '_request_count',
        '_request_factory',
        '_request_handler',
        '_request_parser',
        '_task_handler',
        '_tcp_keepalive',
        '_upgrade',
        '_waiter',
        'access_log',
        'access_logger',
        'debug',
        'logger',
    )

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorting is boring and weak: very easy to make a mistake in sort order.

I'm thinking about ultimately applying black but not ready for it yet.

Copy link
Member

Choose a reason for hiding this comment

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

black is good, but you wouldn't like what it will do with string quotes - it prefers double ultimately.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we can live with it.

Copy link
Member

Choose a reason for hiding this comment

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

What is black?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@kxepal there's a setting to disable quotes normalization. But there's other things I dislike about the style of blackened code.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's a new feature. I agree that black isn't perfect, but eventually, there are no much good alternatives. Good news: it's quite easy to fork it and fix stuff to make you like it. In the end, we could have some aio-libs style guide which is backed by ours formatter. Still that's better than nothing.


def __init__(self, manager, *, loop=None,
keepalive_timeout=75, # NGINX default value is 75 secs
tcp_keepalive=True,
Expand All @@ -94,6 +101,8 @@ def __init__(self, manager, *, loop=None,

super().__init__(loop=loop)

self._request_count = 0
self._keepalive = False
self._manager = manager
self._request_handler = manager.request_handler
self._request_factory = manager.request_factory
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,5 +700,5 @@ async def _prepare_hook(self, response):
match_info = self._match_info
if match_info is None:
return
for app in match_info.apps:
for app in match_info._apps:
await app.on_response_prepare.send(self, response)
6 changes: 3 additions & 3 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class UrlMappingMatchInfo(dict, AbstractMatchInfo):
def __init__(self, match_dict, route):
super().__init__(match_dict)
self._route = route
self._apps = ()
self._apps = []
self._current_app = None
self._frozen = False

Expand All @@ -186,14 +186,14 @@ def get_info(self):

@property
def apps(self):
return self._apps
return tuple(self._apps)

def add_app(self, app):
if self._frozen:
raise RuntimeError("Cannot change apps stack after .freeze() call")
if self._current_app is None:
self._current_app = app
self._apps = (app,) + self._apps
self._apps.insert(0, app)

@property
def current_app(self):
Expand Down