I often read that linked list data structure and its variant skiplists are cache friendly in parallel hardware. What does this mean ? Can some one please explain in an easy to understand way .
Edit: The context is in this link .
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
linked list and similar structures are NOT CPU cache friendly because each node can be randomly arranged in memory resulting in many cache misses.
An ArrayList by comparison will have all its references sequentially in memory so when a cache line is read in (typically 64 byte long) this can read in 16 references at once.
Note: The objects the List refers to can still be arranged randomly in memory, something you have no control over. 😐
From the article in the question.
What this is talking about is that a linked list when modified by multiple threads at once (something
LinkedListin Java doesn’t support) only the nodes of the list which are modified need to be made cache consistent. By comparison if you remove or add an element in the middle or start of an ArrayList, you need to update all the references. Give this is known to be inefficient, its best avoided in any case.The closest example to this in Java is
ConcurrentLinkedQueuewhich supports concurrent adding and removing. The problem is that any benefit you might gain by being able to update the start and the end in terms of the cache is lost by the fact that this action creates garbage which is much more significant, though still not very significant.If you use an ArrayBlockingQueue you get better cache and garbage behaviour as the references are continuous in memory, don’t require shuffling down like ArrayList and don’t create garbage to add an entries. (Unfortunately
take()creates an object 😛 )