-
Notifications
You must be signed in to change notification settings - Fork 1
perf: make table width fitting width-aware #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| NDJSON_SCHEMA_VERSION, | ||
| NdjsonWriter, | ||
| OutputFormatError, | ||
| _fit_table_width, | ||
| render_document, | ||
| render_records, | ||
| resolve_output_format, | ||
|
|
@@ -140,6 +141,17 @@ def test_terminal_table_uses_display_width_and_deterministic_truncation(self) -> | |
| self.assertIn("…", output) | ||
| self.assertLessEqual(max(len(line) for line in output.splitlines()), 20) | ||
|
|
||
| def test_table_width_fitting_handles_large_widths_without_per_unit_loop(self) -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test gap: this test's name implies it verifies the fix avoids a per-unit loop, but it only asserts final output values ( |
||
| widths = [10_000_000, 9_000_000, 8_000_000, 7_000_000] | ||
| work = [0] | ||
|
|
||
| fitted = _fit_table_width(widths, terminal_width=120, _work_counter=work) | ||
|
|
||
| self.assertEqual(sum(fitted), 114) | ||
| self.assertGreaterEqual(min(fitted), 1) | ||
| self.assertEqual(fitted, [28, 28, 29, 29]) | ||
| self.assertLessEqual(work[0], len(widths) * 3) | ||
|
|
||
| def test_terminal_width_and_cell_width_validate_inputs(self) -> None: | ||
| with self.assertRaisesRegex(ValueError, "terminal_width"): | ||
| render_records( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup: this replacement algorithm (level/position/active_count group bookkeeping) is substantially more intricate than the original 8-line greedy loop but has no comment explaining its invariants — e.g. that
orderis a snapshot taken before mutation, or whynext_levelis floored to 1 only whenpositionruns out. I fuzz-tested this against a reimplementation of the old algorithm across ~27,000 random/adversarial cases with zero mismatches, so the logic itself is sound — but a future contributor adjusting width-fitting (a per-column floor, different tie-break order) is likely to break an invariant that's only implicit here.