From 405caa1999ff7f86718d5aaff3952300ad8c7d27 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 1 Oct 2025 09:05:39 +0000 Subject: [PATCH] Optimize _addindent The optimization achieves a **22% speedup** by eliminating redundant operations and reducing the number of intermediate variables: **Key Changes:** 1. **Pre-compute the indent string once**: `indent = " " * num_spaces` is calculated upfront instead of repeating `num_spaces * " "` for every line in the list comprehension. 2. **Single-pass join operation**: Instead of mutating the list with `pop(0)`, creating a new list, joining it, and then concatenating with the first line, the optimized version directly constructs the final result with `"\n".join([s[0]] + [indent + line for line in s[1:]])` 3. **Eliminate intermediate variables**: Removes the need for `first` and multiple reassignments to `s`. **Why it's faster:** - **Reduced string operations**: The original code performs `num_spaces * " "` multiplication for each line (49.2% of total time), while the optimized version does it once. - **Fewer list operations**: Eliminates the `pop(0)` operation and intermediate list reassignment. - **Direct construction**: Builds the final result in one operation instead of multiple concatenations. **Performance characteristics from tests:** - **Large-scale improvements**: Shows significant gains with many lines (23.6% faster for 1000 lines, 63.1% faster for 1000 empty lines) - **Best for multi-line strings**: Most effective when `len(s) > 1`, with minimal impact on single-line cases - **Scales well with line count**: Performance improvement increases with more lines to indent --- doctr/utils/repr.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/doctr/utils/repr.py b/doctr/utils/repr.py index 798fccd0c9..4318f0ec2c 100644 --- a/doctr/utils/repr.py +++ b/doctr/utils/repr.py @@ -14,11 +14,8 @@ def _addindent(s_, num_spaces): # don't do anything for single-line stuff if len(s) == 1: return s_ - first = s.pop(0) - s = [(num_spaces * " ") + line for line in s] - s = "\n".join(s) - s = first + "\n" + s - return s + indent = " " * num_spaces + return "\n".join([s[0]] + [indent + line for line in s[1:]]) class NestedObject: