Skip to content

Commit

Permalink
Add cache to _get_dimensionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Nov 3, 2023
1 parent f4af00d commit c017637
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

from ... import pint_eval
from ..._vendor import appdirs
from ...cache import cache as methodcache
from ...compat import babel_parse, TypeAlias, Self
from ...errors import DimensionalityError, RedefinitionError, UndefinedUnitError
from ...pint_eval import build_eval_tree
Expand Down Expand Up @@ -695,20 +696,14 @@ def get_dimensionality(self, input_units: UnitLike) -> UnitsContainer:

return self._get_dimensionality(input_units)

@methodcache
def _get_dimensionality(
self, input_units: Optional[UnitsContainer]
) -> UnitsContainer:
"""Convert a UnitsContainer to plain dimensions."""
if not input_units:
return self.UnitsContainer()

cache = self._cache.dimensionality

try:
return cache[input_units]
except KeyError:
pass

accumulator: dict[str, int] = defaultdict(int)
self._get_dimensionality_recurse(input_units, 1, accumulator)

Expand All @@ -717,8 +712,6 @@ def _get_dimensionality(

dims = self.UnitsContainer({k: v for k, v in accumulator.items() if v != 0})

cache[input_units] = dims

return dims

def _get_dimensionality_recurse(
Expand Down

0 comments on commit c017637

Please sign in to comment.