Skip to content

Commit 8284da2

Browse files
gooncreeperandrewrk
authored andcommitted
flate.Compress: simplify huffman node comparisons
Instead of comparing each field, nodes are now compared as 32-bit values where `freq` is in the most significant bits.
1 parent d6931b0 commit 8284da2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/std/compress/flate/Compress.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,14 +993,15 @@ const huffman = struct {
993993
const max_leafs = 286;
994994
const max_nodes = max_leafs * 2;
995995

996-
const Node = struct {
997-
freq: u16,
996+
const Node = packed struct(u32) {
998997
depth: u16,
998+
freq: u16,
999999

10001000
pub const Index = u16;
10011001

1002+
/// `freq` is more significant than `depth`
10021003
pub fn smaller(a: Node, b: Node) bool {
1003-
return if (a.freq != b.freq) a.freq < b.freq else a.depth < b.depth;
1004+
return @as(u32, @bitCast(a)) < @as(u32, @bitCast(b));
10041005
}
10051006
};
10061007

0 commit comments

Comments
 (0)