From 4c5b30e55fea9c62204e3652ddb2649ebf70ee42 Mon Sep 17 00:00:00 2001 From: MuchoRio Date: Sat, 10 Oct 2026 19:27:28 +0700 Subject: [PATCH] test(tools): distinguish bounding allocation efficiency via tracemalloc - Replace ambiguous length-only assertion with a peak memory check via tracemalloc. - Clarify test comments regarding character loop iterations and allocation behavior on large budgets. --- tests/test_output_store.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/test_output_store.py b/tests/test_output_store.py index 038d281b..d8a0f071 100644 --- a/tests/test_output_store.py +++ b/tests/test_output_store.py @@ -3,6 +3,7 @@ from __future__ import annotations import re +import tracemalloc import pytest @@ -214,13 +215,24 @@ def test_head_tail_large_line_count_lazy_split() -> None: assert dropped_bytes > 0 -def test_fast_bounding_does_not_thrash_on_huge_strings() -> None: - # 1 million characters: old character loop would do 1M iterations of char.encode(). - # Fast path should finish in milliseconds. - huge_text = "a" * 1_000_000 - prefix = _take_prefix(huge_text, 500) - suffix = _take_suffix(huge_text, 500) +def test_fast_bounding_does_not_thrash_on_large_budgets() -> None: + # When taking a large slice (e.g. 50,000 bytes), the previous character loop + # iterated through 50,000 characters, calling char.encode() and appending each + # to a list, resulting in high heap churn (~500 KB peak traced memory). + # The fast path encodes and slices in C, keeping peak memory tightly bounded. + huge_text = "a" * 100_000 + budget = 50_000 - assert len(prefix) == 500 - assert len(suffix) == 500 + tracemalloc.start() + try: + prefix = _take_prefix(huge_text, budget) + suffix = _take_suffix(huge_text, budget) + _current, peak = tracemalloc.get_traced_memory() + finally: + tracemalloc.stop() + + assert len(prefix) == budget + assert len(suffix) == budget + # Fast path avoids per-character list/bytes allocations (< 250 KB vs ~500 KB previously). + assert peak < 250_000