Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. High performance buffer pooling?

High performance buffer pooling?

Scheduled Pinned Locked Moved C#
questionperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hugo Hallman
    wrote on last edited by
    #1

    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:

    H 1 Reply Last reply
    0
    • H Hugo Hallman

      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:

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 an IntPtr 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 the SafeHandle for just such a purpose. You can either define your own now, or take a look at the GCHandle and HandleRef structs under System.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#'s stackalloc 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]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups