fix(format): fix WidthStrategy edge cases and reduce allocations#572
fix(format): fix WidthStrategy edge cases and reduce allocations#572
Conversation
Fix unresolved review issues from #569: GreedyFrequencyStrategy panic on short rows, ProportionalStrategy not distributing remaining width when totalDeficit is zero, and GreedyFrequencyStrategy not using full width when all columns fit. Also reduce allocations in transposedRows construction and adjustToSum by replacing iterator chains with direct loops. Fixes #570, fixes #571 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the table formatting logic by addressing several edge cases and improving performance. It resolves potential panics when handling short rows, ensures optimal use of available screen width across different strategies, and reduces memory allocations in the greedy width calculation, leading to a more stable and efficient display of tabular data. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Code Metrics Report📊 View detailed coverage report (available for 7 days)
Details | | main (41f4265) | #572 (67beccb) | +/- |
|---------------------|----------------|----------------|--------|
+ | Coverage | 70.4% | 70.6% | +0.1% |
| Files | 74 | 74 | 0 |
| Lines | 6921 | 6944 | +23 |
+ | Covered | 4879 | 4906 | +27 |
+ | Code to Test Ratio | 1:1.2 | 1:1.2 | +0.0 |
| Code | 16137 | 16154 | +17 |
+ | Test | 20769 | 20820 | +51 |
+ | Test Execution Time | 2m31s | 1m10s | -1m21s |Code coverage of files in pull request scope (89.1% → 90.9%)
Reported by octocov |
There was a problem hiding this comment.
Code Review
This pull request provides excellent improvements by fixing several edge cases and optimizing performance. The changes correctly address a panic risk with short rows and a bug that prevented the full screen width from being utilized. The refactoring to reduce allocations and improve readability by replacing complex iterator chains with direct slice manipulation is a significant enhancement. I've identified one additional critical edge case where an empty headers slice could cause a panic in GreedyFrequencyStrategy and have left a comment with a suggested fix, aligning with our rule on defensive programming for edge cases.
Prevent index out of range panic when headers is empty, consistent with the guard already present in MarginalCostStrategy. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the column width calculation strategies to fix several issues. It introduces a bounds check in GreedyFrequencyStrategy to prevent a panic when processing rows shorter than the header. It also modifies both GreedyFrequencyStrategy and ProportionalStrategy to ensure that any remaining available screen width is always distributed among columns, fixing a bug where it could be lost. The changes also include performance optimizations, such as reducing allocations by replacing iterator chains with direct slice manipulation and avoiding repeated calculations in adjustToSum. New tests have been added to cover the fixed edge cases.
|
/gemini summary |
Summary of ChangesThis pull request significantly refines the table formatting logic, focusing on robustness and efficiency. It resolves critical edge cases that could lead to panics, ensures that available screen width is always fully utilized across different width calculation strategies, and introduces performance improvements by reducing memory allocations. These changes contribute to a more stable and optimized display of tabular data within the application. Highlights
Changelog
Activity
|
Summary
Fix unresolved review issues from #569 and reduce allocations in GreedyFrequencyStrategy. Addresses two bugs (panic risk, unused screen width) and one performance improvement.
Key Changes
lo.Must(lo.Nth(in, columnIdx))with direct index access + bounds check to prevent panic on short rows. Replacehiter.Map/hiter.Concat/slices.Collectchain intransposedRowswith direct slice construction. Replacelo.Sumwith inlinesumWidthsto avoid evaluation on everyslog.Debugcall. Always distribute remainder (fall back to column 0 instead of skipping when idx == -1).totalDeficit == 0(all columns already fit).adjustToSumto compute sorted unique thresholds once and inline clipping + sum. Remove unusedclipToMaxfunction.TestGreedyFrequencyStrategy_ShortRows,TestProportionalStrategy_ZeroDeficit,TestGreedyFrequencyStrategy_FullWidthedge case tests.Benchmark
Large (10 cols x 200 rows) Greedy allocations: 8865 → 8492 allocs/op.
Test Plan
make checkpasses (test + lint + fmt-check)Fixes #570, fixes #571