Bug #101024 GCC malloc function attribute
Submitted: 1 Oct 2020 12:55 Modified: 9 Oct 2020 8:00
Reporter: Georgy Kirichenko Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Compiling Severity:S5 (Performance)
Version:8.0 OS:Any
Assigned to: Marc ALFF CPU Architecture:Any
Tags: Contribution

[1 Oct 2020 12:55] Georgy Kirichenko
Description:
GCC supports a malloc function attribute (https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attribu...) which tells that a function allocates a new unused memory chunk which allows compiler to do some optimizations.

According to my investigation such attribute, applied on MySQL allocation functions like my_malloc, me_realloc, my_memdump, my_str(n)dup and some others, improves performance (sysbench used).

Unfortunately the attribute is supported by GCC only (clang has similar attribute - allocator - but it is not used for optimization purposes), however MY_ATTRIBUTE macro able to handle it.

So I would like to suggest to mark all memory allocation function with the attribute.

How to repeat:
Annotate all allocation function with MY_ATTRIBUTE((malloc)) and compare sysbench results
[1 Oct 2020 12:56] Georgy Kirichenko
I will submit corresponding sysbench results ASAP.
[1 Oct 2020 13:25] MySQL Verification Team
Hello Georgy Kirichenko,

Thank you for the report and contribution.

regards,
Umesh
[1 Oct 2020 14:54] Georgy Kirichenko
Thanks Umesh!

I made a mistake - function my_realloc obviously should not be marked with malloc attribute as it is not necessarily that my_realloc allocates a new chunk of memory. I will submit updated contribution ASAP.
[9 Oct 2020 7:57] Georgy Kirichenko
Below is table containing single-thread sysbench results showing performance improvement with patch applied on a X86_64 system

|   test  | threads | vanilla       | malloc_attr  |  diff |
|:-------:|--------:|--------------:|-------------:|------:|
| OLTP_PS |       1 |      19407.75 |     19641.70 | 1.21% |
| OLTP_RO |       1 |        738.05 |       739.01 | 0.13% |
| OLTP_RW |       1 |        366.87 |       367.91 | 0.28% |
[9 Oct 2020 8:00] Georgy Kirichenko
The table above was got on MySQL 8.0 compiled with -O3 because with -O2 the performance gain is modest so I am working on highlighting concrete optimization flag set allowing MY_ATTRIBUTE((malloc)) do the job.
[9 Oct 2020 15:57] Marc ALFF
Point noted about realloc and the revised patch.

Thanks for the contribution.