Optimize table formatting hot paths#435
Open
CodingFeng101 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
This trims two small pieces of repeated work from formatting hot paths:
.match(...)instead of routing every check throughre.match(...)_flat_list(...)when aligning single-line columns, where measured widths are already a flat list of integersBoth changes preserve the existing behavior and keep the multiline path unchanged.
Why
For larger tables, type inference calls
_isnumber_with_thousands_separator()for many cells, and column alignment runs once per rendered column. Avoiding per-cell regex dispatch overhead and unnecessary single-line width flattening reduces work in the common table-rendering path.Benchmark
Measured on Windows / Python 3.12 by loading
upstream/masterand this branch in the same process withtimeit.repeat:2.429s->1.310s(~46% faster)_align_column, 5,000 cells x 200: best0.276s->0.240s(~13% faster)tabulate(..., tablefmt="plain"), 5,000 rows x 10: best1.249s->1.124s(~10% faster)Validation
PYTHONPATH=. python -m pytest test\test_internal.py::test_align_column_decimal_with_thousand_separators test\test_internal.py::test_align_column_decimal_with_incorrect_thousand_separators test\test_output.py::test_floatfmt_thousands test\test_regression.py::test_alignment_of_decimal_numbers_with_commas -qPYTHONPATH=. python -m pytest -q-> 382 passed, 1 skippedpython -m ruff check tabulate\__init__.pypython -m ruff format --check tabulate\__init__.pygit diff --check