Commit 61a5f2ef authored by Jason Madden's avatar Jason Madden

Make more performance-sensitive places use _get_hub_noargs, since under Python...

Make more performance-sensitive places use _get_hub_noargs, since under Python 3 it is twice as fast to call as get_hub.
parent c7491589
# cython: auto_pickle=False # cython: auto_pickle=False
cdef Timeout cdef Timeout
cdef get_hub from _greenlet cimport get_hub
cdef bint _greenlet_imported cdef bint _greenlet_imported
......
...@@ -11,7 +11,7 @@ import functools ...@@ -11,7 +11,7 @@ import functools
import sys import sys
from gevent.hub import get_hub from gevent.hub import _get_hub_noargs as get_hub
from gevent._compat import integer_types from gevent._compat import integer_types
from gevent._compat import reraise from gevent._compat import reraise
from gevent.lock import Semaphore, DummySemaphore from gevent.lock import Semaphore, DummySemaphore
......
...@@ -75,9 +75,6 @@ cdef inline list _extract_stack(int limit) ...@@ -75,9 +75,6 @@ cdef inline list _extract_stack(int limit)
@cython.locals(previous=_Frame, frame=tuple, f=_Frame) @cython.locals(previous=_Frame, frame=tuple, f=_Frame)
cdef _Frame _Frame_from_list(list frames) cdef _Frame _Frame_from_list(list frames)
@cython.final
cdef inline get_hub()
cdef class Greenlet(greenlet): cdef class Greenlet(greenlet):
cdef readonly object value cdef readonly object value
...@@ -126,6 +123,12 @@ cdef class Greenlet(greenlet): ...@@ -126,6 +123,12 @@ cdef class Greenlet(greenlet):
# method # method
cpdef _raise_exception(self) cpdef _raise_exception(self)
@cython.final
cdef greenlet get_hub()
# XXX: TODO: Move the definition of TrackedRawGreenlet
# into a file that can be cython compiled so get_hub can
# return that.
# Declare a bunch of imports as cdefs so they can # Declare a bunch of imports as cdefs so they can
# be accessed directly as static vars without # be accessed directly as static vars without
# doing a module global lookup. This is especially important # doing a module global lookup. This is especially important
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import sys import sys
from gevent.hub import get_hub
from gevent.timeout import Timeout from gevent.timeout import Timeout
...@@ -11,13 +10,13 @@ __all__ = [ ...@@ -11,13 +10,13 @@ __all__ = [
'BoundedSemaphore', 'BoundedSemaphore',
] ]
# In Cython, we define these as 'cdef inline' functions. The # In Cython, we define these as 'cdef [inline]' functions. The
# compilation unit cannot have a direct assignment to them (import # compilation unit cannot have a direct assignment to them (import
# is assignment) without generating a 'lvalue is not valid target' # is assignment) without generating a 'lvalue is not valid target'
# error. # error.
locals()['getcurrent'] = __import__('greenlet').getcurrent locals()['getcurrent'] = __import__('greenlet').getcurrent
locals()['greenlet_init'] = lambda: None locals()['greenlet_init'] = lambda: None
locals()['get_hub'] = __import__('gevent').get_hub
class Semaphore(object): class Semaphore(object):
""" """
...@@ -88,7 +87,8 @@ class Semaphore(object): ...@@ -88,7 +87,8 @@ class Semaphore(object):
# NOTE: Passing the bound method will cause a memory leak on PyPy # NOTE: Passing the bound method will cause a memory leak on PyPy
# with Cython <= 0.23.3. You must use >= 0.23.4. # with Cython <= 0.23.3. You must use >= 0.23.4.
# See https://bitbucket.org/pypy/pypy/issues/2149/memory-leak-for-python-subclass-of-cpyext#comment-22371546 # See https://bitbucket.org/pypy/pypy/issues/2149/memory-leak-for-python-subclass-of-cpyext#comment-22371546
self._notifier = get_hub().loop.run_callback(self._notify_links) hub = get_hub() # pylint:disable=undefined-variable
self._notifier = hub.loop.run_callback(self._notify_links)
def _notify_links(self): def _notify_links(self):
# Subclasses CANNOT override. This is a cdef method. # Subclasses CANNOT override. This is a cdef method.
...@@ -176,7 +176,7 @@ class Semaphore(object): ...@@ -176,7 +176,7 @@ class Semaphore(object):
timer = Timeout._start_new_or_dummy(timeout) timer = Timeout._start_new_or_dummy(timeout)
try: try:
try: try:
result = get_hub().switch() result = get_hub().switch() # pylint:disable=undefined-variable
assert result is self, 'Invalid switch into Semaphore.wait/acquire(): %r' % (result, ) assert result is self, 'Invalid switch into Semaphore.wait/acquire(): %r' % (result, )
except Timeout as ex: except Timeout as ex:
if ex is not timer: if ex is not timer:
......
...@@ -70,7 +70,7 @@ __imports__.extend(__py3_imports__) ...@@ -70,7 +70,7 @@ __imports__.extend(__py3_imports__)
import time import time
import sys import sys
from gevent.hub import get_hub from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import ConcurrentObjectUseError from gevent.hub import ConcurrentObjectUseError
from gevent.timeout import Timeout from gevent.timeout import Timeout
from gevent._compat import string_types, integer_types, PY3 from gevent._compat import string_types, integer_types, PY3
......
...@@ -2,11 +2,16 @@ ...@@ -2,11 +2,16 @@
"""Basic synchronization primitives: Event and AsyncResult""" """Basic synchronization primitives: Event and AsyncResult"""
from __future__ import print_function from __future__ import print_function
import sys import sys
from gevent.hub import get_hub, getcurrent, _NONE
from gevent._util import _NONE
from gevent._compat import reraise from gevent._compat import reraise
from gevent._tblib import dump_traceback, load_traceback
from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import getcurrent
from gevent.hub import InvalidSwitchError from gevent.hub import InvalidSwitchError
from gevent.timeout import Timeout from gevent.timeout import Timeout
from gevent._tblib import dump_traceback, load_traceback
__all__ = ['Event', 'AsyncResult'] __all__ = ['Event', 'AsyncResult']
......
...@@ -13,6 +13,7 @@ from gevent._compat import reraise ...@@ -13,6 +13,7 @@ from gevent._compat import reraise
from gevent._compat import PYPY as _PYPY from gevent._compat import PYPY as _PYPY
from gevent._tblib import dump_traceback from gevent._tblib import dump_traceback
from gevent._tblib import load_traceback from gevent._tblib import load_traceback
from gevent.hub import GreenletExit from gevent.hub import GreenletExit
from gevent.hub import InvalidSwitchError from gevent.hub import InvalidSwitchError
from gevent.hub import Waiter from gevent.hub import Waiter
...@@ -20,6 +21,7 @@ from gevent.hub import _threadlocal ...@@ -20,6 +21,7 @@ from gevent.hub import _threadlocal
from gevent.hub import get_hub_class from gevent.hub import get_hub_class
from gevent.hub import iwait from gevent.hub import iwait
from gevent.hub import wait from gevent.hub import wait
from gevent.timeout import Timeout from gevent.timeout import Timeout
from gevent._config import config as GEVENT_CONFIG from gevent._config import config as GEVENT_CONFIG
...@@ -45,6 +47,7 @@ locals()['greenlet_init'] = lambda: None ...@@ -45,6 +47,7 @@ locals()['greenlet_init'] = lambda: None
def get_hub(): def get_hub():
# This is identical to gevent.hub._get_hub_noargs so that it # This is identical to gevent.hub._get_hub_noargs so that it
# can be inlined for greenlet spawning by cython. # can be inlined for greenlet spawning by cython.
# It is also cimported in semaphore.pxd
hub = _threadlocal.hub hub = _threadlocal.hub
if hub is None: if hub is None:
hubtype = get_hub_class() hubtype = get_hub_class()
......
...@@ -45,7 +45,8 @@ from __future__ import absolute_import ...@@ -45,7 +45,8 @@ from __future__ import absolute_import
import os import os
import sys import sys
from gevent.hub import get_hub, reinit from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import reinit
from gevent._config import config from gevent._config import config
from gevent._compat import PY3 from gevent._compat import PY3
from gevent._util import copy_globals from gevent._util import copy_globals
......
...@@ -38,7 +38,9 @@ Full = __queue__.Full ...@@ -38,7 +38,9 @@ Full = __queue__.Full
Empty = __queue__.Empty Empty = __queue__.Empty
from gevent.timeout import Timeout from gevent.timeout import Timeout
from gevent.hub import get_hub, Waiter, getcurrent from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import Waiter
from gevent.hub import getcurrent
from gevent.hub import InvalidSwitchError from gevent.hub import InvalidSwitchError
......
...@@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function ...@@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function
import sys import sys
from gevent.event import Event from gevent.event import Event
from gevent.hub import get_hub from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import sleep as _g_sleep from gevent.hub import sleep as _g_sleep
from gevent._compat import integer_types from gevent._compat import integer_types
from gevent._compat import iteritems from gevent._compat import iteritems
......
...@@ -100,7 +100,7 @@ def signal(signalnum, handler): ...@@ -100,7 +100,7 @@ def signal(signalnum, handler):
# Note that this conflicts with gevent.subprocess and other users # Note that this conflicts with gevent.subprocess and other users
# of child watchers, until the next time gevent.subprocess/loop.install_sigchld() # of child watchers, until the next time gevent.subprocess/loop.install_sigchld()
# is called. # is called.
from gevent import get_hub # Are we always safe to import here? from gevent.hub import get_hub # Are we always safe to import here?
_signal_signal(signalnum, handler) _signal_signal(signalnum, handler)
get_hub().loop.reset_sigchld() get_hub().loop.reset_sigchld()
return old_handler return old_handler
......
...@@ -40,7 +40,10 @@ import signal ...@@ -40,7 +40,10 @@ import signal
import sys import sys
import traceback import traceback
from gevent.event import AsyncResult from gevent.event import AsyncResult
from gevent.hub import get_hub, linkproxy, sleep, getcurrent from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import linkproxy
from gevent.hub import sleep
from gevent.hub import getcurrent
from gevent._compat import integer_types, string_types, xrange from gevent._compat import integer_types, string_types, xrange
from gevent._compat import PY3 from gevent._compat import PY3
from gevent._compat import reraise from gevent._compat import reraise
......
...@@ -3,7 +3,10 @@ from __future__ import absolute_import ...@@ -3,7 +3,10 @@ from __future__ import absolute_import
import sys import sys
import os import os
from gevent._compat import integer_types from gevent._compat import integer_types
from gevent.hub import get_hub, getcurrent, sleep, _get_hub from gevent.hub import _get_hub_noargs as get_hub
from gevent.hub import getcurrent
from gevent.hub import sleep
from gevent.hub import _get_hub
from gevent.event import AsyncResult from gevent.event import AsyncResult
from gevent.greenlet import Greenlet from gevent.greenlet import Greenlet
from gevent.pool import GroupMappingMixin from gevent.pool import GroupMappingMixin
...@@ -200,7 +203,7 @@ class ThreadPool(GroupMappingMixin): ...@@ -200,7 +203,7 @@ class ThreadPool(GroupMappingMixin):
# pylint:disable=too-many-branches # pylint:disable=too-many-branches
need_decrease = True need_decrease = True
try: try:
while True: while 1: # tiny bit faster than True on Py2
task_queue = self.task_queue task_queue = self.task_queue
task = task_queue.get() task = task_queue.get()
try: try:
......
...@@ -14,8 +14,12 @@ module add timeouts to arbitrary code. ...@@ -14,8 +14,12 @@ module add timeouts to arbitrary code.
which no switches occur, :class:`Timeout` is powerless. which no switches occur, :class:`Timeout` is powerless.
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
from gevent._compat import string_types from gevent._compat import string_types
from gevent.hub import getcurrent, _NONE, get_hub from gevent._util import _NONE
from gevent.hub import getcurrent
from gevent.hub import _get_hub_noargs as get_hub
__all__ = [ __all__ = [
'Timeout', 'Timeout',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment