Caching Example I
When accessing elements from sets of data, it is often the case that some elements are accessed much more frequently than others. In these cases, it is possible to apply caching techniques to speed up access to these frequently accessed elements. This is best demonstrated with the following example.
Consider a CacheTest
class that consists mainly of
a Map
populated with Integer
objects. I use Integer
objects for convenience to
populate the Map
with many elements, but the
actual object type is of no significance since you use only the
hashCode( )
and equals( )
methods, just as the Map
does.
Basically, you provide two ways to access the elements of the
Map
. The first, plain_access()
, just calls the Map.get( )
method as
normal. The second method, cached_access( )
, uses
the lower bits of the hash code of the object to obtain an index
value into an array. This index is then checked to see whether the
object is there. If it is, the corresponding value in a parallel
value array is returned. If it’s not, the object is placed
there with the value in the corresponding value array.
This is about the simplest example of general cached access. It demonstrates the advantages and pitfalls of cached access. I have selected 10 integers that do not map to the same indexes for the example. Running the class gives a straightforward comparison between the two access methods, and I get the result that the cached access varies significantly depending on the VM used. The access speedups ...
Get Java Performance Tuning now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.