Re: Fast way to allocate buffer for producer/consumer scenario



"Ulrich Eckhardt" <eckhardt@xxxxxxxxxxxxxx> wrote in message
news:bq1a93-fa6.ln1@xxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>

HeapAlloc is the recommended method. recent versions of Windows
have shown a dramatically improved heap-allocation performance. Which
version of Windows are you basing your tests on?

The compiler you are using will also influence memory-allocation speeds,
if you are using the c-runtime new/malloc routines. Although VC6.0
was quite good in this area, newer versions of visual-studion (i.e.
2003/2005)
have better memory-management routines.

It is unlikely that you will be able to code a better malloc() that is
already available,
but if you have very specific requirements (such as always allocating
500bytes
at a time as you mention) then maybe a custom allocator would be suitable.
You could replace the global-new operator with your own version and have
no impact on the rest of your program design.

A network service type program suggests to me a circular buffer type
arrangement because your packets aren't going to be in memory
for ever are they? Just allocate yourself a large buffer (i.e. 1Mb)
and maintain circular queue pointers, with older packets being overwritten
by new packets as they arrive.

James

--
Microsoft MVP - Windows SDK
www.catch22.net
Free Win32 Source and Tutorials



.



Relevant Pages

  • Re: DMA Buffer allocation Problem..
    ... HalAllocateCommonBufferalways allocates non-cached memory so you ... Don't forget to free the buffer afterwards with HalFreeCommonBuffer. ... unsigned long lfp; ...
    (microsoft.public.windowsce.platbuilder)
  • RFC/Doc/BUGs: CONFIG_PACKET_MMAP
    ... configurable circular buffer mapped in user space. ... circular buffer of unswapable memory mapped in the capture process. ... the name indicates, this function allocates pages of memory, it allocates a power ... There is another vector of pointers, wich hold references to each frame ...
    (Linux-Kernel)
  • Re: Dynamic memory allocation:Reading a file into buffers using a single linked list!
    ... > I want to read a file using freadand then put it in to memory. ... Here the queue can act as a buffer. ... always add nodes at Last and remove First Node ...
    (comp.lang.c)
  • Re: DMA Buffer allocation Problem..
    ... HalAllocateCommonBuffer() is the function that you are looking for to ... HalAllocateCommonBufferalways allocates non-cached memory so you ... Don't forget to free the buffer afterwards with HalFreeCommonBuffer. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: allocating stl containers in memory buffer
    ... > memory buffer? ... > but won't integer variables (and possible others needed by queue object) ... > allocated using default allocator outside the buffer? ... allocator to your queue object. ...
    (comp.lang.cpp)