News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Memory management from a performance standpoint

Started by AKRichard, July 23, 2012, 10:47:23 AM

Previous topic - Next topic

Greenhorn

Yes, very interesting approach, Jochen.  :t


Greenhorn
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

AKRichard

Quote from: jj2007 on July 24, 2012, 05:54:47 PM
You need to find a balance between speed and complexity. The circular buffer works just fine for many small allocations, and is a lot faster than HeapAlloc.

Try to distinguish
- parts of your code that need many small allocations ---> circular buffer
- parts of your code that need a few big allocations ---> HeapAlloc or VirtualAlloc

Your OS allows to allocate a 400MB circular buffer, and you can specify 20,000 slots if that is needed. The speed advantage will stay.

  I do have the slot manager working, though the return values are still being handled with malloc, I am finally getting comparable speeds.  I have just a few more questions then Ill let this thread drop.

1.  VirtualAlloc assigns a range of virtual memory then maps physical memory to it? if that is correct, wouldnt that be slower then malloc?
2. whats the difference between malloc and heapalloc?

  Sorry if some of the questions seem pretty basic, but I really havent messed with the native side of c++ a whole lot

npnw

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx

malloc
http://msdn.microsoft.com/en-us/library/aa246461(v=vs.60).aspx

malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.

heap alloc

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597(v=vs.85).aspx

Now there may be some differences depending on the target platform for development, and or environment Windows xp, vista, 7, or 8, not sure. These were specific to their environment. 
It doesn't look like they have changed for a long time from memory. 

Hope this helps.

MichaelW

The "storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object" statement is far out of date.
Well Microsoft, here's another nice mess you've gotten us into.

npnw

QuoteThe "storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object" statement is far out of date.

I didn't catch the last part of that when I posted it. My fault.  It does remind me of early Windows programming.
Have to find a more recent explanation of this in MSDN.



jj2007

VS 2012: "The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object."

Since M$ won't tell you, you'll probably have to launch the debugger to find out that it's VirtualAlloc under the hood, i.e. "align 4096" - simple but a bit slow for small allocations.

npnw

jj,

Wasn't 4096 the page size. That way if it was paged it wouldn't cause a fault?

jj2007

Well, that arrogant phrase "guaranteed to be suitably aligned for storage of any type of object" suggests they are using a full page. Post an exe with an int 3 inserted before the "malloc", and we'll see if it's VirtualAlloc...

npnw

jj  Well, that arrogant phrase "guaranteed to be suitably aligned for storage of any type of object" suggests they are using a full page. Post an exe with an int 3 inserted before the "malloc", and we'll see if it's VirtualAlloc...

LOL!

A man after my own heart. 

The INT 3 instruction is defined for use by debuggers to temporarily replace an instruction in a running program, in order to set a breakpoint. Other INT instructions are encoded using two bytes. This makes them unsuitable for use in patching instructions (which can be one byte long). (see SIGTRAP)
The opcode for INT 3 is 0xCC, as opposed to the opcode for INT immediate', which is 0xCD imm8. According to Intel documentation: "Intel and Microsoft assemblers will not generate the CD03 opcode from any mnemonic" and 0xCC has some special features, which are not shared by "the normal 2-byte opcode for INT 3 (CD03)" [IA-32 Arch. Software Developer's Manual. Vol. 2A]

jj2007

lol ;-)

I am not a C coder, but if I remember well, you can insert something like _asm int 3; in C code; which would trigger the Just In Time debugger, Olly in my case.

Cheers, Jochen

MichaelW

Under Windows 2000 and XP the minimum alignment for VirtualAlloc appears to be 65536, or 16 pages.
Well Microsoft, here's another nice mess you've gotten us into.

AKRichard

Lake Tahoe is great for taking your mind off things.  I hated to leave.

  Anyways, I have a modified version of the slot manager that can grow as needed, its not truely circular anymore, but works well even with the return values now .  Im not getting as fast of results with my code as you did yours, but it is running stably in single threaded mode(I have a glitch somewhere that only shows up in multithreaded apps that I may be posting about on another thread if I dont figure it out soon).

npnw, thanks for the links, I had read the one on malloc, but not virtualalloc or heapalloc.  Kind of off topic, but is virtualallocex how they inject code (for creating a hook into one of the windows apis)?

  Thanks everyone for the help.

merlin

Check this link.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx

"VirtualAlloc cannot reserve a reserved page. It can commit a page that is already committed. This means you can commit a range of pages, regardless of whether they have already been committed, and the function will not fail."

MEM_LARGE_PAGES
0x20000000

Allocates memory using large page support.
The size and alignment must be a multiple of the large-page minimum.
To obtain this value, use the GetLargePageMinimum function.

You could just use VirtualAlloc and use the GetLargePageMinimum. I don't know how efficient this would be.