Spawn: Fix O(N²) string parsing #424
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes a performance issue in
Spawn:parse_result()that was creating excessive string allocations when parsing git command output.The previous implementation used character-by-character iteration:
For large git output (e.g., git ls-files returning 20KB), this created ~20,000 individual string objects. While the algorithmic complexity is O(N), the constant factor from N allocations + table operations made this unnecessarily slow.
This PR replaces it with efficient newline-based splitting using string.find(): local newline_pos = output:find('\n', start, true) callback(output:sub(start, newline_pos - 1))
This creates only one substring per line (not per character), dramatically reducing allocations.
Testing with a 932-file repository showed git ls-files parsing improved from 796ms → 594ms (25% improvement), and overall stage operations improved from 1605ms → 1201ms. Existing functionality unchanged - all git command parsing works as before.