Skip to content

Commit 7683a9b

Browse files
authored
Merge pull request #219 from BobTheBuidler/cdef-aliases
feat: cdef aliases to eliminate global lookups
2 parents f8adf8b + 8ace7b3 commit 7683a9b

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

cytoolz/dicttoolz.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ from cpython.ref cimport PyObject, Py_DECREF, Py_INCREF, Py_XDECREF
88
# Locally defined bindings that differ from `cython.cpython` bindings
99
from cytoolz.cpython cimport PyDict_Next_Compat, PtrIter_Next
1010

11-
from copy import copy
12-
from collections.abc import Mapping
11+
from collections import abc
12+
13+
14+
# cdef aliases to eliminate global lookups
15+
16+
cdef object Mapping = abc.Mapping
17+
del abc
1318

1419

1520
__all__ = ['merge', 'merge_with', 'valmap', 'keymap', 'itemmap', 'valfilter',

cytoolz/functoolz.pyx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
import inspect
2-
import sys
3-
from functools import partial
4-
from importlib import import_module
5-
from operator import attrgetter
6-
from types import MethodType
7-
from cytoolz.utils import no_default
8-
import cytoolz._signatures as _sigs
9-
10-
from toolz.functoolz import (InstanceProperty, instanceproperty, is_arity,
11-
num_required_args, has_varargs, has_keywords,
12-
is_valid_args, is_partial_args)
13-
141
cimport cython
152
from cpython.dict cimport PyDict_Merge, PyDict_New
163
from cpython.object cimport (PyCallable_Check, PyObject_Call, PyObject_CallObject,
@@ -20,6 +7,48 @@ from cpython.sequence cimport PySequence_Concat
207
from cpython.set cimport PyFrozenSet_New
218
from cpython.tuple cimport PyTuple_Check, PyTuple_GET_SIZE
229

10+
import functools
11+
import importlib
12+
import inspect
13+
import operator
14+
import types
15+
from toolz import functoolz as functoolz_py
16+
17+
from cytools import _signatures
18+
from cytoolz import utils
19+
20+
# cdef constants to eliminate global lookups
21+
cdef object partial = functools.partial
22+
del functools
23+
24+
cdef object import_module = importlib.import_module
25+
del importlib
26+
27+
cdef object signature = inspect.signature
28+
del inspect
29+
30+
cdef object attrgetter = operator.attrgetter
31+
del operator
32+
33+
cdef object MethodType = types.MethodType
34+
del types
35+
36+
cdef object InstanceProperty = functoolz_py.InstanceProperty
37+
cdef object instanceproperty = functoolz_py.instanceproperty
38+
cdef object is_arity = functoolz_py.is_arity
39+
cdef object num_required_args = functoolz_py.num_required_args
40+
cdef object has_varargs = functoolz_py.has_varargs
41+
cdef object has_keywords = functoolz_py.has_keywords
42+
cdef object is_valid_args = functoolz_py.is_valid_args
43+
cdef object is_partial_args = functoolz_py.is_partial_args
44+
del functoolz_py
45+
46+
cdef object no_default = utils.no_default
47+
del utils
48+
49+
cdef object signature_or_spec = _signatures.signature_or_spec
50+
del _signatures
51+
2352

2453
__all__ = ['identity', 'thread_first', 'thread_last', 'memoize', 'compose', 'compose_left',
2554
'pipe', 'complement', 'juxt', 'do', 'curry', 'memoize', 'flip',
@@ -289,7 +318,7 @@ cdef class curry:
289318
# kwargs = dict(self.keywords, **kwargs)
290319

291320
if self._sigspec is None:
292-
sigspec = self._sigspec = _sigs.signature_or_spec(func)
321+
sigspec = self._sigspec = signature_or_spec(func)
293322
self._has_unknown_args = has_varargs(func, sigspec) is not False
294323
else:
295324
sigspec = self._sigspec
@@ -330,7 +359,7 @@ cdef class curry:
330359

331360
property __signature__:
332361
def __get__(self):
333-
sig = inspect.signature(self.func)
362+
sig = signature(self.func)
334363
args = self.args or ()
335364
keywords = self.keywords or {}
336365
if is_partial_args(self.func, args, keywords, sig) is False:
@@ -558,8 +587,8 @@ cdef class Compose:
558587

559588
property __signature__:
560589
def __get__(self):
561-
base = inspect.signature(self.first)
562-
last = inspect.signature(self.funcs[-1])
590+
base = signature(self.first)
591+
last = signature(self.funcs[-1])
563592
return base.replace(return_annotation=last.return_annotation)
564593

565594
property __name__:

cytoolz/itertoolz.pyx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,31 @@ from cpython.tuple cimport PyTuple_GET_ITEM, PyTuple_GetSlice, PyTuple_New, PyTu
1010
# Locally defined bindings that differ from `cython.cpython` bindings
1111
from cytoolz.cpython cimport PtrIter_Next, PtrObject_GetItem
1212

13-
from collections import deque
14-
from heapq import heapify, heappop, heapreplace
15-
from itertools import chain, islice, zip_longest
16-
from operator import itemgetter
17-
from cytoolz.utils import no_default
13+
import collections
14+
import heapq
15+
import itertools
16+
import operator
17+
from cytools import utils
18+
19+
# cdef aliases to eliminate global lookups
20+
cdef object deque = collections.deque
21+
del collections
22+
23+
cdef object heapify = heapq.heapify
24+
cdef object heappop = heapq.heappop
25+
cdef object heapreplace = heapq.heapreplace
26+
del heapq
27+
28+
cdef object chain = itertools.chain
29+
cdef object islice = itertools.islice
30+
cdef object zip_longest = itertools.zip_longest
31+
del itertools
32+
33+
cdef object itemgetter = operator.itemgetter
34+
del operator
35+
36+
cdef object no_default = utils.no_default
37+
del utils
1838

1939

2040
__all__ = ['remove', 'accumulate', 'groupby', 'merge_sorted', 'interleave',

cytoolz/recipes.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from cpython.sequence cimport PySequence_Tuple
22
from cytoolz.itertoolz cimport frequencies, pluck
33

4-
from itertools import groupby
4+
import itertools
5+
6+
# cdef alias to eliminate global lookups
7+
cdef object groupby = itertools.groupby
8+
del itertools
59

610

711
__all__ = ['countby', 'partitionby']

0 commit comments

Comments
 (0)