Hi Rich,

Thanks for your time.

Totally agreed that in realworld application, it would take way more time to process that huge bulk of memory, compared with the  average 3 microsecond per malloc&free.


At 2022-09-22 01:58:17, "Rich Felker" <dalias@libc.org> wrote:

>
>
>> Your test case, with the completely random size distribution across
>> various large sizes, is likely a worst case. The mean size you're
>> allocating is 128k, which is the threshold for direct mmap/munmap of
>> each allocation, so at least half of the allocations you're making can
>> *never* be reused, and will always be immediately unmapped on free. It
>> might be interesting to change the scaling factor from 1k to 256 bytes
>> so that basically all of the allocation sizes are in the >> malloc-managed range.
>
>One observation if this change is made: it looks like at least 70% of
>the time is spent performing madvise(MADV_FREE), and that a large
>portion of the rest (just looking at strace) seems to be repeatedly
>mapping and freeing a 17-page (68k) block, probably because this size
>happens to be at the boundary of some threshold where bounce
>protection isn't happening. I think we should look at both of these in
>more detail, since they both suggest opportunities for large
>performance improvements at low cost.
>
I have made several profiling, the report indeed show that as the size decreased, performance went up significantly and madvise now take major portion of time, as you suggested,
also madviser's portion decrease as size decrease, when average size reach to 2K, madviser was only picked by profiler less than 2%:
1. average 64K(1K~128K) malloc/free
# time ./m.alpine
real    1m 50.12s
user    0m 39.80s
sys    1m 10.17s
2. average 32K (1K~64K) malloc/free
# time ./m.alpine
real    1m 12.89s
user    0m 30.62s
sys    0m 41.91s

3. average 8K (1K~16K)
# time ./m.alpine
real    0m 42.35s
user    0m 22.94s
sys    0m 19.27s
4. average 4K (1k~8K)
# time ./m.debian
real    0m32.477s
user    0m31.829s
sys    0m0.596s
5 average 2K(128B~4096B) (madviser only about 1.7%)
# time ./m.alpine
real    0m 13.02s
user    0m 12.68s
sys    0m 0.26s
6. average 1K (128B~2048B)
# time ./m.alpine
real    0m 10.75s
user    0m 10.68s
sys    0m 0.01s


David