Skip to content

fix(vm): eliminate tail recursion on larger partition in qsortEx - #629

Open
Huge wants to merge 1 commit into
vtereshkov:masterfrom
Huge:fix/qsort-recursion-depth
Open

Huge wants to merge 1 commit into
vtereshkov:masterfrom
Huge:fix/qsort-recursion-depth

Conversation

@Huge

@Huge Huge commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

In qsortEx, the function unconditionally recursed on both subarrays after partitioning. For already sorted or reverse-sorted dynamic arrays, this creates O(N) recursion depth on the C call stack, which can exhaust stack space and crash when sorting large arrays.

This patch updates qsortEx to recurse only on the smaller partition and iterate on the larger partition, guaranteeing that the maximum recursion depth is bounded by O(log N).

qsortEx unconditionally recursed on both subarrays after partitioning.
For already sorted or reverse-sorted dynamic arrays, this creates O(N)
recursion depth on the C call stack, which can exhaust stack space on
large arrays.

Recurse only on the smaller partition and iterate on the larger partition,
guaranteeing maximum recursion depth bounded by O(log N).

Reported-by: BS.ehlas.cz

This branch has not been deployed

No deployments
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