Fast way to allocate buffer for producer/consumer scenario



Hi!

The topic already says most of what I want, but here is a slightly more
verbose explanation:
I have a thread that performs network service by simply reading from a
socket. At the moment, it then copies the received data into the internal
queue and triggers another thread to handle the received data. The size of
the received data ranges from around 6 bytes to 500 bytes for 98% of all
cases but other, rare cases being even in the range of a few megabytes.

What I now did was to restructure this so that instead of copying the buffer
to the queue, the data is moved into the queue, i.e. the queue assumes
ownership of the buffer and the network service thread allocates a new
buffer for the next transfer.

During that, I noticed that the performance strongly depends on the way the
buffers are allocated, which is why I wanted to ask for the best way to do
it. I guess that the probably fastest way would be a dedicated memory pool,
i.e. one that is used only for this transfer. However, this is not a
trivial task a) to get right and b) to test that you got it right, so I'd
rather not do this but build on a good system-provided way.

I have at this moment tried GlobalAlloc, VirtualAlloc and 'operator new'.
All three of them had a comparable performance but I'm not satisfied with
either of them. GlobalAlloc() is marked as obsolete and slower than
VirtualAlloc(), which is supposed to replace it. VirtualAlloc() however
interoperates with the OS to reserve pages in virtual and physical memory,
which in itself presents an overhead. Doing so repeatedly seems like a lot
of overhead to me, I'd rather recycle the pages inside the process before
interacting with the OS. 'operator new' does exactly that, but it uses a
bytesize granularity instead of a pagesize granularity which imposes a
certain overhead.

I have taken a look at the heap functions for creating and accessing a heap,
is that perhaps the right approach? To be honest, this API seems to be
quite complicated, which is why I ask here first if it would solve my
problems.

Any suggestions anyone?

Uli

.



Relevant Pages

  • Re: Fast way to allocate buffer for producer/consumer scenario
    ... > queue and triggers another thread to handle the received data. ... > ownership of the buffer and the network service thread allocates a new ... I guess that the probably fastest way would be a dedicated memory ...
    (microsoft.public.vc.language)
  • Re: [take19 1/4] kevent: Core files.
    ... they are still accessible through usual queue) from mapped buffer. ... that buffer (i.e. says to kernel that appropriate kevents can be freed ... Each time the kernel cannot queue an event in the ring buffer, ...
    (Linux-Kernel)
  • Re: Fast way to allocate buffer for producer/consumer scenario
    ... queue and triggers another thread to handle the received data. ... What I now did was to restructure this so that instead of copying the buffer ... VirtualAllochowever interoperates with the OS to reserve pages in virtual and physical memory, which in itself presents an overhead. ... However, I think you'll get the biggest benefit by using a "small buffer" optimization, where smaller allocations are held directly in the object and therefore copied rather than moved. ...
    (microsoft.public.vc.language)
  • Re: High-performance IO
    ... have to call VirtualAlloc() before MapUserPhysicalPages). ... of disk IO under the following conditions. ... Currently I use four independent memory buffers ... How should I allocate the memory buffer? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: [take19 1/4] kevent: Core files.
    ... they are still accessible through usual queue) from mapped buffer. ... the case when kernel is going to generate infinite number of events for ... yet another recovery pass. ...
    (Linux-Kernel)