Skip to content

Conversation

@MaxFangX
Copy link
Contributor

@MaxFangX MaxFangX commented Nov 3, 2025

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 i = 1, #output do
    local char = output:sub(i, i)  -- Creates a new 1-char string
    -- ... accumulate in table ...
end

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.

@MaxFangX MaxFangX force-pushed the 2025-11-03-fix-string-parsing branch 2 times, most recently from d46a687 to 4f6afab Compare November 4, 2025 12:49
@MaxFangX MaxFangX force-pushed the 2025-11-03-fix-string-parsing branch from 4f6afab to 5cc0b9e Compare November 4, 2025 15:27
@tanvirtin tanvirtin merged commit 9f52b9d into tanvirtin:main Nov 10, 2025
1 of 2 checks passed
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.

2 participants