Skip to content

fix: correct accounting in DictEncoder::estimated_memory_size, Interner::estimated_memory_size - #9720

Merged
alamb merged 9 commits into
apache:mainfrom
mzabaluev:fix-estimated-memory-size-on-dict-encoder
Apr 27, 2026
Merged

fix: correct accounting in DictEncoder::estimated_memory_size, Interner::estimated_memory_size#9720
alamb merged 9 commits into
apache:mainfrom
mzabaluev:fix-estimated-memory-size-on-dict-encoder

Conversation

@mzabaluev

@mzabaluev mzabaluev commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The returned value should estimate the actual memory usage, but instead it uses the evaluation of the encoded size of the dictionary data, and bypasses the hash table memory usage added by the Interner member. The implementation of Storage::estimated_memory_size implementation for the unique key storage was not correct as well, but it was unused.

What changes are included in this PR?

Correct both problems by making the KeyStorage's implementation of estimated_memory_size return the size of the allocated uniques vector added with the values' sizes if applicable, and make DictEncoder::estimated_memory_size delegate to the interner, which calls the method of KeyStorage and adds accounting for its own data structure.

Are these changes tested?

Added tests verifying that at least the expected added amounts are accounted for when values are added. Overreporting is hard to disprove due to dependency on allocation behavior internal to other libraries.

Are there any user-facing changes?

No.

The returned value should estimate the actual memory usage, but
instead it used the evaluation of the encoded size of the dictionary
data, and bypassed the hash table memory usage added by the Interner.
The implementation of Storage::estimated_memory_size for the
unique key storage was not correct as well, but it was unused.
Correct both problems.
@github-actions github-actions Bot added the parquet Changes to the parquet crate label Apr 14, 2026
@alamb

alamb commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Is there some way to add tests for this change?

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @mzabaluev

Comment on lines +185 to +186
fn estimated_memory_size(&self) -> usize {
self.interner.storage().size_in_bytes + self.indices.len() * std::mem::size_of::<usize>()
self.interner.estimated_memory_size() + self.indices.len() * std::mem::size_of::<usize>()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the right direction (in the sense of account for the size in the structures that hold the memory, rather than the wrapper)

However, it is not clear to me that KeyStorage includes the heap bytes for types like BYTE_ARRAY

I think we need to add some tests for this code to make sure we have it right

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing upper bounds would require intimate knowledge of reallocation behavior of Vec and hashbrown, but I'll try to get some confirmation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are correct about the byte arrays, I need to account for variable- and fixed length array values.

@mzabaluev mzabaluev Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have accounted for the byte arrays' allocations and added some tests.

@mzabaluev

Copy link
Copy Markdown
Contributor Author

Nobody seems to have really tested these methods, because this Interner code is also wrong:
https://github.com/mzabaluev/arrow-rs/blob/19002abc3522af60b6eaa99c0d5d4999b70cd681/parquet/src/util/interner.rs#L82

It should multiply the table capacity by the number of keys, not add them.

Should multiply hash table capacity by the key size, not add them.
@alamb

alamb commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Nobody seems to have really tested these methods, because this Interner code is also wrong: https://github.com/mzabaluev/arrow-rs/blob/19002abc3522af60b6eaa99c0d5d4999b70cd681/parquet/src/util/interner.rs#L82

It should multiply the table capacity by the number of keys, not add them.

The fact there are no tests and the code is wrong is probably related

@mzabaluev
mzabaluev marked this pull request as draft April 16, 2026 16:44
To estimate allocation size, the vector capacity is the right thing to
use, not the length.
As we cannot test the exact memory allocation behavior or even
give the upper bound without delving into implementation details of
Vec and hashbrown, the only reliable test is to confirm that the lower
bounds are respected, that is, increases in memory use from the empty
state (where all vectors are empty and therefore have added no
allocations) add at least the expected amount of memory.
@mzabaluev
mzabaluev marked this pull request as ready for review April 16, 2026 17:28
@mzabaluev mzabaluev changed the title fix: correct accounting in DictEncoder::estimated_memory_size fix: correct accounting in DictEncoder::estimated_memory_size, Interner::estimated_memory_size Apr 16, 2026
}

#[cfg(test)]
mod tests {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mzabaluev

I ran these tests without your code change and they all passed. Thus I don't think the are covering whatever issue you have found

andrewlamb@Andrews-MacBook-Pro-3:~/Software/arrow-rs$ git diff
diff --git a/parquet/src/encodings/encoding/dict_encoder.rs b/parquet/src/encodings/encoding/dict_encoder.rs
index 37cfdb9ba1..2f32f9c0bb 100644
--- a/parquet/src/encodings/encoding/dict_encoder.rs
+++ b/parquet/src/encodings/encoding/dict_encoder.rs
@@ -64,12 +64,7 @@ impl<T: DataType> Storage for KeyStorage<T> {
     }

     fn estimated_memory_size(&self) -> usize {
-        let uniques_heap_bytes = match T::get_physical_type() {
-            Type::FIXED_LEN_BYTE_ARRAY => self.type_length * self.uniques.len(),
-            _ => <Self::Value as ParquetValueType>::variable_length_bytes(&self.uniques)
-                .unwrap_or(0) as usize,
-        };
-        self.uniques.capacity() * std::mem::size_of::<T::T>() + uniques_heap_bytes
+        self.size_in_bytes + self.uniques.capacity() * std::mem::size_of::<T::T>()
     }
 }

diff --git a/parquet/src/util/interner.rs b/parquet/src/util/interner.rs
index deae3720d5..34c7d1390f 100644
--- a/parquet/src/util/interner.rs
+++ b/parquet/src/util/interner.rs
@@ -77,7 +77,9 @@ impl<S: Storage> Interner<S> {
     /// Return estimate of the memory used, in bytes
     #[allow(dead_code)] // not used in parquet_derive, so is dead there
     pub fn estimated_memory_size(&self) -> usize {
-        self.storage.estimated_memory_size() + self.dedup.allocation_size()
+        self.storage.estimated_memory_size() +
+            // estimate size of dedup hashmap as just th size of the keys
+            self.dedup.capacity() + std::mem::size_of::<S::Key>()
     }

     /// Returns the storage for this interner

And then

andrewlamb@Andrews-MacBook-Pro-3:~/Software/arrow-rs$ cargo test --lib -p parquet -- dict_encoder
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running unittests src/lib.rs (target/debug/deps/parquet-d5e640393e7492a1)

running 6 tests
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_primitive_with_duplicates ... ok
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_primitive_all_distinct ... ok
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_fixed_len_byte_array_with_duplicates ... ok
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_byte_array_with_duplicates ... ok
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_fixed_len_byte_array_all_distinct ... ok
test encodings::encoding::dict_encoder::tests::test_estimated_memory_size_byte_array_all_distinct ... ok

test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 1051 filtered out; finished in 0.00s

andrewlamb@Andrews-MacBook-Pro-3:~/Software/arrow-rs$

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was another change. This makes ByteArray and FixedLenByteArray tests fail:

@@ -188,8 +183,7 @@ impl<T: DataType> Encoder<T> for DictEncoder<T> {
     ///
     /// For this encoder, the indices are unencoded bytes (refer to [`Self::write_indices`]).
     fn estimated_memory_size(&self) -> usize {
-        self.interner.estimated_memory_size()
-            + self.indices.capacity() * std::mem::size_of::<usize>()
+        self.interner.storage().size_in_bytes + self.indices.len() * std::mem::size_of::<usize>()
     }
 }

I have added more tests that fail with the baseline code.

@mzabaluev
mzabaluev marked this pull request as draft April 24, 2026 00:43
Add tests demonstrating failures of the previous implementation,
also one asserting that the uniques vector's capacity is included
in the estimate.
@mzabaluev
mzabaluev marked this pull request as ready for review April 24, 2026 02:33

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mzabaluev - this looks really close

I think we need one more change to make sure this works on 32 bit architectures as well (usize --> u32)-- I am happy to make the changes if you agree, but I wanted to check with you first

fn estimated_memory_size(&self) -> usize {
self.interner.storage().size_in_bytes + self.indices.len() * std::mem::size_of::<usize>()
self.interner.estimated_memory_size()
+ self.indices.capacity() * std::mem::size_of::<usize>()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indices is `Vec

    /// The buffered indices
    indices: Vec<u64>,

But this is using usize

Suggested change
+ self.indices.capacity() * std::mem::size_of::<usize>()
+ self.indices.capacity() * std::mem::size_of::<u64>()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I did not notice that this was also wrong.

);

// Must also account for the 9 buffered indices.
let indices_size = 9 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly this should probably be size_of<u64>

);

// Must also account for the 100 buffered indices.
let indices_size = 100 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

);

// Must also account for the 9 buffered indices.
let indices_size = 9 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

);

// Must also account for the 100 buffered indices.
let indices_size = 100 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

);

// Must also account for the 9 buffered indices.
let indices_size = 9 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

);

// Must also account for the 100 buffered indices.
let indices_size = 100 * std::mem::size_of::<usize>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

The elements of the indices vector are u64, so the memory size
should be calculated accordingly.

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sticking with this @mzabaluev -- very finicky but a nice result overall

@alamb
alamb merged commit cb8d4c0 into apache:main Apr 27, 2026
16 checks passed
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Jun 2, 2026
…erner::estimated_memory_size` (apache#9720)

# Which issue does this PR close?

- Closes apache#9719, apache#9744.

# Rationale for this change

The returned value should estimate the actual memory usage, but instead
it uses the evaluation of the encoded size of the dictionary data, and
bypasses the hash table memory usage added by the `Interner` member. The
implementation of `Storage::estimated_memory_size` implementation for
the unique key storage was not correct as well, but it was unused.

# What changes are included in this PR?

Correct both problems by making the `KeyStorage`'s implementation of
`estimated_memory_size` return the size of the allocated `uniques`
vector added with the values' sizes if applicable, and make
`DictEncoder::estimated_memory_size` delegate to the `interner`, which
calls the method of `KeyStorage` and adds accounting for its own data
structure.

# Are these changes tested?

Added tests verifying that at least the expected added amounts are
accounted for when values are added. Overreporting is hard to disprove
due to dependency on allocation behavior internal to other libraries.

# Are there any user-facing changes?

No.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet Changes to the parquet crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect accounting in DictEncoder::estimated_memory_size

2 participants