Skip to content

get_buffer_memory_size() returns an incorrect larger size #5352

Closed

Description

@Yifeng-Sigma

Which part is this question about

Question about get_buffer_memory_size() method

Describe your question

We have an arrow chunk which consists of two columns, all generic string type.
If we use value() method to pull out each string value and add up, the total size is 108684 bytes.
If we use value_data().len(), gives us 108684 bytes
If we use value_offsets().len() gives us 5430 bytes
But use get_buffer_memory_size() gives us 521664 bytes
NullBuffer is 0.

Noticed that the implementation of get_buffer_memory_size() is basically to add up value_offsets, value_data and null buffer. But why there's a significant difference? Here 108684 is what we expect.

Additional context

Activity

  1. added
    question
    on Jan 31, 2024
  2. Jefffrey commented on Jan 31, 2024

    @Jefffrey
    Contributor

    I think this PR might help clear up confusion, as it enhances the docstring of get_buffer_memory_size(): #5347

        /// Note that this does not always correspond to the exact memory usage of an array,
        /// since multiple arrays can share the same buffers or slices thereof.
    

    Note that value_data().len() gives the len, i.e. how many bytes are used in the buffer, whilst get_buffer_memory_size() calculates using the capacity of the buffer (how much memory is allocated to it), which might be larger than the len:

    fn get_buffer_memory_size(&self) -> usize {
    let mut sum = self.value_offsets.inner().inner().capacity();
    sum += self.value_data.capacity();
    if let Some(x) = &self.nulls {
    sum += x.buffer().capacity()
    }
    sum
    }

    This should help explain the discrepancy.

  3. tustvold commented on Jan 31, 2024

    @tustvold
    Contributor

    To further expand on the above, the IPC reader avoids copying by slicing buffers, and this is a common source of arrays with such shared buffers

  4. Yifeng-Sigma commented on Jan 31, 2024

    @Yifeng-Sigma
    Author

    Thank you, this is super helpful!

    If I'm only interested in the in the cumulative size of each element without considering the underlying implementation (whether it's copying, slicing), should I use value_data().len()?

    The context is that we want to serialize the arrow data to json, and before serialization we want to have a rough estimate on the size of serialized data based on arrow's data size.

    In this case, we are interested in the size that use value() method to pull out each string value and add up, which equals to value_data().len()

  5. tustvold commented on Jan 31, 2024

    @tustvold
    Contributor

    There is https://docs.rs/arrow-data/latest/arrow_data/struct.ArrayData.html#method.get_slice_memory_size but if you're only interested in strings you could do something like

    let offsets = array.offsets();
    let total_length = offsets.last().unwrap().as_usize() - offsets.first().unwrap().as_usize();
    

    I should highlight that this will ignore the overheads from encoding field names in JSON though, and will be a pretty crude approximation.

  6. Yifeng-Sigma commented on Feb 1, 2024

    @Yifeng-Sigma
    Author

    There is https://docs.rs/arrow-data/latest/arrow_data/struct.ArrayData.html#method.get_slice_memory_size but if you're only interested in strings you could do something like

    let offsets = array.offsets();
    let total_length = offsets.last().unwrap().as_usize() - offsets.first().unwrap().as_usize();
    

    I should highlight that this will ignore the overheads from encoding field names in JSON though, and will be a pretty crude approximation.

    We are interested in types in addition to string as well. get_slice_memory_size() is exactly what I'm looking for!
    Seems only get_buffer_memory_size() and get_array_memory_size are implemented for array, but get_slice_memory_size() is implemented for ArrayData only, wondering what's the recommendation for array here?

  7. tustvold commented on Feb 1, 2024

    @tustvold
    Contributor

    You can call Array::data to convert to the ArrayData representation

  8. Yifeng-Sigma commented on Feb 1, 2024

    @Yifeng-Sigma
    Author

    You can call Array::data to convert to the ArrayData representation

    But seems it will do a clone() underlying, which is heavy if I only want to get the size? Is it possible to implement get_slice_memory_size() for array as well?

  9. tustvold commented on Feb 1, 2024

    @tustvold
    Contributor

    Clone is cheap on buffers, as the underlying storage is reference counted.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions