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
cdef Timeout
cdef get_hub
from _greenlet cimport get_hub
cdef bint _greenlet_imported
......
......@@ -11,7 +11,7 @@ import functools
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 reraise
from gevent.lock import Semaphore, DummySemaphore
......
......@@ -75,9 +75,6 @@ cdef inline list _extract_stack(int limit)
@cython.locals(previous=_Frame, frame=tuple, f=_Frame)
cdef _Frame _Frame_from_list(list frames)
@cython.final
cdef inline get_hub()
cdef class Greenlet(greenlet):
cdef readonly object value
......@@ -126,6 +123,12 @@ cdef class Greenlet(greenlet):
# method
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
# be accessed directly as static vars without
# doing a module global lookup. This is especially important
......
......@@ -2,7 +2,6 @@
from __future__ import print_function, absolute_import, division
import sys
from gevent.hub import get_hub
from gevent.timeout import Timeout
......@@ -11,13 +10,13 @@ __all__ = [
'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
# is assignment) without generating a 'lvalue is not valid target'
# error.
locals()['getcurrent'] = __import__('greenlet').getcurrent
locals()['greenlet_init'] = lambda: None
locals()['get_hub'] = __import__('gevent').get_hub
class Semaphore(object):
"""
......@@ -88,7 +87,8 @@ class Semaphore(object):
# NOTE: Passing the bound method will cause a memory leak on PyPy
# 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
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):
# Subclasses CANNOT override. This is a cdef method.
......@@ -176,7 +176,7 @@ class Semaphore(object):
timer = Timeout._start_new_or_dummy(timeout)
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, )
except Timeout as ex:
if ex is not timer:
......
......@@ -70,7 +70,7 @@ __imports__.extend(__py3_imports__)
import time
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.timeout import Timeout
from gevent._compat import string_types, integer_types, PY3
......
......@@ -2,11 +2,16 @@
"""Basic synchronization primitives: Event and AsyncResult"""
from __future__ import print_function
import sys
from gevent.hub import get_hub, getcurrent, _NONE
from gevent._util import _NONE
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.timeout import Timeout
from gevent._tblib import dump_traceback, load_traceback
__all__ = ['Event', 'AsyncResult']
......
......@@ -13,6 +13,7 @@ from gevent._compat import reraise
from gevent._compat import PYPY as _PYPY
from gevent._tblib import dump_traceback
from gevent._tblib import load_traceback
from gevent.hub import GreenletExit
from gevent.hub import InvalidSwitchError
from gevent.hub import Waiter
......@@ -20,6 +21,7 @@ from gevent.hub import _threadlocal
from gevent.hub import get_hub_class
from gevent.hub import iwait
from gevent.hub import wait
from gevent.timeout import Timeout
from gevent._config import config as GEVENT_CONFIG
......@@ -45,6 +47,7 @@ locals()['greenlet_init'] = lambda: None
def get_hub():
# This is identical to gevent.hub._get_hub_noargs so that it
# can be inlined for greenlet spawning by cython.
# It is also cimported in semaphore.pxd
hub = _threadlocal.hub
if hub is None:
hubtype = get_hub_class()
......
......@@ -45,7 +45,8 @@ from __future__ import absolute_import
import os
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._compat import PY3
from gevent._util import copy_globals
......
......@@ -38,7 +38,9 @@ Full = __queue__.Full
Empty = __queue__.Empty
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
......
......@@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function
import sys
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._compat import integer_types
from gevent._compat import iteritems
......
......@@ -100,7 +100,7 @@ def signal(signalnum, handler):
# Note that this conflicts with gevent.subprocess and other users
# of child watchers, until the next time gevent.subprocess/loop.install_sigchld()
# 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)
get_hub().loop.reset_sigchld()
return old_handler
......
......@@ -40,7 +40,10 @@ import signal
import sys
import traceback
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 PY3
from gevent._compat import reraise
......
......@@ -3,7 +3,10 @@ from __future__ import absolute_import
import sys
import os
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.greenlet import Greenlet
from gevent.pool import GroupMappingMixin
......@@ -200,7 +203,7 @@ class ThreadPool(GroupMappingMixin):
# pylint:disable=too-many-branches
need_decrease = True
try:
while True:
while 1: # tiny bit faster than True on Py2
task_queue = self.task_queue
task = task_queue.get()
try:
......
......@@ -14,8 +14,12 @@ module add timeouts to arbitrary code.
which no switches occur, :class:`Timeout` is powerless.
"""
from __future__ import absolute_import, print_function, division
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__ = [
'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