C#中的ArrayPool创建方法 [英] ArrayPool create method in C#

查看:254
本文介绍了C#中的ArrayPool创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中与ArrayPool一起使用。
我想创建自己的池,最大数组数为5,最大大小为1050000。
我使用了此ArrayPool.Create()方法。
我无法理解一件事-我尝试在下面的代码段中从池中租借10次,尽管我将max arrays指定为5,但是为什么它没有显示任何错误。
另外,我将最大长度指定为1050000。那么我如何租借4200000数组而不会出现任何错误?

I was using with ArrayPool in C#. I wanted to create my own pool with max no of arrays 5 and max size of array 1050000. I used this ArrayPool.Create() method. I am not able to understand one thing - i am trying to rent from the pool 10 times in the snippet below ,although i specified max arrays to be 5 , then why is it not showing any error. Also, i specified max length to be 1050000.Then how am i able to rent a 4200000 array without any error ?

        byte[] buffer;
        ArrayPool<byte> pool = ArrayPool<byte>.Create(1050000, 5);

        for (int i = 0; i < 10; i++)
        {
            buffer = pool.Rent(4200000);
        }


推荐答案

传递给 ArrayPool.Create 并不意味着您不能接收到大于这些限制的数组。而是使用它们来控制 ConfigurableArrayPool 的存储算法。第二个参数是存储桶中的最大插槽数,第一个参数是任何数组的最大大小。此值由内部常量 1,048,576 ,已经比您的<​​code> 1,050,000 小 c $ c>。

The options passed to ArrayPool.Create don't imply you cannot recieve an array larger than those limits. Instead they are used to control the bucketing algorithm of the ConfigurableArrayPool. The second argument is the maximum number of slots in a bucket and the first is the maximum size of any array. This value is capped by an internal constant of 1,048,576 which is already smaller than your 1,050,000.

当您从数组池中租用时,算法将尝试在其中一个存储桶中定位数组/插槽。这些存储桶(及其内部插槽)的数量受到所传递的值的限制。如果池中没有使用 minimum 大小的数组,或者是因为所有插槽都在使用中或者因为请求的大小大于最大大小,它会分配一个新的大小(不合并)并返回该大小。

When you Rent from the array pool, the algorithm will attempt to locate an array in one of the buckets/slots. The number of these buckets (and their internal slots) are what become limited by the values you passed in. If the pool doesn't have an array of the minimum size requested either because all slots are in use or because the requested size is greater than the maximum, it will instead allocate a new one (without pooling it) and return that.

简而言之,当您请求大于您传递给 Create 方法的(上限)大小的数组,您将产生分配并收到不参与池的数组。使用此数组调用 Return 不会将其放回池中;而是将被丢弃。

In short, when you request an array larger than the (capped) size you passed in to the Create method you will incur an allocation and recieve an array that does not participate in the pool. Calling Return with this array will not place it back into the pool; instead it will be "dropped".

但是请记住,这些规则仅适用于内置阵列池。您(或其他人)可以编写一个实现,以限制返回数组的大小,甚至抛出值的实现-尽管我认为这些可能行为不当(至少不支持文档) )。

Keep in mind however that these rules only apply to the built-in array pool. You (or someone else) could write an implementation that caps the size of the returned array or even throws -- though I'd argue that those might not be considered well-behaved (at least without supporting doc).

更新,根据您的评论:

虽然为true,但没有直接对应于数字的参数桶中,有间接。存储桶数是使用您传入的最大数组大小计算的。确定最大存储桶基于2和其他逻辑

While true there is not a parameter that corresponds directly to the number of buckets, there is indirectly. The number of buckets is calculated using the maximum array size you pass in. The max buckets is determined based on powers of 2 and some other logic.

这篇关于C#中的ArrayPool创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆