Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no capacity in ConcurrentQueue?

Tags:

.net

Why doesn't ConcurrentQueue have a capacity like its non-concurrent cousin? There isn't a mentioning of a default capacity either.

Does the "missing" capacity impact performance compared to the non-concurrent version where the implementer can provide a qualified guess of the typical size of the queue?

like image 615
kasperhj Avatar asked May 05 '15 07:05

kasperhj


1 Answers

ConcurrentQueue is implemented using lock free techniques. It is based on "Linked list". Capacity doesn't makes sense in Linked list so it doesn't expose the capacity.

Adding elements to the LinkedList is dirt cheap, it doesn't need to resize the array. It is just modifying the Tail pointer(reference). If the implementation uses array, then resizing the array is expensive, so pre allocating the array will improve performance significantly when you enqueue many elements.

Other non concurrent implementations of the Queue (System.Collections.Generic.Queue<T> and System.Collections.Queue) are array based. So it makes sense to pre allocate the array with given capacity in order to avoid resizing the array often and thus it exposes a Capacity property.

like image 71
Sriram Sakthivel Avatar answered Oct 26 '22 08:10

Sriram Sakthivel