Skip to content

Perf: Optimize string construction in _find_and_replace#27

Open
mohasarc wants to merge 1 commit intoJDArmy:mainfrom
mohasarc:perf/string-concat
Open

Perf: Optimize string construction in _find_and_replace#27
mohasarc wants to merge 1 commit intoJDArmy:mainfrom
mohasarc:perf/string-concat

Conversation

@mohasarc
Copy link

This PR optimizes _find_and_replace by replacing incremental string concatenation (wm_text += ...) with list accumulation and .join().

The Issue: Using += inside the loop forces quadratic time complexity O(N^2) for string construction because Python creates a new string object for every iteration.

The Solution: I refactored the code to accumulate segments in a list (wm_parts) and join them once at the end. This ensures linear time complexity O(N) for memory allocation without altering the underlying search logic.

This is in line with standard python performance recommendation: https://peps.python.org/pep-0008/#programming-recommendations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant