@@ -92,6 +92,10 @@ pub(super) enum EstimateScore {
9292 /// A finite compression ratio.
9393 Finite ( f64 ) ,
9494 /// Trial compression produced a 0-byte output.
95+ ///
96+ /// This is treated as an unbounded score for scheme selection, but has no finite trace ratio.
97+ /// Final full-array compression still has to pass the normal before/after acceptance check, so
98+ /// a zero-byte sample can choose a scheme but cannot force a larger output to be returned.
9599 ZeroBytes ,
96100}
97101
@@ -112,6 +116,24 @@ impl EstimateScore {
112116 Self :: ZeroBytes => None ,
113117 }
114118 }
119+
120+ /// Returns whether this estimate is eligible to compete.
121+ fn is_valid ( self ) -> bool {
122+ match self {
123+ Self :: Finite ( ratio) => ratio. is_finite ( ) && !ratio. is_subnormal ( ) && ratio > 1.0 ,
124+ Self :: ZeroBytes => true ,
125+ }
126+ }
127+
128+ /// Returns whether this estimate beats another valid estimate.
129+ fn beats ( self , other : Self ) -> bool {
130+ match ( self , other) {
131+ ( Self :: ZeroBytes , Self :: Finite ( _) ) => true ,
132+ ( Self :: ZeroBytes , Self :: ZeroBytes ) => false ,
133+ ( Self :: Finite ( _) , Self :: ZeroBytes ) => false ,
134+ ( Self :: Finite ( ratio) , Self :: Finite ( best_ratio) ) => ratio > best_ratio,
135+ }
136+ }
115137}
116138
117139/// Winner estimate carried from scheme selection into result tracing.
@@ -138,20 +160,7 @@ pub(super) fn is_better_score(
138160 score : EstimateScore ,
139161 best : & Option < ( & ' static dyn Scheme , EstimateScore ) > ,
140162) -> bool {
141- match score {
142- EstimateScore :: ZeroBytes => {
143- best. is_none_or ( |( _, best_score) | !matches ! ( best_score, EstimateScore :: ZeroBytes ) )
144- }
145- EstimateScore :: Finite ( ratio) => {
146- ratio. is_finite ( )
147- && !ratio. is_subnormal ( )
148- && ratio > 1.0
149- && best. is_none_or ( |( _, best_score) | match best_score {
150- EstimateScore :: Finite ( best_ratio) => ratio > best_ratio,
151- EstimateScore :: ZeroBytes => false ,
152- } )
153- }
154- }
163+ score. is_valid ( ) && best. is_none_or ( |( _, best_score) | score. beats ( best_score) )
155164}
156165
157166/// Estimates compression ratio by compressing a ~1% sample of the data.
0 commit comments