Replies: 4 comments 5 replies
-
|
The shared array pool is optimized for synchronous rent and return. I think that's where the short lived advice comes from. |
Beta Was this translation helpful? Give feedback.
-
|
The other thing that should be mentioned here is that if your array is also small (in bytes used - size of type * number of elements), in addition to being short-lived, it may behoove you to look at using |
Beta Was this translation helpful? Give feedback.
-
|
I think we need guidelines. I was optimizing Linq's TakeLast, but I have a different opinion from the reviewer regarding ArrayPool and yield. I would like official guidelines (at least for implementations within the BCL).
Reviewer:
|
Beta Was this translation helpful? Give feedback.
-
|
The use of ArrayPool.Rent and Return can be used, for example, within a loop where an array of varying size needs to be created multiple times. The ArrayPool attempts to re-use memory allocated for prior arrays. for (int i =0; ... ){
var buffer = ArrayPool<int>.Shared.Rent(N);
...
ArrayPool.Return(buffer, clearArray: true);
}However, in many cases, when N is constant (or when the exact value doesn't strictly matter), such code can be optimized by moving the buffer to an outer loop or function and just re-using it. int[] buffer = new int[N];
for (int i =0; ... ){
...
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When should they be used? Are there any guidelines?
And the docs said:
It's not very clear, how frequent is frequent?
Is it premature optimization to use them instead of temporary arrays which created in the all method bodies?
Even refactor all short life object like the follow, is this bad?
Beta Was this translation helpful? Give feedback.
All reactions