Skip to content

Conversation

@mohasarc
Copy link
Contributor

This PR optimizes string construction in directive.py, process.py, and event.py by replacing repeated += concatenation inside loops with list accumulation and ''.join().

The Issue: In Python, strings are immutable. Using += to append to a string inside a loop forces the interpreter to create a new string object and copy the old content for every iteration. This results in quadratic time complexity O(n^2), which can significantly degrade performance when processing large inputs.

The Solution: I refactored the string construction logic to collect substrings in a list and use ''.join() at the end. This approach ensures linear time complexity O(n) because the total size of the final string is calculated once, and memory is allocated efficiently.

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

@mondeja mondeja added enhancement New feature or request refactor labels Jan 25, 2026
Copy link
Owner

@mondeja mondeja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and it's ~4% faster. Thank you!

@mondeja mondeja merged commit 39df609 into mondeja:master Jan 25, 2026
18 of 19 checks passed
@mohasarc
Copy link
Contributor Author

Glad to hear that :)

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

Labels

enhancement New feature or request refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants