mbutrovich opened a new issue, #12543:
URL: https://github.com/apache/arrow-rs/issues/12543

   **Describe the bug**
   
   With the `pool` feature, `MutableBuffer::shrink_to_fit` resizes the buffer's 
reservation when the new capacity is nonzero, but not when it is zero. 
`try_reallocate` returns early from its zero-size branch before it calls 
`self.reservation.resize`:
   
   
https://github.com/apache/arrow-rs/blob/2d2919c18b3856a11d8a61560b2b3781bddc87da/arrow-buffer/src/buffer/mutable.rs#L420-L441
   
   The pool keeps counting the freed capacity until the buffer is dropped. 
`Buffer::shrink_to_fit` does not have this problem, because 
`Bytes::try_realloc` resizes the reservation after both its zero and nonzero 
branches:
   
   
https://github.com/apache/arrow-rs/blob/2d2919c18b3856a11d8a61560b2b3781bddc87da/arrow-buffer/src/bytes.rs#L133-L155
   
   **To Reproduce**
   
   Run with `cargo test -p arrow-buffer --features pool`:
   
   ```rust
   use arrow_buffer::{MemoryPool, MutableBuffer, TrackingMemoryPool};
   
   #[test]
   fn reservation_after_shrink_to_zero() {
       let pool = TrackingMemoryPool::default();
   
       let mut nonzero = MutableBuffer::with_capacity(128);
       nonzero.resize(10, 1);
       nonzero.claim(&pool);
       nonzero.shrink_to_fit();
       assert_eq!(pool.used(), 64);
       drop(nonzero);
   
       let mut zero = MutableBuffer::with_capacity(128);
       zero.claim(&pool);
       zero.shrink_to_fit();
       assert_eq!(zero.capacity(), 0);
       assert_eq!(pool.used(), 0);
   }
   ```
   
   The last assertion fails with `left: 128, right: 0`.
   
   **Expected behavior**
   
   After `shrink_to_fit` frees the allocation, `pool.used()` is 0, just as it 
is 64 after shrinking to 64 bytes.
   
   **Additional context**
   
   `try_reallocate` with a new size of 0 is reachable only through 
`shrink_to_fit`/`try_shrink_to_fit` on a buffer with `len() == 0`, because 
`try_reserve` never asks for capacity 0.
   
   **Willingness to contribute**
   
   I plan to contribute a fix for this bug independently
   


-- 
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]

Reply via email to