Skip to content

Commit b6a2590

Browse files
committed
refactor(core.utils): extract _iter_tmp_entries and _sum_tmp_sizes
The same scandir-then-stat-for-size loop appeared in three places (`_compute_total_size`, `_sweep_stale_tmp_files`, `_enforce_size_cap`). `_iter_tmp_entries` covers the scandir + is_file filter + context-managed cleanup that all three need. `_sum_tmp_sizes` covers the size accumulation that both `_compute_total_size` and `_enforce_size_cap` need. Net result: each callsite now reads as one obvious line instead of seven, and a future change to the temp-walk discipline (e.g. switching to a different scan strategy) lands in one place.
1 parent 8345735 commit b6a2590

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

cuda_core/cuda/core/utils/_program_cache/_file_stream.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,33 @@ def _compute_total_size(self) -> int:
613613
total += path.stat().st_size
614614
except FileNotFoundError:
615615
continue
616-
if self._tmp.exists():
617-
for tmp in self._tmp.iterdir():
618-
if not tmp.is_file():
619-
continue
620-
try:
621-
total += tmp.stat().st_size
622-
except FileNotFoundError:
623-
continue
616+
return total + self._sum_tmp_sizes()
617+
618+
def _iter_tmp_entries(self) -> Iterable[os.DirEntry]:
619+
# Mirror ``_iter_entry_paths``: scandir + cached d_type for the
620+
# file/dir filter + deterministic handle close on early exit.
621+
# Yields ``DirEntry`` (not Path) so callers can use ``entry.stat``
622+
# / ``entry.path`` directly without an extra wrap.
623+
try:
624+
with os.scandir(self._tmp) as it:
625+
yield from (entry for entry in it if entry.is_file(follow_symlinks=False))
626+
except FileNotFoundError:
627+
return
628+
629+
def _sum_tmp_sizes(self) -> int:
630+
"""Sum sizes of every file in ``tmp/``, skipping vanished entries.
631+
632+
Both ``_compute_total_size`` (open-time seed) and
633+
``_enforce_size_cap`` (eviction reconciliation) need this --
634+
temp files occupy disk too, so undercounting them would let
635+
bursts of in-flight writes silently exceed ``max_size_bytes``.
636+
"""
637+
total = 0
638+
for entry in self._iter_tmp_entries():
639+
try:
640+
total += entry.stat(follow_symlinks=False).st_size
641+
except FileNotFoundError:
642+
continue
624643
return total
625644

626645
def _sweep_stale_tmp_files(self) -> None:
@@ -630,15 +649,11 @@ def _sweep_stale_tmp_files(self) -> None:
630649
in-flight write from another process is not interrupted. Best
631650
effort: a missing file or a permission failure is ignored.
632651
"""
633-
if not self._tmp.exists():
634-
return
635652
cutoff = time.time() - _TMP_STALE_AGE_SECONDS
636-
for tmp in self._tmp.iterdir():
637-
if not tmp.is_file():
638-
continue
653+
for entry in self._iter_tmp_entries():
639654
try:
640-
if tmp.stat().st_mtime < cutoff:
641-
tmp.unlink()
655+
if entry.stat(follow_symlinks=False).st_mtime < cutoff:
656+
os.unlink(entry.path)
642657
except (FileNotFoundError, PermissionError):
643658
continue
644659

@@ -675,14 +690,7 @@ def _enforce_size_cap(self) -> None:
675690
# write-only churn (true LRU instead of FIFO).
676691
entries.append((st.st_atime, st.st_size, path, st))
677692
total += st.st_size
678-
if self._tmp.exists():
679-
for tmp in self._tmp.iterdir():
680-
if not tmp.is_file():
681-
continue
682-
try:
683-
total += tmp.stat().st_size
684-
except FileNotFoundError:
685-
continue
693+
total += self._sum_tmp_sizes()
686694
if total <= self._max_size_bytes:
687695
# Re-seed the tracker from the scan: catches drift from
688696
# cross-process writers/deleters that the per-write delta

0 commit comments

Comments
 (0)