Skip to content

Commit

Permalink
Micro-optimization for LazyJsonlIterator len() (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzelasko committed Dec 14, 2023
1 parent 9e3c6fe commit 8cca852
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lhotse/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ def __init__(self, path: Pathlike) -> None:
self._len = None

def __iter__(self):
tot = 0
with open_best(self.path, "r") as f:
for line in f:
data = decode_json_line(line)
yield data
tot += 1
if self._len is None:
self._len = tot

def __len__(self) -> int:
if self._len is None:
Expand Down
16 changes: 16 additions & 0 deletions test/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest

from lhotse import CutSet, FeatureSet, RecordingSet, SupervisionSet, combine
from lhotse.lazy import LazyJsonlIterator
from lhotse.testing.dummies import DummyManifest, as_lazy
from lhotse.utils import fastcopy, is_module_available

Expand Down Expand Up @@ -248,3 +249,18 @@ def test_dillable():
"dummy-mono-cut-0000-random-suffix",
"dummy-mono-cut-0001-random-suffix",
]


def test_lazy_jsonl_iterator_caches_len():
cuts = DummyManifest(CutSet, begin_id=0, end_id=200)
expected_len = 200
with as_lazy(cuts) as cuts_lazy:
path = cuts_lazy.data.path
print(path)
it = LazyJsonlIterator(path)
assert it._len is None
for _ in it:
pass
assert it._len is not None
assert it._len == expected_len
assert len(it) == expected_len

0 comments on commit 8cca852

Please sign in to comment.