Re: [Python-Dev] [Python-checkins] cpython: asyncio.events: Use __slots__ in Handle and TimerHandle
On 02/12/2014 02:02 PM, yury.selivanov wrote:
http://hg.python.org/cpython/rev/920304e1f36b changeset: 89175:920304e1f36b user: Yury Selivanov <yselivanov@sprymix.com> date: Wed Feb 12 17:01:52 2014 -0500 summary: asyncio.events: Use __slots__ in Handle and TimerHandle
files: Lib/asyncio/events.py | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -19,6 +19,8 @@ class Handle: """Object returned by callback registration methods."""
+ __slots__ = ['_callback', '_args', '_cancelled'] + def __init__(self, callback, args): assert not isinstance(callback, Handle), 'A Handle is not a callback' self._callback = callback @@ -46,6 +48,8 @@ class TimerHandle(Handle): """Object returned by timed callback registration methods."""
+ __slots__ = ['_when'] + def __init__(self, when, callback, args): assert when is not None super().__init__(callback, args)
Apologies up front if these are stupid questions, but: Why __slots__? Are we going to have so many of these things that memory is an issue? The asserts in the code -- those are not for checking user input, correct? -- ~Ethan~
On Wed, Feb 12, 2014 at 3:10 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
On 02/12/2014 02:02 PM, yury.selivanov wrote:
http://hg.python.org/cpython/rev/920304e1f36b changeset: 89175:920304e1f36b user: Yury Selivanov <yselivanov@sprymix.com> date: Wed Feb 12 17:01:52 2014 -0500 summary: asyncio.events: Use __slots__ in Handle and TimerHandle
files: Lib/asyncio/events.py | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -19,6 +19,8 @@ class Handle: """Object returned by callback registration methods."""
+ __slots__ = ['_callback', '_args', '_cancelled'] + def __init__(self, callback, args): assert not isinstance(callback, Handle), 'A Handle is not a callback' self._callback = callback @@ -46,6 +48,8 @@ class TimerHandle(Handle): """Object returned by timed callback registration methods."""
+ __slots__ = ['_when'] + def __init__(self, when, callback, args): assert when is not None super().__init__(callback, args)
Apologies up front if these are stupid questions, but:
Why __slots__? Are we going to have so many of these things that memory is an issue
There's one of these created for every callback -- which includes every time 'yield from' blocks for a Future. The savings are primarily in allocation cost (no dicts to allocate).
The asserts in the code -- those are not for checking user input, correct?
Yeah, this class is never instantiated directly by the user. -- --Guido van Rossum (python.org/~guido)
participants (2)
-
Ethan Furman
-
Guido van Rossum