Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is performance better in a LinkedList when a reference type value and its node are created at the same time?

Tags:

c#

This questions stems from the MSDN page on LinkedList under "Remarks" (fifth line).

Lists that contain reference types perform better when a node and its value are created at the same time. LinkedList accepts null as a valid Value property for reference types and allows duplicate values.

I have searched through the source code and nothing really stands out to me. Could it be that this line was once true but was just forgotten about? If not, then why is it the case?

like image 732
Ricky L. Avatar asked Oct 02 '17 15:10

Ricky L.


1 Answers

My guess is that it has to do with Locality of reference.

.NET uses a compressing garbage collector, that means that in case of reference type values, those would be allocated alongside its related LinkedListNode<T> in the actual RAM region. If you access the value right after reaching the node, there is a good chance the value is already in cache.

like image 50
George Polevoy Avatar answered Oct 19 '22 04:10

George Polevoy