Author Topic: HeapAlloc vs HeapReAlloc  (Read 1154 times)

jj2007

  • Member
  • *****
  • Posts: 8776
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #15 on: April 02, 2018, 06:14:16 PM »
This is really weird. I attach a new version that calculates a) the ratio of timings and b) the reuse rate in the HeapReAllocate case. For short strings (<=8 bytes), reuse is 100%, for <=32 bytes it's still 84%.

Results for Core i5 on Win7-64:
Code: [Select]
alloc --------------------------------------
16 ms 234 ms 62 ms 31 ms 47 ms 94 ms 31 ms 62 ms 63 ms 46 ms 686 ms total

realloc
16 ms 16 ms 15 ms 0 ms 16 ms 15 ms 16 ms 16 ms 15 ms 16 ms
reuse: 60% - 141 ms total
alloc : realloc = 4.87 for len=640

alloc --------------------------------------
15 ms 468 ms 63 ms 93 ms 359 ms 63 ms 62 ms 78 ms 109 ms 265 ms 1575 ms total

realloc
16 ms 15 ms 16 ms 15 ms 16 ms 16 ms 15 ms 16 ms 31 ms 16 ms
reuse: 59% - 172 ms total
alloc : realloc = 9.16 for len=1280

alloc --------------------------------------
46 ms 640 ms 156 ms 328 ms 171 ms 156 ms 234 ms 187 ms 188 ms 171 ms 2277 ms total

realloc
203 ms 468 ms 468 ms 468 ms 452 ms 453 ms 452 ms 468 ms 468 ms 468 ms
reuse: 59% - 4368 ms total
alloc : realloc = 0.52 for len=2560

alloc --------------------------------------
406 ms 405 ms 297 ms 218 ms 265 ms 188 ms 202 ms 188 ms 187 ms 140 ms 2496 ms total

realloc
749 ms 1560 ms 1716 ms 1653 ms 1654 ms 1732 ms 1747 ms 1763 ms 1731 ms 1732 ms
reuse: 59% - 16037 ms total
alloc : realloc = 0.16 for len=5120

Note that the speed advantage for realloc rises to a factor 9 until string len rand(1280), then it drops dramatically, i.e. realloc becomes much slower than alloc ::)

Siekmanski

  • Member
  • *****
  • Posts: 1674
Re: HeapAlloc vs HeapReAlloc
« Reply #16 on: April 02, 2018, 06:58:00 PM »
I7 Win8.1 64
Code: [Select]
alloc --------------------------------------
0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 16 ms
reuse: 0% - 31 ms total
alloc : realloc = 0.48 for len=80

alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 16 ms total

realloc
0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms
reuse: 0% - 15 ms total
alloc : realloc = 1.07 for len=160

alloc --------------------------------------
16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 32 ms total

realloc
0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms
reuse: 0% - 31 ms total
alloc : realloc = 1.03 for len=320

alloc --------------------------------------
15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms
reuse: 0% - 31 ms total
alloc : realloc = 0.48 for len=640

alloc --------------------------------------
0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 32 ms total

realloc
0 ms 15 ms 0 ms 0 ms 16 ms 0 ms 0 ms 15 ms 0 ms 0 ms
reuse: 0% - 46 ms total
alloc : realloc = 0.70 for len=1280

alloc --------------------------------------
16 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 15 ms 47 ms total

realloc
0 ms 16 ms 0 ms 16 ms 0 ms 15 ms 0 ms 16 ms 0 ms 0 ms
reuse: 0% - 63 ms total
alloc : realloc = 0.75 for len=2560

alloc --------------------------------------
16 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 47 ms total

realloc
0 ms 16 ms 16 ms 15 ms 0 ms 16 ms 15 ms 16 ms 0 ms 16 ms
reuse: 0% - 110 ms total
alloc : realloc = 0.43 for len=5120
ok?
Creative coders use backward thinking techniques as a strategy.

LiaoMi

  • Member
  • ***
  • Posts: 320
Re: HeapAlloc vs HeapReAlloc
« Reply #17 on: April 02, 2018, 07:51:33 PM »
i7-4gen win10 64

Code: [Select]
alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms
reuse: 0% - 32 ms total
alloc : realloc = 80

alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms 15 ms total

realloc
0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms
reuse: 0% - 16 ms total
alloc : realloc = 160

alloc --------------------------------------
0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms
reuse: 0% - 32 ms total
alloc : realloc = 320

alloc --------------------------------------
0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
16 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 15 ms 0 ms
reuse: 0% - 47 ms total
alloc : realloc = 640

alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms total

realloc
15 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 16 ms 0 ms 15 ms
reuse: 0% - 62 ms total
alloc : realloc = 1280

alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms total

realloc
16 ms 0 ms 15 ms 0 ms 0 ms 16 ms 0 ms 15 ms 0 ms 16 ms
reuse: 0% - 78 ms total
alloc : realloc = 2560

alloc --------------------------------------
0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 31 ms total

realloc
16 ms 0 ms 16 ms 0 ms 15 ms 16 ms 0 ms 15 ms 16 ms 0 ms
reuse: 0% - 94 ms total
alloc : realloc = 5120
ok?
0.47 for len=0.94 for len=0.47 for len=0.32 for len=0.00 for len=0.21 for len=0.33 for len=

felipe

  • Member
  • ****
  • Posts: 943
  • Eagles are just great!
Re: HeapAlloc vs HeapReAlloc
« Reply #18 on: April 03, 2018, 04:02:13 AM »
JJ, how much ram do you have?  :idea:
Felipe.

mikeburr

  • Regular Member
  • *
  • Posts: 31
Re: HeapAlloc vs HeapReAlloc
« Reply #19 on: April 03, 2018, 04:25:30 AM »
first incarnation of heaptest under linux mint 18 .. wine ..  AMD 1 GHz dual core
alloc ...approx 130 ... realloc ... approx 1500
regards mikeb

felipe

  • Member
  • ****
  • Posts: 943
  • Eagles are just great!
Re: HeapAlloc vs HeapReAlloc
« Reply #20 on: April 03, 2018, 04:28:58 AM »
Code: [Select]
alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms
reuse: 0% - 32 ms total
alloc : realloc = 0.47 for len=80

alloc --------------------------------------
0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 15 ms total

realloc
0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms
reuse: 0% - 31 ms total
alloc : realloc = 0.48 for len=160

alloc --------------------------------------
0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 32 ms total

realloc
0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms
reuse: 0% - 15 ms total
alloc : realloc = 2.13 for len=320

alloc --------------------------------------
16 ms 0 ms 0 ms 0 ms 0 ms 15 ms 0 ms 0 ms 0 ms 0 ms 31 ms total

realloc
16 ms 0 ms 0 ms 15 ms 0 ms 16 ms 0 ms 0 ms 16 ms 0 ms
reuse: 0% - 63 ms total
alloc : realloc = 0.49 for len=640

alloc --------------------------------------
15 ms 0 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 47 ms total

realloc
0 ms 15 ms 0 ms 16 ms 0 ms 15 ms 0 ms 16 ms 0 ms 16 ms
reuse: 0% - 78 ms total
alloc : realloc = 0.60 for len=1280

alloc --------------------------------------
15 ms 0 ms 16 ms 0 ms 0 ms 0 ms 0 ms 16 ms 0 ms 0 ms 47 ms total

realloc
15 ms 16 ms 0 ms 15 ms 16 ms 0 ms 16 ms 15 ms 0 ms 16 ms
reuse: 0% - 109 ms total
alloc : realloc = 0.43 for len=2560

alloc --------------------------------------
16 ms 15 ms 16 ms 0 ms 0 ms 0 ms 0 ms 0 ms 0 ms 16 ms 63 ms total

realloc
16 ms 15 ms 16 ms 16 ms 15 ms 16 ms 15 ms 32 ms 15 ms 16 ms
reuse: 0% - 172 ms total
alloc : realloc = 0.37 for len=5120
ok?

Same machine as before.
Felipe.

jj2007

  • Member
  • *****
  • Posts: 8776
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #21 on: April 03, 2018, 06:19:28 AM »
My results for Win10 on a slow machine. I really don't know how to interpret these results: damn slow on Win7, no reuse of heap memory on Win10, pretty fast on XP in a virtual machine ::)

Win10, Celeron cpu:
Code: [Select]
alloc --------------------------------------
0 ms 16 ms 0 ms 0 ms 16 ms 0 ms 15 ms 16 ms 0 ms 16 ms 79 ms total

realloc
15 ms 16 ms 0 ms 15 ms 16 ms 16 ms 0 ms 15 ms 0 ms 16 ms
reuse: 0% - 109 ms total
alloc : realloc = 0.72 for len=80

alloc --------------------------------------
0 ms 15 ms 0 ms 16 ms 0 ms 16 ms 0 ms 15 ms 0 ms 16 ms 78 ms total

realloc
0 ms 16 ms 15 ms 0 ms 16 ms 32 ms 15 ms 0 ms 16 ms 0 ms
reuse: 0% - 110 ms total
alloc : realloc = 0.71 for len=160

alloc --------------------------------------
15 ms 0 ms 16 ms 0 ms 15 ms 0 ms 16 ms 0 ms 0 ms 15 ms 77 ms total

realloc
0 ms 31 ms 0 ms 15 ms 0 ms 16 ms 16 ms 16 ms 16 ms 15 ms
reuse: 0% - 125 ms total
alloc : realloc = 0.62 for len=320

alloc --------------------------------------
16 ms 15 ms 16 ms 16 ms 15 ms 16 ms 16 ms 0 ms 15 ms 16 ms 141 ms total

realloc
15 ms 32 ms 15 ms 16 ms 16 ms 15 ms 16 ms 15 ms 16 ms 0 ms
reuse: 0% - 156 ms total
alloc : realloc = 0.90 for len=640

alloc --------------------------------------
15 ms 16 ms 16 ms 15 ms 16 ms 0 ms 0 ms 16 ms 16 ms 15 ms 125 ms total

realloc
32 ms 31 ms 31 ms 16 ms 15 ms 32 ms 31 ms 15 ms 32 ms 15 ms
reuse: 0% - 250 ms total
alloc : realloc = 0.50 for len=1280

alloc --------------------------------------
32 ms 15 ms 16 ms 15 ms 16 ms 0 ms 16 ms 15 ms 16 ms 16 ms 157 ms total

realloc
15 ms 31 ms 32 ms 31 ms 31 ms 31 ms 32 ms 31 ms 16 ms 31 ms
reuse: 0% - 281 ms total
alloc : realloc = 0.56 for len=2560

alloc --------------------------------------
47 ms 63 ms 15 ms 0 ms 16 ms 15 ms 16 ms 16 ms 15 ms 16 ms 219 ms total

realloc
47 ms 47 ms 46 ms 47 ms 47 ms 47 ms 47 ms 47 ms 47 ms 47 ms
reuse: 0% - 469 ms total
alloc : realloc = 0.47 for len=5120
ok?

WinXP VM with Core i5:
Code: [Select]
alloc --------------------------------------
10 ms 20 ms 20 ms 10 ms 10 ms 10 ms 10 ms 20 ms 10 ms 10 ms 130 ms total

realloc
20 ms 10 ms 10 ms 20 ms 10 ms 10 ms 10 ms 10 ms 10 ms 10 ms
reuse: 60% - 120 ms total
alloc : realloc = 1.08 for len=320

alloc --------------------------------------
20 ms 70 ms 70 ms 50 ms 40 ms 30 ms 50 ms 41 ms 40 ms 40 ms 451 ms total

realloc
20 ms 10 ms 20 ms 10 ms 20 ms 10 ms 10 ms 20 ms 10 ms 20 ms
reuse: 59% - 150 ms total
alloc : realloc = 3.01 for len=640

alloc --------------------------------------
30 ms 370 ms 101 ms 70 ms 100 ms 90 ms 50 ms 60 ms 60 ms 50 ms 981 ms total

realloc
20 ms 20 ms 20 ms 10 ms 20 ms 20 ms 20 ms 20 ms 11 ms 20 ms
reuse: 58% - 181 ms total
alloc : realloc = 5.42 for len=1280

alloc --------------------------------------
50 ms 500 ms 151 ms 90 ms 80 ms 80 ms 60 ms 70 ms 50 ms 60 ms 1191 ms total

realloc
30 ms 30 ms 20 ms 30 ms 20 ms 20 ms 20 ms 31 ms 20 ms 20 ms
reuse: 59% - 241 ms total
alloc : realloc = 4.94 for len=2560

alloc --------------------------------------
80 ms 90 ms 90 ms 80 ms 70 ms 50 ms 70 ms 50 ms 61 ms 50 ms 691 ms total

realloc
50 ms 30 ms 40 ms 40 ms 40 ms 30 ms 40 ms 40 ms 40 ms 40 ms
reuse: 59% - 390 ms total
alloc : realloc = 1.77 for len=5120

JJ, how much ram do you have?  :idea:
8GB

Siekmanski

  • Member
  • *****
  • Posts: 1674
Re: HeapAlloc vs HeapReAlloc
« Reply #22 on: April 03, 2018, 07:21:37 AM »
Don't know how HeapAlloc and HeapReAlloc work internally...
Maybe windows does reserve much more memory, in case a realloc is needed, using some kind of priority system and giving the extra unused reserved memory back when needed?
Looking at the time intervals between allocations this could be plausible, just a thought.
Creative coders use backward thinking techniques as a strategy.

jj2007

  • Member
  • *****
  • Posts: 8776
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #23 on: April 03, 2018, 07:42:12 AM »
Thanks, Marinus.

There is a thread at SOF: Reason for ~100x slowdown with heap memory functions using HEAP_NO_SERIALIZE, and M$ writes about Low-fragmentation Heap

Both are good reads, and almost unrelated to my problem, but the second one contains a phrase that I found interesting:
Quote
The LFH also cannot be enabled if you are using the heap debugging tools in Debugging Tools for Windows

So I checked my source and found OPT_Debug 1, which tells RichMasm to use... guess what... Debugging Tools for Windows. Mystery solved, the heap gets extra fast again when disabling that option :icon_redface:

I added a warning to the old Detecting and debugging heap corruption thread.

Maybe windows does reserve much more memory, in case a realloc is needed, using some kind of priority system and giving the extra unused reserved memory back when needed?

It used to work that way, see the reuse figures above, but one interesting result is that on Win10, there is no reuse of the same buffer with HeapReAlloc.

Thanks to everybody, and apologies that I wasted your time :icon14:

felipe

  • Member
  • ****
  • Posts: 943
  • Eagles are just great!
Re: HeapAlloc vs HeapReAlloc
« Reply #24 on: April 03, 2018, 10:38:19 AM »
apologies that I wasted your time :icon14:

What are you talking about? Just by doing some research we learn. i like to do some research and helping to do it too.  8)
Felipe.

LordAdef

  • Member
  • ****
  • Posts: 590
Re: HeapAlloc vs HeapReAlloc
« Reply #25 on: April 03, 2018, 11:25:51 AM »
+1