Skip to content

Commit 392088a

Browse files
committed
remove fn min_max in inftrees.rs
turns out this function was sort of fully unrolled, emitting ~200 instructions ?!
1 parent 5422149 commit 392088a

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed

zlib-rs/src/inflate/inftrees.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@ pub(crate) enum CodeType {
1010

1111
const MAX_BITS: usize = 15;
1212

13-
fn min_max<const N: usize>(count: [u16; N]) -> (usize, usize) {
14-
let mut max = MAX_BITS;
15-
while max >= 1 {
16-
if count[max] != 0 {
17-
break;
18-
}
19-
max -= 1;
20-
}
21-
22-
let mut min = 1;
23-
while min < max {
24-
if count[min] != 0 {
25-
break;
26-
}
27-
min += 1;
28-
}
29-
30-
(min, max)
31-
}
32-
3313
/// Length codes 257..285 base
3414
const LBASE: [u16; 31] = [
3515
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
@@ -70,16 +50,15 @@ pub(crate) fn inflate_table(
7050
// number of codes of each length
7151
let mut count = [0u16; MAX_BITS + 1];
7252

73-
for len in lens[0..codes].iter().copied() {
74-
count[len as usize] += 1;
53+
let (mut min, mut max) = (MAX_BITS, 0);
54+
for &len in &lens[0..codes] {
55+
if len > 0 {
56+
count[len as usize] += 1;
57+
max = Ord::max(max, usize::from(len));
58+
min = Ord::min(min, usize::from(len));
59+
}
7560
}
7661

77-
let mut root = bits;
78-
79-
let (min, max) = min_max(count);
80-
root = Ord::min(root, max);
81-
root = Ord::max(root, min);
82-
8362
if max == 0 {
8463
// no symbols to code at all
8564
let code = Code {
@@ -94,6 +73,9 @@ pub(crate) fn inflate_table(
9473
return InflateTable::Success { root: 1, used: 2 };
9574
}
9675

76+
// NOTE: if max != 0, then min has been set to a value <= max.
77+
let root = bits.clamp(min, max);
78+
9779
/* check for an over-subscribed or incomplete set of lengths */
9880
let mut left = 1i32;
9981
let mut len = 1;

0 commit comments

Comments
 (0)