Skip to content

Commit

Permalink
CLN: use cache instead of lru_cache for unbound cache (#53462)
Browse files Browse the repository at this point in the history
  • Loading branch information
fangchenli authored May 31, 2023
1 parent a7eeca3 commit 11b297c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pandas/core/_numba/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pandas.compat._optional import import_optional_dependency


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_shared_aggregator(
func: Callable[..., Scalar],
nopython: bool,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/array_algos/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def take_2d_multi(
return out


@functools.lru_cache(maxsize=128)
@functools.lru_cache
def _get_take_nd_function_cached(
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: AxisInt
):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
return dtype, fill_value


@functools.lru_cache(maxsize=128)
@functools.lru_cache
def _maybe_promote_cached(dtype, fill_value, fill_value_type):
# The cached version of _maybe_promote below
# This also use fill_value_type as (unused) argument to use this in the
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/numba_.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def f(values, index, ...):
)


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_agg_func(
func: Callable[..., Scalar],
nopython: bool,
Expand Down Expand Up @@ -121,7 +121,7 @@ def group_agg(
return group_agg


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_transform_func(
func: Callable[..., np.ndarray],
nopython: bool,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def get_kind_from_how(cls, how: str) -> str:
# Note: we make this a classmethod and pass kind+how so that caching
# works at the class level and not the instance level
@classmethod
@functools.lru_cache(maxsize=None)
@functools.cache
def _get_cython_function(
cls, kind: str, how: str, dtype: np.dtype, is_numeric: bool
):
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/window/numba_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pandas._typing import Scalar


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_apply_func(
func: Callable[..., Scalar],
nopython: bool,
Expand Down Expand Up @@ -77,7 +77,7 @@ def roll_apply(
return roll_apply


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_ewm_func(
nopython: bool,
nogil: bool,
Expand Down Expand Up @@ -175,7 +175,7 @@ def ewm(
return ewm


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_table_func(
func: Callable[..., np.ndarray],
nopython: bool,
Expand Down Expand Up @@ -241,7 +241,7 @@ def roll_table(
# This function will no longer be needed once numba supports
# axis for all np.nan* agg functions
# https://github.com/numba/numba/issues/1269
@functools.lru_cache(maxsize=None)
@functools.cache
def generate_manual_numpy_nan_agg_with_axis(nan_func):
if TYPE_CHECKING:
import numba
Expand All @@ -259,7 +259,7 @@ def nan_agg_with_axis(table):
return nan_agg_with_axis


@functools.lru_cache(maxsize=None)
@functools.cache
def generate_numba_ewm_table_func(
nopython: bool,
nogil: bool,
Expand Down
11 changes: 4 additions & 7 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"""
from __future__ import annotations

from functools import (
lru_cache,
reduce,
)
import functools
import itertools
import re
from typing import (
Expand Down Expand Up @@ -193,10 +190,10 @@ def __init__(self, inherited: str | None = None) -> None:
self.inherited = self.compute_css(inherited)
else:
self.inherited = None
# We should avoid lru_cache on the __call__ method.
# We should avoid cache on the __call__ method.
# Otherwise once the method __call__ has been called
# garbage collection no longer deletes the instance.
self._call_cached = lru_cache(maxsize=None)(self._call_uncached)
self._call_cached = functools.cache(self._call_uncached)

compute_css = CSSResolver()

Expand Down Expand Up @@ -726,7 +723,7 @@ def _format_header(self) -> Iterable[ExcelCell]:
row = [x if x is not None else "" for x in self.df.index.names] + [
""
] * len(self.columns)
if reduce(lambda x, y: x and y, (x != "" for x in row)):
if functools.reduce(lambda x, y: x and y, (x != "" for x in row)):
gen2 = (
ExcelCell(self.rowcounter, colindex, val, self.header_style)
for colindex, val in enumerate(row)
Expand Down

0 comments on commit 11b297c

Please sign in to comment.