Skip to content

Commit 846c9aa

Browse files
authored
Apply lru_cache manually per LazyReferenceMapper instance (#1985)
1 parent fa850c1 commit 846c9aa

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

fsspec/implementations/reference.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ def __init__(
157157
if self.engine == "pyarrow" and find_spec("pyarrow") is None:
158158
raise ImportError("engine choice `pyarrow` is not installed.")
159159

160+
# Apply `lru_cache` decorator manually per instance.
161+
# This way `self` reference is not held on class level.
162+
# WARNING: However, this means that self and its members are not reflected
163+
# in the cache key, so we expect they won't be mutated once a value is cached.
164+
self.listdir = lru_cache()(self.listdir)
165+
self._key_to_record = lru_cache(maxsize=4096)(self._key_to_record)
166+
160167
def __getattr__(self, item):
161168
if item in ("_items", "record_size", "zmetadata"):
162169
self.setup()
@@ -219,7 +226,6 @@ def create(root, storage_options=None, fs=None, record_size=10000, **kwargs):
219226
fs.pipe("/".join([root, ".zmetadata"]), json.dumps(met).encode())
220227
return LazyReferenceMapper(root, fs, **kwargs)
221228

222-
@lru_cache
223229
def listdir(self):
224230
"""List top-level directories"""
225231
dirs = (p.rsplit("/", 1)[0] for p in self.zmetadata if not p.startswith(".z"))
@@ -331,7 +337,6 @@ def _load_one_key(self, key):
331337
# URL, offset, size
332338
return selection[:3]
333339

334-
@lru_cache(4096)
335340
def _key_to_record(self, key):
336341
"""Details needed to construct a reference for one key"""
337342
field, chunk = key.rsplit("/", 1)

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ select = [
189189
ignore = [
190190
# Loop control variable `loop` not used within loop body
191191
"B007",
192-
# Use of `functools.lru_cache` or `functools.cache` on methods can lead to memory leaks
193-
"B019",
194192
# Star-arg unpacking after a keyword argument is strongly discouraged
195193
"B026",
196194
# No explicit `stacklevel` keyword argument found

0 commit comments

Comments
 (0)