Skip to content

Commit e4a4d10

Browse files
committed
Improve performance of view_summary
After porting summary.x to python, performance for calling `summary.x CASE` for summaries with thousands of keys become unreasonably bad. This PR fixes that.
1 parent 185b869 commit e4a4d10

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

python/tests/test_view_summary.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,18 @@ def test_that_restart_and_base_times_are_concated(capsys):
859859
4,5.0,06/01/2014,5.6299e+16,5.6299e+16,-99.0
860860
"""
861861
)
862+
863+
864+
@pytest.mark.usefixtures("use_tmpdir")
865+
@pytest.mark.timeout(10)
866+
def test_performance_with_many_keys(monkeypatch, benchmark):
867+
dir = Path("subdir")
868+
dir.mkdir()
869+
monkeypatch.chdir(dir)
870+
create_summary(case="TEST", summary_keys=[f"WWIT:N-{i}" for i in range(5000)])
871+
monkeypatch.chdir("..")
872+
873+
def bench():
874+
run(["summary.x", "-v", str(dir / "TEST"), "*"])
875+
876+
benchmark(bench)

python/view_summary/__main__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ def order_keys_by(self, patterns: list[str]) -> None:
427427
# to preserve backwards-compatibility
428428
already_matched = set()
429429
new_matched_keywords = []
430+
sort_until = None
430431
for pat in patterns:
431432
if "*" in pat:
432433
for i in range(len(self.matched_keywords)):
@@ -436,9 +437,7 @@ def order_keys_by(self, patterns: list[str]) -> None:
436437
if fnmatch.fnmatch(kw, pat):
437438
new_matched_keywords.append((kw, self.keyword_indices[i]))
438439
already_matched.add(kw)
439-
new_matched_keywords = natsorted(
440-
new_matched_keywords, key=lambda v: v[0]
441-
)
440+
sort_until = len(new_matched_keywords)
442441

443442
else:
444443
try:
@@ -450,6 +449,11 @@ def order_keys_by(self, patterns: list[str]) -> None:
450449
logger.warning(
451450
f"could not find variable: '{pat}' in summary file",
452451
)
452+
if sort_until is not None:
453+
new_matched_keywords = (
454+
natsorted(new_matched_keywords[:sort_until], key=lambda v: v[0])
455+
+ new_matched_keywords[sort_until:]
456+
)
453457
self.matched_keywords = [k for k, _ in new_matched_keywords]
454458
self.keyword_indices = np.array(
455459
[i for _, i in new_matched_keywords], dtype=np.int64

test_requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
pytest
2+
pytest-benchmark
3+
pytest-timeout
24
hypothesis
35
pydantic
46
typing_extensions

0 commit comments

Comments
 (0)