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 b1d59a1
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions uarray/_backend.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
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

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 +94,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 +182,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 All @@ -210,7 +210,16 @@ def skip_backend(backend):
set_backend: A context manager that allows setting of backends.
set_global_backend: Set a single, global backend for a domain.
"""
return _SkipBackendContext(backend)
try:
return backend.__ua_cache__["skip"]
except AttributeError:
backend.__ua_cache__ = {}
except KeyError:
pass

ctx = _SkipBackendContext(backend)
backend.__ua_cache__["skip"] = ctx
return ctx


def get_defaults(f):
Expand Down

0 comments on commit b1d59a1

Please sign in to comment.