@@ -133,12 +133,41 @@ struct Scratch {
133133 batch_indices : Vec < u32 > ,
134134}
135135
136+ /// A scratch buffer keeps its capacity after a batch unless that capacity is
137+ /// more than this many entries and more than [`SCRATCH_RETAIN_RATIO`] times
138+ /// what the batch used. Then it shrinks to the smaller of the two. This stops
139+ /// one unusually large batch from holding memory for all the batches after it.
140+ const MAX_RETAINED_SCRATCH_ENTRIES : usize = 64 * 1024 ;
141+
142+ /// See [`MAX_RETAINED_SCRATCH_ENTRIES`]
143+ const SCRATCH_RETAIN_RATIO : usize = 4 ;
144+
136145impl Scratch {
137146 fn allocated_size ( & self ) -> usize {
138147 self . groups_with_rows . allocated_size ( )
139148 + self . offsets . allocated_size ( )
140149 + self . batch_indices . allocated_size ( )
141150 }
151+
152+ /// Shrink each buffer that is much larger than the batch that just used
153+ /// it. Call this after the batch, before the buffers are cleared for the
154+ /// next batch.
155+ fn release_oversized ( & mut self ) {
156+ fn release_if_oversized < T > ( buffer : & mut Vec < T > ) {
157+ let keep = SCRATCH_RETAIN_RATIO * buffer. len ( ) ;
158+ if buffer. capacity ( ) > MAX_RETAINED_SCRATCH_ENTRIES
159+ && buffer. capacity ( ) > keep
160+ {
161+ // The batch is done with the contents, so clear them first
162+ // and the shrink has nothing to copy
163+ buffer. clear ( ) ;
164+ buffer. shrink_to ( MAX_RETAINED_SCRATCH_ENTRIES . min ( keep) ) ;
165+ }
166+ }
167+ release_if_oversized ( & mut self . groups_with_rows ) ;
168+ release_if_oversized ( & mut self . offsets ) ;
169+ release_if_oversized ( & mut self . batch_indices ) ;
170+ }
142171}
143172
144173/// Maximum number of prepared group inputs retained while timing an
@@ -275,6 +304,7 @@ impl GroupsAccumulatorAdapter {
275304 opt_filter,
276305 f,
277306 ) ;
307+ scratch. release_oversized ( ) ;
278308 self . adjust_allocation ( scratch_size_pre, scratch. allocated_size ( ) ) ;
279309 self . scratch = scratch;
280310 result
@@ -1255,6 +1285,49 @@ mod tests {
12551285 Ok ( ( ) )
12561286 }
12571287
1288+ /// One large batch must not make the adapter keep its scratch capacity
1289+ /// for the smaller batches after it.
1290+ #[ test]
1291+ fn adapter_releases_oversized_scratch ( ) -> Result < ( ) > {
1292+ const NUM_GROUPS : usize = 2 * MAX_RETAINED_SCRATCH_ENTRIES ;
1293+ let mut adapter = GroupsAccumulatorAdapter :: new ( || {
1294+ Ok ( Box :: new ( MaxAccumulator :: try_new ( & DataType :: Int64 ) ?)
1295+ as Box < dyn Accumulator > )
1296+ } ) ;
1297+
1298+ // every row is a different group, so the scratch buffers need one
1299+ // entry for each row
1300+ let group_indices: Vec < usize > = ( 0 ..NUM_GROUPS ) . collect ( ) ;
1301+ let values: ArrayRef =
1302+ Arc :: new ( Int64Array :: from_iter_values ( 0 ..NUM_GROUPS as i64 ) ) ;
1303+ adapter. update_batch ( & [ values] , & group_indices, None , NUM_GROUPS ) ?;
1304+ let large_scratch = adapter. scratch . allocated_size ( ) ;
1305+ assert ! ( large_scratch >= NUM_GROUPS * size_of:: <usize >( ) ) ;
1306+ let size_after_large_batch = adapter. size ( ) ;
1307+
1308+ // a batch with one row shrinks each large buffer to
1309+ // SCRATCH_RETAIN_RATIO times what it used: one group, two offsets and
1310+ // one row index
1311+ let values: ArrayRef = Arc :: new ( Int64Array :: from ( vec ! [ 1 ] ) ) ;
1312+ adapter. update_batch ( & [ values] , & [ 0 ] , None , NUM_GROUPS ) ?;
1313+ let small_scratch = adapter. scratch . allocated_size ( ) ;
1314+ assert ! ( small_scratch < large_scratch) ;
1315+ assert_eq ! (
1316+ adapter. scratch. groups_with_rows. capacity( ) ,
1317+ SCRATCH_RETAIN_RATIO
1318+ ) ;
1319+ assert_eq ! ( adapter. scratch. offsets. capacity( ) , 2 * SCRATCH_RETAIN_RATIO ) ;
1320+ assert_eq ! (
1321+ adapter. scratch. batch_indices. capacity( ) ,
1322+ SCRATCH_RETAIN_RATIO
1323+ ) ;
1324+ assert_eq ! (
1325+ adapter. size( ) ,
1326+ size_after_large_batch - large_scratch + small_scratch
1327+ ) ;
1328+ Ok ( ( ) )
1329+ }
1330+
12581331 #[ test]
12591332 fn adapter_preserving_evaluation_uses_accumulator_contract ( ) -> Result < ( ) > {
12601333 let mut accumulator = GroupsAccumulatorAdapter :: new ( || {
0 commit comments