Author Topic: Fast memory allocation  (Read 8460 times)

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Fast memory allocation
« on: December 21, 2015, 11:35:32 PM »
deleted
« Last Edit: February 25, 2022, 11:03:28 AM by nidud »

guga

  • Member
  • *****
  • Posts: 1451
  • Assembly is a state of art.
    • RosAsm
Re: Fast memory allocation
« Reply #1 on: January 04, 2016, 03:13:51 AM »
my results

Code: [Select]
Intel(R) Core(TM) i7 CPU         870  @ 2.93GHz (SSE4.2)
----------------------------------------------
512K -- 200000h
185290    cycles -    499K - 0: HeapAlloc
23885     cycles -    499K - 7: malloc - fixed
23607     cycles -    499K - 8: malloc - auto
17786     cycles -    499K - 9: malloc - dynamic
2M -- 200000h
1058865   cycles -   1996K - 0: HeapAlloc
24210     cycles -   1996K - 7: malloc - fixed
39535     cycles -   1996K - 8: malloc - auto
26909     cycles -   1996K - 9: malloc - dynamic
3M -- 200000h
1556853   cycles -   2904K - 0: HeapAlloc
1088794   cycles -   2904K - 7: malloc - fixed
50941     cycles -   2904K - 8: malloc - auto
694567    cycles -   2904K - 9: malloc - dynamic
4M -- 200000h
1624188   cycles -   3993K - 0: HeapAlloc
1235034   cycles -   3993K - 7: malloc - fixed
25434     cycles -   3993K - 8: malloc - auto
728995    cycles -   3993K - 9: malloc - dynamic
8M -- 200000h
2322818   cycles -   7986K - 0: HeapAlloc
1964005   cycles -   7986K - 7: malloc - fixed
71131     cycles -   7986K - 8: malloc - auto
1154075   cycles -   7986K - 9: malloc - dynamic

result:
   210648 cycles - code(544) 8: malloc - auto
  2622332 cycles - code(496) 9: malloc - dynamic
  4335928 cycles - code(325) 7: malloc - fixed
  6748014 cycles - code( 53) 0: HeapAlloc
hit any key to continue...


question. Can this new malloc (auto) be a replacement for virtualalloc ?
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

jj2007

  • Member
  • *****
  • Posts: 13283
  • Assembly is fun ;-)
    • MasmBasic
Re: Fast memory allocation
« Reply #2 on: January 04, 2016, 03:19:58 AM »
Here are some more, but honestly, I have no idea how to interpret your numbers ::)

Can you give a verbose version of this?
   176684 cycles - code(544) 8: malloc - auto
  2541613 cycles - code(496) 9: malloc - dynamic



Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (AVX)
----------------------------------------------
512K -- 200000h
173231    cycles -    499K - 0: HeapAlloc
15217     cycles -    499K - 7: malloc - fixed
14822     cycles -    499K - 8: malloc - auto
20625     cycles -    499K - 9: malloc - dynamic
2M -- 200000h
406836    cycles -   1996K - 0: HeapAlloc
16913     cycles -   1996K - 7: malloc - fixed
19218     cycles -   1996K - 8: malloc - auto
21011     cycles -   1996K - 9: malloc - dynamic
3M -- 200000h
729334    cycles -   2904K - 0: HeapAlloc
985867    cycles -   2904K - 7: malloc - fixed
50724     cycles -   2904K - 8: malloc - auto
735871    cycles -   2904K - 9: malloc - dynamic
4M -- 200000h
587725    cycles -   3993K - 0: HeapAlloc
1070638   cycles -   3993K - 7: malloc - fixed
29647     cycles -   3993K - 8: malloc - auto
711767    cycles -   3993K - 9: malloc - dynamic
8M -- 200000h
846687    cycles -   7986K - 0: HeapAlloc
1006395   cycles -   7986K - 7: malloc - fixed
62273     cycles -   7986K - 8: malloc - auto
1052339   cycles -   7986K - 9: malloc - dynamic

result:
   176684 cycles - code(544) 8: malloc - auto
  2541613 cycles - code(496) 9: malloc - dynamic
  2743813 cycles - code( 53) 0: HeapAlloc
  3095030 cycles - code(325) 7: malloc - fixed
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Fast memory allocation
« Reply #3 on: January 04, 2016, 03:56:50 AM »
deleted
« Last Edit: February 25, 2022, 11:03:40 AM by nidud »

guga

  • Member
  • *****
  • Posts: 1451
  • Assembly is a state of art.
    • RosAsm
Re: Fast memory allocation
« Reply #4 on: January 04, 2016, 04:18:18 AM »
Tks nidud,

Btw, i presume that your function uses a table of structures (M_BLOCK) to store the address of the memory to be allocated/deallocated, right ?

So, if it is a array of these structures, is there a limit of the number of elements ?  mean, what is the maximum amount of M_BLOCKS structures ? When this amount exceeds, another array is created to ?

I´m asking this, because, let´s say a program have 300 calls to this function  and the maximum amount of these calls are 100. The new memalloc will create the extra 200 group of M_BLOCKS so all 300 calls can work ? In other words, in case of exceeding is there a correspondence of realloc or a function that extendes the memory blocks ?
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Fast memory allocation
« Reply #5 on: January 04, 2016, 05:24:38 AM »
deleted
« Last Edit: February 25, 2022, 11:03:50 AM by nidud »

Grincheux

  • Member
  • ***
  • Posts: 330
  • Never be pleased, Always improve
Re: Fast memory allocation
« Reply #6 on: January 04, 2016, 08:58:44 AM »
Here are my results. What can you say in final? Which kind of memory allocation must we use? Is there an algo (HeapAlloc, malloc, VirtualAlloc...) for intel and an other to use and/or if user have <8Mb memory or > 16Mb. What about the size of the memory allocated ? Same question as above.

I expect your are patient, this a very hard analyze.

Quote
C:\Users\Grincheux\Downloads\malloc>timeit
AMD Athlon(tm) II X2 250 Processor (SSE3)
----------------------------------------------
512K -- 200000h
341027    cycles -    499K - 0: HeapAlloc
17903     cycles -    499K - 7: malloc - fixed
17255     cycles -    499K - 8: malloc - auto
23946     cycles -    499K - 9: malloc - dynamic
2M -- 200000h
659761    cycles -   1996K - 0: HeapAlloc
18834     cycles -   1996K - 7: malloc - fixed
22671     cycles -   1996K - 8: malloc - auto
25272     cycles -   1996K - 9: malloc - dynamic
3M -- 200000h
682142    cycles -   2904K - 0: HeapAlloc
765550    cycles -   2904K - 7: malloc - fixed
42529     cycles -   2904K - 8: malloc - auto
1188740   cycles -   2904K - 9: malloc - dynamic
4M -- 200000h
633392    cycles -   3993K - 0: HeapAlloc
908497    cycles -   3993K - 7: malloc - fixed
30416     cycles -   3993K - 8: malloc - auto
1344787   cycles -   3993K - 9: malloc - dynamic
8M -- 200000h
1255745   cycles -   7986K - 0: HeapAlloc
1048506   cycles -   7986K - 7: malloc - fixed
65802     cycles -   7986K - 8: malloc - auto
2025293   cycles -   7986K - 9: malloc - dynamic

result:
   178673 cycles - code(544) 8: malloc - auto
  2759290 cycles - code(325) 7: malloc - fixed
  3572067 cycles - code( 53) 0: HeapAlloc
  4608038 cycles - code(496) 9: malloc - dynamic
hit any key to continue...
C:\Users\Grincheux\Downloads\malloc>
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

Grincheux

  • Member
  • ***
  • Posts: 330
  • Never be pleased, Always improve
Re: Fast memory allocation
« Reply #7 on: January 04, 2016, 09:00:07 AM »
Here are my results. What can you say in final? Which kind of memory allocation must we use? Is there an algo (HeapAlloc, malloc, VirtualAlloc...) for intel and an other to use and/or if user have <8Gb memory or > 16Gb. What about the size of the memory allocated ? Same question as above.
Is there a possibility that the rams vendor affects the algo, or the quality ot this ram?

I expect your are patient, this a very hard analyze.

Quote
C:\Users\Grincheux\Downloads\malloc>timeit
AMD Athlon(tm) II X2 250 Processor (SSE3)
----------------------------------------------
512K -- 200000h
341027    cycles -    499K - 0: HeapAlloc
17903     cycles -    499K - 7: malloc - fixed
17255     cycles -    499K - 8: malloc - auto
23946     cycles -    499K - 9: malloc - dynamic
2M -- 200000h
659761    cycles -   1996K - 0: HeapAlloc
18834     cycles -   1996K - 7: malloc - fixed
22671     cycles -   1996K - 8: malloc - auto
25272     cycles -   1996K - 9: malloc - dynamic
3M -- 200000h
682142    cycles -   2904K - 0: HeapAlloc
765550    cycles -   2904K - 7: malloc - fixed
42529     cycles -   2904K - 8: malloc - auto
1188740   cycles -   2904K - 9: malloc - dynamic
4M -- 200000h
633392    cycles -   3993K - 0: HeapAlloc
908497    cycles -   3993K - 7: malloc - fixed
30416     cycles -   3993K - 8: malloc - auto
1344787   cycles -   3993K - 9: malloc - dynamic
8M -- 200000h
1255745   cycles -   7986K - 0: HeapAlloc
1048506   cycles -   7986K - 7: malloc - fixed
65802     cycles -   7986K - 8: malloc - auto
2025293   cycles -   7986K - 9: malloc - dynamic

result:
   178673 cycles - code(544) 8: malloc - auto
  2759290 cycles - code(325) 7: malloc - fixed
  3572067 cycles - code( 53) 0: HeapAlloc
  4608038 cycles - code(496) 9: malloc - dynamic
hit any key to continue...
C:\Users\Grincheux\Downloads\malloc>
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Fast memory allocation
« Reply #8 on: January 04, 2016, 10:38:34 AM »
deleted
« Last Edit: February 25, 2022, 11:04:03 AM by nidud »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9994
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Fast memory allocation
« Reply #9 on: January 04, 2016, 12:50:05 PM »
It would be worth adding to the list of allocation techniques the old API GlobalAlloc() with the GMEM_FIXED flag as it is a fair bit faster that the older style of GlobalLock() then GlobalAlloc(). Very few of the memory allocation strategies are well suited for large counts of small allocations, VirtualAlloc() is usually the slowest followed by HeapAlloc() depending on the flags used. A language like BASIC pays a penalty for its string allocation as it does a separate allocation for each string and a reallocation for each string change.

Depending on your needs, a single fixed size allocation that is addressed in slices with a pointer array is far faster if you can live with fixed sized blocks, it also has the additional advantage that you can clear it with a zero fill of the entire allocation while retaining the same set of pointers. If you need individually sized blocks that require to be resized on a needs basis, you are stuck with individual allocations for each item but you pay a speed penalty for doing so both in the allocation and freeing each block when no longer required.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Fast memory allocation
« Reply #10 on: October 27, 2016, 01:30:21 AM »
deleted
« Last Edit: February 25, 2022, 11:04:12 AM by nidud »