Repository navigation
perf: optimize cell type inference, ANSI stripping, and multiline checks - #518
Open
millisdoe-glitch wants to merge 1 commit into
Open
millisdoe-glitch wants to merge 1 commit into
millisdoe-glitch wants to merge 1 commit into
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.
Summary
This PR optimizes performance bottlenecks in cell type deduction, ANSI code stripping, and multiline string checks during table formatting:
_isint():if type(string) is float: return Falsefast path.floatinstances enteredhasattr(string, "is_integer")(which evaluates toTrueon floats), triggeringstr(type(string))string creation and.startswith("<class 'numpy.int")checks on every single float cell in the table._type()&_isnumber_with_thousands_separator():_isnumber_with_thousands_separator(string)regex is evaluated at most once per non-integer cell (previously called twice in consecutiveelifbranches)._float_with_thousands_separators.match(string)directly instead of module-levelre.match(...)._strip_ansi():if "\x1b" not in s: return s(andb"\x1b"for bytestrings). Plain text cells (the vast majority of table inputs) skip_ansi_codes.sub()regex traversal completely (~5.3x faster for plain strings)._is_multiline():re.search(_multiline_codes, s)with fast C-level substring checks"\n" in s or "\r" in s(~10.9x faster)._afterpoint():string.lower().rfind("e")withmax(string.rfind("e"), string.rfind("E")), eliminating intermediate lowercase string allocations.Benchmark & Profiling
On a 1000x10 mixed string/numeric table (
tabulate(data, headers=headers, tablefmt="grid")):_type()cumulative time: reduced from 0.109s to 0.057s (47.7% faster)._column_type()cumulative time: reduced from 0.129s to 0.075s (41.8% faster).Verification
pytest test/).