Skip to content

Commit

Permalink
Use a cache on the backend itself to store the context.
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi committed Sep 27, 2019
1 parent ce82053 commit 39d2ff9
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions uarray/_backend.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
from typing import (
Callable,
Iterable,
Dict,
Tuple,
Any,
Set,
Optional,
Type,
Union,
List,
)
import typing
import inspect
import functools
from . import _uarray # type: ignore
import copyreg # type: ignore
import atexit
import pickle
import weakref

ArgumentExtractorType = Callable[..., Tuple["Dispatchable", ...]]
ArgumentReplacerType = Callable[[Tuple, Dict, Tuple], Tuple[Tuple, Dict]]
ArgumentExtractorType = typing.Callable[..., typing.Tuple["Dispatchable", ...]]
ArgumentReplacerType = typing.Callable[
[typing.Tuple, typing.Dict, typing.Tuple], typing.Tuple[typing.Tuple, typing.Dict]
]

from ._uarray import ( # type: ignore
BackendNotImplementedError,
Expand Down Expand Up @@ -103,7 +95,7 @@ def generate_multimethod(
argument_extractor: ArgumentExtractorType,
argument_replacer: ArgumentReplacerType,
domain: str,
default: Optional[Callable] = None,
default: typing.Optional[typing.Callable] = None,
):
"""
Generates a multimethod.
Expand Down Expand Up @@ -191,7 +183,16 @@ def set_backend(backend, coerce=False, only=False):
skip_backend: A context manager that allows skipping of backends.
set_global_backend: Set a single, global backend for a domain.
"""
return _SetBackendContext(backend, coerce, only)
try:
return backend.__ua_cache__["set", coerce, only]
except AttributeError:
backend.__ua_cache__ = {}
except KeyError:
pass

ctx = _SetBackendContext(backend, coerce, only)
backend.__ua_cache__["set", coerce, only] = ctx
return ctx


def skip_backend(backend):
Expand Down

0 comments on commit 39d2ff9

Please sign in to comment.