Skip to content

Commit

Permalink
Make import times significantly faster
Browse files Browse the repository at this point in the history
Thanks @fjetter for identifying `toolz.curried.operator` as the main
culprit in dask/distributed#6659
  • Loading branch information
eriknw committed Jul 5, 2022
1 parent 32135de commit 77c044e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
21 changes: 10 additions & 11 deletions toolz/curried/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import operator

from toolz.functoolz import curry, num_required_args, has_keywords


def should_curry(f):
num = num_required_args(f)
return num is None or num > 1 or num == 1 and has_keywords(f) is not False
from toolz.functoolz import curry


# Tests will catch if/when this needs updated
IGNORE = {
"__abs__", "__index__", "__inv__", "__invert__", "__neg__", "__not__",
"__pos__", "_abs", "abs", "attrgetter", "index", "inv", "invert",
"itemgetter", "neg", "not_", "pos", "truth"
}
locals().update(
{name: curry(f) if should_curry(f) else f
for name, f in vars(operator).items() if callable(f)},
{name: f if name in IGNORE else curry(f)
for name, f in vars(operator).items() if callable(f)}
)

# Clean up the namespace.
del IGNORE
del curry
del num_required_args
del has_keywords
del operator
del should_curry
4 changes: 2 additions & 2 deletions toolz/functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import sys
from operator import attrgetter, not_
from importlib import import_module
from textwrap import dedent
from types import MethodType
import sys

from .utils import no_default

Expand Down Expand Up @@ -780,6 +778,8 @@ def __call__(self, *args, **kwargs):

@instanceproperty(classval=__doc__)
def __doc__(self):
from textwrap import dedent

exc = self.exc
try:
if isinstance(exc, tuple):
Expand Down
3 changes: 2 additions & 1 deletion toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import operator
from functools import partial
from itertools import filterfalse, zip_longest
from random import Random
from collections.abc import Sequence
from toolz.utils import no_default

Expand Down Expand Up @@ -1052,5 +1051,7 @@ def random_sample(prob, seq, random_state=None):
[7, 9, 19, 25, 30, 32, 34, 48, 59, 60, 81, 98]
"""
if not hasattr(random_state, 'random'):
from random import Random

random_state = Random(random_state)
return filter(lambda _: random_state.random() < prob, seq)

0 comments on commit 77c044e

Please sign in to comment.