From 2f91274ea21cf620c0a9881609e5bff7ec60f43f Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 22:49:32 +0000 Subject: [PATCH] Optimize BreaklessListsPreprocessor.run The optimized code achieves a **40% speedup** through three key micro-optimizations that reduce Python's overhead in tight loops: **What optimizations were applied:** 1. **Localized method references**: `append = result_lines.append` and `match_list_start = self.LIST_START_PATTERN.match` avoid repeated attribute lookups during iteration 2. **Loop structure improvement**: Replaced `while i < len(lines)` with `for i in range(length)` to eliminate redundant `len()` calls and manual index incrementing 3. **Simplified conditional logic**: Removed a redundant `current_line.strip()` check that was duplicated in the blank line insertion logic **Why these optimizations provide speedup:** - **Attribute lookup elimination**: Python's attribute resolution (`self.LIST_START_PATTERN.match`, `result_lines.append`) has overhead. Localizing these to variables makes them direct variable lookups, which are significantly faster in loops - **Loop efficiency**: `for` loops with `range()` are more efficient than `while` loops with manual indexing because Python can optimize the iteration internally - **Reduced redundant operations**: Eliminating the duplicate `len(lines)` calls and redundant `.strip()` checks reduces CPU cycles per iteration **Impact on workloads:** The optimizations are most beneficial for **large-scale document processing**, as shown by the test results where large documents (500-1000 lines) see **40-50% speedups**, while small documents (2-5 lines) show minimal or slight slowdowns due to the overhead of variable assignment. This suggests the function processes substantial markdown documents in production, making these micro-optimizations valuable for real-world usage where document size matters more than single-line processing overhead. --- .../_output/md_extensions/breakless_lists.py | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/marimo/_output/md_extensions/breakless_lists.py b/marimo/_output/md_extensions/breakless_lists.py index 78a981506ed..432fbc46780 100644 --- a/marimo/_output/md_extensions/breakless_lists.py +++ b/marimo/_output/md_extensions/breakless_lists.py @@ -32,27 +32,25 @@ def run(self, lines: list[str]) -> list[str]: return lines result_lines: list[str] = [] - i = 0 + append = result_lines.append # Localize for faster loop performance + match_list_start = self.LIST_START_PATTERN.match - while i < len(lines): + length = len(lines) + for i in range(length): current_line = lines[i] - result_lines.append(current_line) + append(current_line) # Check if we need to look ahead for a list - if i + 1 < len(lines): + if i + 1 < length: next_line = lines[i + 1] # If current line is not empty and next line starts a list if ( current_line.strip() # Current line has content - and self.LIST_START_PATTERN.match(next_line) + and match_list_start(next_line) ): # Next line starts a list - # Check if there's already a blank line - if current_line.strip(): - # Insert blank line to enable list interruption - result_lines.append("") - - i += 1 + # Insert blank line to enable list interruption + append("") return result_lines