Search code examples
javaarraysmemory-managementlinked-listcpu-cache

Java object arrays - use of hardware memory cache


Iterating over consecutive elements of an array is generally considered to be more efficient than iterating over consecutive linked list elements because of caching.

This is undoubtedly true as long as elements have elementary data types. But if the elements are objects, my understanding is that only the references to the objects will be stored in the contiguous memory area of the array (which is likely to be cached) while the actual object data will be stored anywhere in main memory and cannot be cached effectively.

As you normally not only iterate over the container but also need to access object data in each iteration, doesn't this more or less kill the performance benefit of the array over the list?

Edit: The comment about different scenarios varying greatly is probably correct. So let's consider a specific one: You search for a specific object in the container. In order to find it you need to compare a given string to another string that is a class variable of the object.


Solution

  • No, for objects ("pointers") there is an indirection in both. A linked list needs for every node to step to the next one. So it still has an overhead.

    But yes, in a relative way the gain concerns only a part, very roughly the half of the pure walk through, counting indirection steps.

    And ofcourse every indirection makes access more chaotic, slower.

    BTW there is the ArrayList too being similar fast as arrays.