Skip to content

Commit ee7559e

Browse files
authored
Merge pull request #193 from nyurik/loop-hash-to-bin-tree
Simplify loop in StoreAndFindMatchesH10
2 parents 0fd919c + c4ee8b8 commit ee7559e

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

src/enc/backward_references/hash_to_binary_tree.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ where
202202
}
203203
}
204204
impl<
205-
Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>,
205+
Alloc: Allocator<u16> + Allocator<u32>,
206206
Buckets: Allocable<u32, Alloc> + SliceWrapperMut<u32> + SliceWrapper<u32>,
207207
Params: H10Params,
208208
> CloneWithAlloc<Alloc> for H10<Alloc, Buckets, Params>
@@ -440,73 +440,74 @@ pub fn StoreAndFindMatchesH10<
440440
where
441441
Buckets: PartialEq<Buckets>,
442442
{
443-
let mut matches_offset = 0usize;
444-
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
445-
let max_comp_len: usize = min(max_length, 128usize);
443+
let mut matches_offset = 0_usize;
444+
let cur_ix_masked = cur_ix & ring_buffer_mask;
445+
let max_comp_len = min(max_length, 128);
446446
let should_reroot_tree = max_length >= 128;
447447
let key = xself.HashBytes(&data[cur_ix_masked..]);
448-
let forest: &mut [u32] = xself.forest.slice_mut();
449-
let mut prev_ix: usize = xself.buckets_.slice()[key] as usize;
450-
let mut node_left: usize = LeftChildIndexH10!(xself, cur_ix);
451-
let mut node_right: usize = RightChildIndexH10!(xself, cur_ix);
452-
let mut best_len_left: usize = 0usize;
453-
let mut best_len_right: usize = 0usize;
454-
let mut depth_remaining: usize;
448+
let forest = xself.forest.slice_mut();
449+
let mut prev_ix = xself.buckets_.slice()[key] as usize;
450+
let mut node_left = LeftChildIndexH10!(xself, cur_ix);
451+
let mut node_right = RightChildIndexH10!(xself, cur_ix);
452+
let mut best_len_left = 0_usize;
453+
let mut best_len_right = 0_usize;
454+
let mut depth_remaining = 64_usize;
455+
455456
if should_reroot_tree {
456457
xself.buckets_.slice_mut()[key] = cur_ix as u32;
457458
}
458-
depth_remaining = 64usize;
459-
'break16: loop {
460-
{
461-
let backward: usize = cur_ix.wrapping_sub(prev_ix);
462-
let prev_ix_masked: usize = prev_ix & ring_buffer_mask;
463-
if backward == 0usize || backward > max_backward || depth_remaining == 0usize {
464-
if should_reroot_tree {
465-
forest[node_left] = xself.invalid_pos_;
466-
forest[node_right] = xself.invalid_pos_;
467-
}
468-
break 'break16;
459+
460+
loop {
461+
let backward = cur_ix.wrapping_sub(prev_ix);
462+
let prev_ix_masked = prev_ix & ring_buffer_mask;
463+
if backward == 0 || backward > max_backward || depth_remaining == 0 {
464+
if should_reroot_tree {
465+
forest[node_left] = xself.invalid_pos_;
466+
forest[node_right] = xself.invalid_pos_;
469467
}
470-
{
471-
let cur_len: usize = min(best_len_left, best_len_right);
468+
break;
469+
}
472470

473-
let len: usize = cur_len.wrapping_add(FindMatchLengthWithLimit(
474-
&data[cur_ix_masked.wrapping_add(cur_len)..],
475-
&data[prev_ix_masked.wrapping_add(cur_len)..],
476-
max_length.wrapping_sub(cur_len),
477-
));
478-
if matches_offset != matches.len() && (len > *best_len) {
479-
*best_len = len;
480-
BackwardMatchMut(&mut matches[matches_offset]).init(backward, len);
481-
matches_offset += 1;
482-
}
483-
if len >= max_comp_len {
484-
if should_reroot_tree {
485-
forest[node_left] = forest[LeftChildIndexH10!(xself, prev_ix)];
486-
forest[node_right] = forest[RightChildIndexH10!(xself, prev_ix)];
487-
}
488-
break 'break16;
489-
}
490-
if data[cur_ix_masked.wrapping_add(len)] as i32
491-
> data[prev_ix_masked.wrapping_add(len)] as i32
492-
{
493-
best_len_left = len;
494-
if should_reroot_tree {
495-
forest[node_left] = prev_ix as u32;
496-
}
497-
node_left = RightChildIndexH10!(xself, prev_ix);
498-
prev_ix = forest[node_left] as usize;
499-
} else {
500-
best_len_right = len;
501-
if should_reroot_tree {
502-
forest[node_right] = prev_ix as u32;
503-
}
504-
node_right = LeftChildIndexH10!(xself, prev_ix);
505-
prev_ix = forest[node_right] as usize;
506-
}
471+
let cur_len = min(best_len_left, best_len_right);
472+
473+
let len = cur_len.wrapping_add(FindMatchLengthWithLimit(
474+
&data[cur_ix_masked.wrapping_add(cur_len)..],
475+
&data[prev_ix_masked.wrapping_add(cur_len)..],
476+
max_length.wrapping_sub(cur_len),
477+
));
478+
479+
if matches_offset != matches.len() && len > *best_len {
480+
*best_len = len;
481+
BackwardMatchMut(&mut matches[matches_offset]).init(backward, len);
482+
matches_offset += 1;
483+
}
484+
485+
if len >= max_comp_len {
486+
if should_reroot_tree {
487+
forest[node_left] = forest[LeftChildIndexH10!(xself, prev_ix)];
488+
forest[node_right] = forest[RightChildIndexH10!(xself, prev_ix)];
507489
}
490+
break;
508491
}
492+
493+
if data[cur_ix_masked.wrapping_add(len)] > data[prev_ix_masked.wrapping_add(len)] {
494+
best_len_left = len;
495+
if should_reroot_tree {
496+
forest[node_left] = prev_ix as u32;
497+
}
498+
node_left = RightChildIndexH10!(xself, prev_ix);
499+
prev_ix = forest[node_left] as usize;
500+
} else {
501+
best_len_right = len;
502+
if should_reroot_tree {
503+
forest[node_right] = prev_ix as u32;
504+
}
505+
node_right = LeftChildIndexH10!(xself, prev_ix);
506+
prev_ix = forest[node_right] as usize;
507+
}
508+
509509
depth_remaining = depth_remaining.wrapping_sub(1);
510510
}
511+
511512
matches_offset
512513
}

0 commit comments

Comments
 (0)