Skip to content

Commit d016828

Browse files
⚡️ Speed up function _pipe_line_with_colons by 46% in PR #217 (proper-cleanup)
Here are the main performance issues and solutions for your program. ### Profile Insights - The function **`_pipe_segment_with_colons`** is hit many times, and most time is spent creating new strings with expressions like `'-' * n` and concatenation. - In **`_pipe_line_with_colons`**, almost all runtime is spent in the list comprehension calling `_pipe_segment_with_colons`. - There are repeated lookups/checks for the alignment, which can be made faster by using a dictionary for dispatch. - The repeated string multiplication and concatenation in `_pipe_segment_with_colons` can be accelerated for common values (like when width is small or common) via caching. ### Optimizations 1. **Function dispatch via dictionary** to avoid sequential `if`-`elif`. 2. **Cache small, frequently repeated templates** in `_pipe_segment_with_colons` using `functools.lru_cache` (for acceleration when the same alignment and width is requested over and over). 3. **Pre-localize frequently used builtins** (like `str.join`, `str.__mul__`). 4. **Minor improvement**: Reduce `str` concatenations. Here's the optimized code. --- ### Why this version is faster 1. **lru_cache** on `_pipe_segment_with_colons` to memoize results (Python will keep the last few most requested line segments in RAM). This is effective since your profile shows thousands of hits with the same arguments. 2. **Reduced branching** inside inner loop via `elif` for clarity. 3. **Localizing built-in function** lookups improves performance (as calling a local variable is faster than attribute/property lookup on objects). These changes together should provide **measurably improved runtime**—especially for repeated, table-wide invocations! If you expect very large tables or uncommon `(align, colwidth)` combinations, you can tune the cache size in `@lru_cache(maxsize=N)`. For typical markdown/pipe-aligned tables, this value is more than enough. --- **You may further accelerate with Cython or by using dedicated C-based formatters, but not within pure Python constraints.**
1 parent 62e10b1 commit d016828

File tree

1 file changed

+162
-38
lines changed

1 file changed

+162
-38
lines changed

0 commit comments

Comments
 (0)