@@ -240,6 +240,7 @@ pub struct MemoryConsumer {
240240 name : String ,
241241 can_spill : bool ,
242242 id : usize ,
243+ parent_id : Option < usize > ,
243244}
244245
245246impl PartialEq for MemoryConsumer {
@@ -250,6 +251,7 @@ impl PartialEq for MemoryConsumer {
250251 if is_same_id {
251252 assert_eq ! ( self . name, other. name) ;
252253 assert_eq ! ( self . can_spill, other. can_spill) ;
254+ assert_eq ! ( self . parent_id, other. parent_id) ;
253255 }
254256
255257 is_same_id
@@ -263,6 +265,7 @@ impl Hash for MemoryConsumer {
263265 self . id . hash ( state) ;
264266 self . name . hash ( state) ;
265267 self . can_spill . hash ( state) ;
268+ self . parent_id . hash ( state) ;
266269 }
267270}
268271
@@ -278,6 +281,17 @@ impl MemoryConsumer {
278281 name : name. into ( ) ,
279282 can_spill : false ,
280283 id : Self :: new_unique_id ( ) ,
284+ parent_id : None ,
285+ }
286+ }
287+
288+ /// Create a new [`MemoryConsumer`] with a parent consumer ID for lineage tracking
289+ pub fn new_with_parent ( name : impl Into < String > , parent_id : usize ) -> Self {
290+ Self {
291+ name : name. into ( ) ,
292+ can_spill : false ,
293+ id : Self :: new_unique_id ( ) ,
294+ parent_id : Some ( parent_id) ,
281295 }
282296 }
283297
@@ -289,6 +303,7 @@ impl MemoryConsumer {
289303 name : self . name . clone ( ) ,
290304 can_spill : self . can_spill ,
291305 id : Self :: new_unique_id ( ) ,
306+ parent_id : self . parent_id ,
292307 }
293308 }
294309
@@ -312,6 +327,11 @@ impl MemoryConsumer {
312327 & self . name
313328 }
314329
330+ /// Returns the parent consumer ID if this consumer has a parent
331+ pub fn parent_id ( & self ) -> Option < usize > {
332+ self . parent_id
333+ }
334+
315335 /// Registers this [`MemoryConsumer`] with the provided [`MemoryPool`] returning
316336 /// a [`MemoryReservation`] that can be used to grow or shrink the memory reservation
317337 pub fn register ( self , pool : & Arc < dyn MemoryPool > ) -> MemoryReservation {
@@ -462,6 +482,27 @@ impl MemoryReservation {
462482 }
463483 }
464484
485+ /// Create a new [`MemoryReservation`] with a new [`MemoryConsumer] that
486+ /// is a child of this reservation's consumer.
487+ ///
488+ /// This is useful for creating memory consumers with lineage tracking.
489+ pub fn new_child_reservation ( & self , name : impl Into < String > ) -> MemoryReservation {
490+ MemoryConsumer :: new_with_parent ( name, self . consumer ( ) . id ( ) )
491+ . register ( & self . registration . pool )
492+ }
493+
494+ /// Create a new [`MemoryReservation`] which is a clone to the current
495+ /// [`MemoryReservation`]. This means that it's a cloned [`MemoryConsumer`]
496+ /// with the same configuration, but a new unique ID.
497+ ///
498+ /// This is useful for creating memory consumers with lineage tracking,
499+ /// while dealing with multithreaded scenarios.
500+ pub fn cloned_reservation ( & self ) -> MemoryReservation {
501+ self . consumer ( )
502+ . clone_with_new_id ( )
503+ . register ( & self . registration . pool )
504+ }
505+
465506 /// Splits off all the bytes from this [`MemoryReservation`] into
466507 /// a new [`MemoryReservation`] with the same [`MemoryConsumer`]
467508 pub fn take ( & mut self ) -> MemoryReservation {
0 commit comments