Where Are Value Types Stored In (C#) Generic Collections

后端 未结 5 1706
北海茫月
北海茫月 2021-02-08 09:31

It is true that generic collections perform better than non-generic collections for value types. (i.e. List vs. ArrayList).

But why is that, other than the boxing-unboxi

相关标签:
5条回答
  • 2021-02-08 10:02

    In generics, such as List<T>, they're still stored on the heap. The difference is that, internally, a List<int> makes a single array of integers, and can store the numbers directly. WIth ArrayList, you end up storing an array of references to boxed integer values.

    0 讨论(0)
  • 2021-02-08 10:03

    The relevant implementation detail is that the underlying storage for a List<T> is a T[]. So for a List<int> the values will be stored in an int[]. The integers are stored in a contiguous chunk of memory, allocated from the garbage collected heap.

    What makes it so fast is not just that the integers are not boxed, it is that an int[] works so very well with the CPU cache. When you read the first element, you essentially get the next 15 for free without having to read the slow RAM or secondary cache. This works not nearly so well for a boxed int because it is so large and the extra reference can have poor cache locality. However, the garbage collector really helps to take the sting out of that cost by compacting the heap.

    0 讨论(0)
  • 2021-02-08 10:06

    There are several reasons besides boxing and unboxing, including memory caching and the way they are enumerated to perform their jobs. Check out this post, especially the comments.

    0 讨论(0)
  • 2021-02-08 10:14

    The performance gains in generics are generally only with regards to value types used with generics compared with value types stored in non-generic equivalents.

    This is because with generics value types do not need to be cast to object and stored on the heap (boxed). In fact they can remain on the stack which is more performant.

    http://msdn.microsoft.com/en-us/library/ms172181.aspx

    0 讨论(0)
  • 2021-02-08 10:15

    An ArrayList is an local array of references to objects stored in the heap.

    A generic List of references types is a local array of references to objects stored in the heap.

    A generic List of value types is a local array of those value types.

    There are two areas of memory, which most references call "The Stack" and "The Heap". Most people who use those terms have no idea why. ("The Stack" may be a stack, but The Heap almost certainly isn't a heap). I prefer the terms "Over Here" and "Over There". When boxed, the value type data is stored "Over There". When stored in an array (perhaps inside a generic List), the value type data is stored "Over Here". "Over here" is better.

    0 讨论(0)
提交回复
热议问题