High performance buffer pooling?
-
Hello. I'm working on a performance critical component, and need buffers to work in. I'll only need one buffer per thread, (at most) so I'll store one in thread-local-storage and I'll keep a record on the average needed size, per AppDomain. Thus I'll change the size of the pooled buffers peridocially to the average size (rounded up to the nearest SystemPageSize, which is usually 4K). So, the question is if this is even a good idea in managed code at all? If I was working in unmanaged code, I'd not even have asked, but now I don't know if it's simply better to call
new byte[requiredSize].
:confused: -
Hello. I'm working on a performance critical component, and need buffers to work in. I'll only need one buffer per thread, (at most) so I'll store one in thread-local-storage and I'll keep a record on the average needed size, per AppDomain. Thus I'll change the size of the pooled buffers peridocially to the average size (rounded up to the nearest SystemPageSize, which is usually 4K). So, the question is if this is even a good idea in managed code at all? If I was working in unmanaged code, I'd not even have asked, but now I don't know if it's simply better to call
new byte[requiredSize].
:confused:Better is a relative term. If "better" == "safer", then
new byte[requiredSize]
is better. If "better" == "faster", then you'll get better performance by P/Invoking HeapAlloc or similar. The problem with this approach is that the memory is unmanaged by the CLR so it can't track it. You could marshal it to anIntPtr
and use that in managed code, but the buffer is still allocated on the unmanaged heap (or stack if you used something like_alloca
). You'll have to free it explicitly. This is where wrapping such functions in a class following the disposable pattern is handle, and .NET 2.0 will introduce theSafeHandle
for just such a purpose. You can either define your own now, or take a look at theGCHandle
andHandleRef
structs underSystem.Runtime.InteropServices
which may be helpful depending on how you're using the memory (especially if you need to marshal managed types to native memory). There is yet another way, though. Read about C#'sstackalloc
operator keyword. This, of course, will alloc memory on the heap and give you an unsafe (i.e., has access to direct memory and is not GC'd) pointer. HTH This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]