为什么OrderedQueryable类的ToArray方法分配了这么多内存?

huangapple go评论74阅读模式
英文:

Why does the ToArray method of the OrderedQueryable class much allocated?

问题

我查看了System.Linq命名空间的源代码1,并注意到OrderedQueryable类的ToArray方法创建了三个数组,但我们可以只使用一个数组进行排序。为什么要这样做?

英文:

I looked source code of the System.Linq namespace and I noticed that the ToArray method of the OrderedQueryable class created three arrays, but we could use one array for ordering. Why is this done??

答案1

得分: 1

这是因为分配不会花费太多时间,它是一个固定长度的操作,与数组的大小无关。

总体而言,.NET Framework 假定我们只处理合理大小的数组,超过1MB的数组相对较少,短时间内分配内存是一个很好的折衷方案。

此外,每个CPU内置了非常快速的内存移动操作,.NET Framework 不直接提供对这些函数的访问,但内部可以访问它们,它们使用指针和直接传输内存到内存,而不使用托管代码。

对于排序操作,它们选择通过索引映射的方式。int是CPU性能最高的数据类型。(虽然不是最小的)。如果您有一个包含300字节长结构的1000个元素的数组,那么每次交换项目都需要移动900字节!!!(从原始到临时的300,从源到目标的300,从临时到原始的300)。使用索引映射,这只需要移动12个字节,而且可能在CPU寄存器中完成。可能快1000倍。
因此,拥有两个数组以及一个索引是最优化的解决方案,以最小化内存移动操作。

你是对的,这个实现不支持最小内存使用,但性能更好。

英文:

This is because an allocation is not taking much time, it's a fixed-length operation in matter of time, independent from the size of the array.

Overall .NET Framework assumes we deal only with reasonable sizes, Arrays beyond 1 MB are rather seldom and the allocation of memory -for a short time - is a good compromise.

Additionally, there are very fast memory move operations built in in every CPU, the .NET Framework does not give direct access to those functions, but the internals do have access to it, they make use of pointers and direct transfer memory to memory, without managed code.

For a sorting operation, they choose the way via an index-map. An int is the most performant data-type of the cpu. (Although not smallest possible). This especially saves memory, if you have an array of structs. Imagine an array of 1000 Elements with a 300 byte long structure. Each swap of items would need to move 900 bytes !!! (300 to temporary, 300 from origin to destination, 300 from temporry to origin). With an index Map this is 12 bytes to move, and it's probably done in CPU registers. Probably 1000 times faster.
So to have two arrays, plus an index, is the most optimal solution to minmize memory-move operations.

You are right, the implementation is not favouring the smallest memory use, but better performance.

huangapple
  • 本文由 发表于 2020年1月3日 16:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定