Skip to content

Commit 99895e6

Browse files
authored
refactor: Simplify heap size estimation for types that own no heap allocations (#22918)
## Which issue does this PR close? - Closes None. ## Rationale for this change This pr simplifies heap size estimation by using a macro for types that own no heap allocations. This removes a lot of redundant code. ## What changes are included in this PR? See above. ## Are these changes tested? Yes, previous tests are passing and more tests are added. ## Are there any user-facing changes? No.
1 parent 6520315 commit 99895e6

1 file changed

Lines changed: 48 additions & 110 deletions

File tree

datafusion/common/src/heap_size.rs

Lines changed: 48 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -410,24 +410,6 @@ impl DFHeapSize for UnionFields {
410410
}
411411
}
412412

413-
impl DFHeapSize for UnionMode {
414-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
415-
0 // no heap allocations
416-
}
417-
}
418-
419-
impl DFHeapSize for TimeUnit {
420-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
421-
0 // no heap allocations
422-
}
423-
}
424-
425-
impl DFHeapSize for IntervalUnit {
426-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
427-
0 // no heap allocations
428-
}
429-
}
430-
431413
impl DFHeapSize for Field {
432414
fn heap_size(&self, ctx: &mut DFHeapSizeCtx) -> usize {
433415
self.name().heap_size(ctx)
@@ -452,98 +434,40 @@ impl DFHeapSize for IntervalDayTime {
452434
}
453435
}
454436

455-
impl DFHeapSize for DateTime<Utc> {
456-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
457-
0 // no heap allocations
458-
}
459-
}
460-
461-
impl DFHeapSize for bool {
462-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
463-
0 // no heap allocations
464-
}
465-
}
466-
impl DFHeapSize for u8 {
467-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
468-
0 // no heap allocations
469-
}
470-
}
471-
472-
impl DFHeapSize for u16 {
473-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
474-
0 // no heap allocations
475-
}
476-
}
477-
478-
impl DFHeapSize for u32 {
479-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
480-
0 // no heap allocations
481-
}
482-
}
483-
484-
impl DFHeapSize for u64 {
485-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
486-
0 // no heap allocations
487-
}
488-
}
489-
490-
impl DFHeapSize for i8 {
491-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
492-
0 // no heap allocations
493-
}
494-
}
495-
496-
impl DFHeapSize for i16 {
497-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
498-
0 // no heap allocations
499-
}
500-
}
501-
502-
impl DFHeapSize for i32 {
503-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
504-
0 // no heap allocations
505-
}
506-
}
507-
impl DFHeapSize for i64 {
508-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
509-
0 // no heap allocations
510-
}
511-
}
512-
513-
impl DFHeapSize for i128 {
514-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
515-
0 // no heap allocations
516-
}
517-
}
518-
519-
impl DFHeapSize for i256 {
520-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
521-
0 // no heap allocations
522-
}
523-
}
524-
525-
impl DFHeapSize for f16 {
526-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
527-
0 // no heap allocations
528-
}
529-
}
530-
531-
impl DFHeapSize for f32 {
532-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
533-
0 // no heap allocations
534-
}
535-
}
536-
impl DFHeapSize for f64 {
537-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
538-
0 // no heap allocations
539-
}
540-
}
541-
542-
impl DFHeapSize for usize {
543-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
544-
0 // no heap allocations
545-
}
546-
}
437+
/// Implement [`DFHeapSize`] for types that own no heap allocations.
438+
macro_rules! impl_zero_heap_size {
439+
($($t:ty),+ $(,)?) => {
440+
$(
441+
impl DFHeapSize for $t {
442+
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
443+
0 // no heap allocations
444+
}
445+
}
446+
)+
447+
};
448+
}
449+
450+
impl_zero_heap_size!(
451+
bool,
452+
u8,
453+
u16,
454+
u32,
455+
u64,
456+
usize,
457+
i8,
458+
i16,
459+
i32,
460+
i64,
461+
i128,
462+
i256,
463+
f16,
464+
f32,
465+
f64,
466+
UnionMode,
467+
TimeUnit,
468+
IntervalUnit,
469+
DateTime<Utc>,
470+
);
547471

548472
#[cfg(test)]
549473
mod tests {
@@ -621,6 +545,20 @@ mod tests {
621545
assert_eq!(size(&f16::from_f32(0.0)), 0);
622546
}
623547

548+
#[test]
549+
fn test_heap_size_union_mode() {
550+
assert_eq!(size(&UnionMode::Sparse), 0);
551+
assert_eq!(size(&UnionMode::Dense), 0);
552+
}
553+
554+
#[test]
555+
fn test_heap_size_time_units() {
556+
assert_eq!(size(&TimeUnit::Second), 0);
557+
assert_eq!(size(&IntervalUnit::YearMonth), 0);
558+
assert_eq!(size(&DateTime::<Utc>::UNIX_EPOCH), 0);
559+
assert_eq!(size(&Utc::now()), 0);
560+
}
561+
624562
#[test]
625563
fn test_string() {
626564
let mut s = String::with_capacity(32);

0 commit comments

Comments
 (0)