Skip to content

Commit

Permalink
Merge pull request #4 from jp9000/tail-end-optimization
Browse files Browse the repository at this point in the history
Fix stack overflow with tail-end optimization
  • Loading branch information
kornelski authored Jan 3, 2023
2 parents ce9c32c + 0422cac commit a77199a
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ fn usz(i: isize) -> usize {
i as usize
}

fn split(I: &mut [isize], V: &mut [isize], start: usize, len: usize, h: usize) {
struct SplitParams {
start: usize,
len: usize,
}

fn split_internal(
I: &mut [isize],
V: &mut [isize],
start: usize,
len: usize,
h: usize,
) -> Option<SplitParams> {
if len < 16 {
let mut k = start;
while k < start + len {
Expand All @@ -70,6 +81,8 @@ fn split(I: &mut [isize], V: &mut [isize], start: usize, len: usize, h: usize) {
}
k += j;
}

None
} else {
let x = V[usz(I[start + len / 2] + h as isize)];
let mut jj = 0;
Expand Down Expand Up @@ -116,12 +129,25 @@ fn split(I: &mut [isize], V: &mut [isize], start: usize, len: usize, h: usize) {
if jj == kk - 1 {
I[jj] = -1;
}

if start + len > kk {
split(I, V, kk, start + len - kk, h);
Some(SplitParams {
start: kk,
len: start + len - kk,
})
} else {
None
}
}
}

fn split(I: &mut [isize], V: &mut [isize], start: usize, len: usize, h: usize) {
let mut ret = Some(SplitParams { start, len });
while let Some(params) = ret {
ret = split_internal(I, V, params.start, params.len, h);
}
}

fn qsufsort(I: &mut [isize], V: &mut [isize], old: &[u8]) {
let mut buckets: [isize; 256] = [0; 256];
for &o in old {
Expand Down

0 comments on commit a77199a

Please sign in to comment.