Browse Source
Callers may wish to use TemporaryBuffer as an essentially unbounded buffer by passing Integer.MAX_VALUE as the size. (This makes it behave like ByteArrayOutputStream, only without requiring contiguous memory.) Unfortunately, it was always allocating an array in the backing block pointer list to hold enough blocks to MAX_VALUE--all 262,016 of them. It wasn't allocating the blocks themselves, but this array was still extremely wasteful, using about 2MiB of memory on a 64-bit system. Tweak the interface to specify an estimated size, and only allocate the block pointer list enough entries to hold that size. It's an ArrayList, so if that estimate was wrong, it'll grow. We assume the cost of finding enough contiguous memory to grow that array is acceptable. While we're in there, fix an off-by-one error: due to integer division we were undercounting the number of blocks needed to store n bytes of data as (n / SZ). Change-Id: I794eca3ac4472bcc605b3641e177922aca92b9c0stable-4.0
Dave Borowitz
10 years ago
2 changed files with 67 additions and 5 deletions
Loading…
Reference in new issue