When compressing or decompressing many (very) small files in a row, the time spent in construction of new compressor or decompressor objects can be longer than the time spent in actual compression or decompression. A large part of this initialization overhead comes from allocation and garbage collection of large arrays.
The ArrayCache API provides a way to cache large array allocations
for reuse. It can give a major performance improvement when compressing or
decompressing many tiny files. If you are only (de)compressing one or two
files or the files a very big, array caching won't improve anything,
although it won't make anything slower either.
Important: The users of ArrayCache don't return the allocated arrays
back to the cache in all situations.
This a reason why it's called a cache instead of a pool.
If it is important to be able to return every array back to a cache,
ResettableArrayCache can be useful.
In compressors (OutputStreams) the arrays are returned to the cache
when a call to finish() or close() returns
successfully (no exceptions are thrown).
In decompressors (InputStreams) the arrays are returned to the cache when
the decompression is successfully finished (read returns -1)
or close() or close(boolean) is called. This is true even
if closing throws an exception.
Raw decompressors don't support close(boolean). With raw
decompressors, if one wants to put the arrays back to the cache without
closing the underlying InputStream, one can wrap the
InputStream into CloseIgnoringInputStream when creating
the decompressor instance. Then one can use close().
Different cache implementations can be extended from this base class. All cache implementations must be thread safe.
This class also works as a dummy cache that simply calls new
to allocate new arrays and doesn't try to cache anything. A statically
allocated dummy cache is available via getDummyCache().
If no ArrayCache is specified when constructing a compressor or
decompressor, the default ArrayCache implementation is used.
See getDefaultCache() and setDefaultCache(ArrayCache).
This is a class instead of an interface because it's possible that in the future we may want to cache other array types too. New methods can be added to this class without breaking existing cache implementations.
BasicArrayCache
| Modifier and Type | Field and Description |
|---|---|
| private static volatile ArrayCache | defaultCache
Global default |
| private static final ArrayCache | dummyCache
Global dummy cache instance that is returned by |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public byte[] | getByteArray(int
the minimum size of the array to allocate;
an implementation may return an array that
is larger than the given size, boolean sizeif true, the caller expects that the first
fillWithZeros)size elements in the array are zero;
if false, the array contents can be anything,
which speeds things up when reusing a cached
arrayAllocates a new byte array. |
| public static ArrayCache | |
| public static ArrayCache | |
| public int[] | getIntArray(int
the minimum size of the array to allocate;
an implementation may return an array that
is larger than the given size, boolean sizeif true, the caller expects that the first
fillWithZeros)size elements in the array are zero;
if false, the array contents can be anything,
which speeds things up when reusing a cached
arrayAllocates a new int array. |
| public void | |
| public void | |
| public static void |
| defaultCache | back to summary |
|---|---|
| private static volatile ArrayCache defaultCache Global default | |
| dummyCache | back to summary |
|---|---|
| private static final ArrayCache dummyCache Global dummy cache instance that is returned by | |
| ArrayCache | back to summary |
|---|---|
| public ArrayCache() Creates a new | |
| getByteArray | back to summary |
|---|---|
| public byte[] getByteArray(int size, boolean fillWithZeros) Allocates a new byte array.
This implementation simply returns
| |
| getDefaultCache | back to summary |
|---|---|
| public static ArrayCache getDefaultCache() Gets the default | |
| getDummyCache | back to summary |
|---|---|
| public static ArrayCache getDummyCache() Returns a statically-allocated | |
| getIntArray | back to summary |
|---|---|
| public int[] getIntArray(int size, boolean fillWithZeros) Allocates a new int array.
This implementation simply returns
| |
| putArray | back to summary |
|---|---|
| public void putArray(byte[] array) Puts the given byte array to the cache. The caller must no longer use the array. This implementation does nothing. | |
| putArray | back to summary |
|---|---|
| public void putArray(int[] array) Puts the given int array to the cache. The caller must no longer use the array. This implementation does nothing. | |
| setDefaultCache | back to summary |
|---|---|
| public static void setDefaultCache(ArrayCache arrayCache) Sets the default | |