Royi Namir
Royi Namir

Reputation: 148524

.net Arrays and sequentiality - clarification?

.Net arrays elements are stored in a sequential memory cells.Even the so called "dynamic" List<T> uses an Array behind the scenes - where each addition of elements - make sure that an appropriate array is created. ( unless inistantiated with a pre defined capacity)

Question

Why it was written like that?

Seeking every time (when size exceeds) - a new sequential region - can be a big performance hit.

They could also used another approach which is - n % m regions of sequential cells . - so we benefit both seek performance AND not needing to search for 100% sequential free cells."

(was the designers counted on the fact that the GC will compact/defrag memory so it will always be easy for them to find sequential free cells ? )

Upvotes: 0

Views: 60

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273219

They could also used another approach which is - n % m regions of sequential cells . - so we benefit both seek performance AND not needing to search for 100% sequential free cells."

There is no need to "search for 100% sequential free cells.". Allocation is very fast in .NET.

The real cost is in copying the contents of the old to the new array. And you are right, a mxn structure could reduce some of that cost.

But there's always a trade-off, while the worst-case for Add() would improve the costs for Insert(), Remove() and especially Seek() (this[int x] ) would increase.

Upvotes: 2

Related Questions