77//! narrowing `f64`/`f32` through `f16`) halves or quarters raw storage and lets child float
88//! schemes such as `ALP` and `ALPRD` operate at the narrower width without losing a single bit.
99
10+ use std:: sync:: OnceLock ;
11+
1012use vortex_array:: ArrayId ;
1113use vortex_array:: ArrayRef ;
1214use vortex_array:: Canonical ;
@@ -41,8 +43,12 @@ use crate::schemes::float::FloatRLEScheme;
4143#[ derive( Debug , Clone , Copy ) ]
4244struct NarrowestFloatTarget ( Option < PType > ) ;
4345
46+ /// Cached full-array compressed output from estimation when the array is too short to sample,
47+ /// reused directly by [`FloatWidenScheme::compress`] to avoid compressing the array twice.
48+ struct CachedFullWiden ( OnceLock < ArrayRef > ) ;
49+
4450const SAMPLE_CHUNK_LEN : usize = 1024 ;
45- const SAMPLE_CHUNK_COUNT : u32 = 10 ;
51+ const SAMPLE_CHUNK_COUNT : u32 = 2 ;
4652
4753fn compress_with_target (
4854 compressor : & CascadingCompressor ,
@@ -182,11 +188,11 @@ impl Scheme for FloatWidenScheme {
182188 // `f32`-only outlier.
183189 CompressionEstimate :: Deferred ( DeferredEstimate :: Callback ( Box :: new (
184190 move |compressor, data, _best_so_far, compress_ctx, exec_ctx| {
185- let ( sample, sample_ctx) =
191+ let ( sample, sample_ctx, is_full_array ) =
186192 match sample_aligned_chunks ( data. array ( ) , SAMPLE_CHUNK_LEN , SAMPLE_CHUNK_COUNT )
187193 {
188- Some ( sample) => ( sample, compress_ctx. with_sampling ( ) ) ,
189- None => ( data. array ( ) . clone ( ) , compress_ctx) ,
194+ Some ( sample) => ( sample, compress_ctx. with_sampling ( ) , false ) ,
195+ None => ( data. array ( ) . clone ( ) , compress_ctx, true ) ,
190196 } ;
191197 let sample_before = uncompressed_size_in_bytes ( & sample, exec_ctx) ?;
192198 if sample_before == 0 {
@@ -196,15 +202,22 @@ impl Scheme for FloatWidenScheme {
196202 let sample_mask = sample_primitive
197203 . validity ( ) ?
198204 . execute_mask ( sample_primitive. len ( ) , exec_ctx) ?;
199- let sample_after = compress_with_target (
205+ let compressed = compress_with_target (
200206 compressor,
201207 & sample_primitive,
202208 & sample_mask,
203209 narrowest,
204210 sample_ctx,
205211 exec_ctx,
206- ) ?
207- . nbytes ( ) ;
212+ ) ?;
213+ let sample_after = compressed. nbytes ( ) ;
214+ if is_full_array {
215+ drop (
216+ data. get_or_insert_with ( || CachedFullWiden ( OnceLock :: new ( ) ) )
217+ . 0
218+ . set ( compressed) ,
219+ ) ;
220+ }
208221 if sample_after == 0 {
209222 return Ok ( EstimateVerdict :: Skip ) ;
210223 }
@@ -222,6 +235,14 @@ impl Scheme for FloatWidenScheme {
222235 compress_ctx : CompressorContext ,
223236 exec_ctx : & mut ExecutionCtx ,
224237 ) -> VortexResult < ArrayRef > {
238+ if let Some ( cached) = data
239+ . get_or_insert_with ( || CachedFullWiden ( OnceLock :: new ( ) ) )
240+ . 0
241+ . get ( )
242+ {
243+ return Ok ( cached. clone ( ) ) ;
244+ }
245+
225246 let primitive = data. array_as_primitive ( ) . into_owned ( ) ;
226247 let mask = primitive
227248 . validity ( ) ?
0 commit comments