adamreeve commented on code in PR #8925: URL: https://github.com/apache/arrow-rs/pull/8925#discussion_r2587319067
########## parquet/tests/metadata_memory.rs: ########## @@ -0,0 +1,156 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Tests calculation of the Parquet metadata heap size + +use parquet::arrow::arrow_reader::{ArrowReaderMetadata, ArrowReaderOptions}; +use parquet::encryption::decrypt::FileDecryptionProperties; +use parquet::file::metadata::PageIndexPolicy; +use std::alloc::{GlobalAlloc, Layout, System}; +use std::fs::File; +use std::hint::black_box; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +pub struct TrackingAllocator { + pub bytes_allocated: AtomicUsize, +} + +impl TrackingAllocator { + pub const fn new() -> Self { + Self { + bytes_allocated: AtomicUsize::new(0), + } + } + + pub fn bytes_allocated(&self) -> usize { + self.bytes_allocated.load(Ordering::Relaxed) + } +} + +impl Default for TrackingAllocator { + fn default() -> Self { + Self::new() + } +} + +unsafe impl GlobalAlloc for TrackingAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.bytes_allocated + .fetch_add(layout.size(), Ordering::Relaxed); + unsafe { System.alloc(layout) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.bytes_allocated + .fetch_sub(layout.size(), Ordering::Relaxed); + unsafe { System.dealloc(ptr, layout) } + } +} + +#[global_allocator] +static ALLOCATOR: TrackingAllocator = TrackingAllocator::new(); + +#[test] +fn test_metadata_heap_memory() { + // Run test cases sequentially so that heap allocations + // are restricted to a single test case at a time. + let test_data = arrow::util::test_util::parquet_test_data(); + let reader_options = + ArrowReaderOptions::default().with_page_index_policy(PageIndexPolicy::Required); + + { + let path = format!("{test_data}/alltypes_dictionary.parquet"); + verify_metadata_heap_memory(&path, 0.0, || reader_options.clone()); + } + + { + // Calculated heap size doesn't match exactly, possibly due to extra overhead not accounted + // for in the HeapSize implementation for parquet::data_type::ByteArray. Review Comment: I haven't managed to track down exactly where the difference comes from so this is a bit of a guess. The confusing part is the file with encryption has stats for a `ByteArray` column too so I'm not sure why it does give the exact same heap size. Maybe there's something different about how the stats from encrypted metadata work or maybe the difference is from somewhere else. But the computed size is still very close to the actual heap allocation size so I think this is good enough. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
