[MDEV-23865] malloc function attribute will improve performance Created: 2020-10-01  Updated: 2023-07-12  Resolved: 2023-07-12

Status: Closed
Project: MariaDB Server
Component/s: Server
Fix Version/s: 11.1.2, 11.2.1

Type: Task Priority: Minor
Reporter: Georgy Kirichenko Assignee: Daniel Black
Resolution: Fixed Votes: 0
Labels: beginner-friendly


 Description   

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



 Comments   
Comment by Sergei Golubchik [ 2020-10-03 ]

would you want to submit a patch or create a pull request for this?

Comment by Georgy Kirichenko [ 2020-10-07 ]

Hi Sergei!

Please give me some time to contribute, I am preparing a patch right now.

Comment by Zhao Junwang [ 2020-10-22 ]

I'v tested this by adding something below, using sysbench oltp_point_select, see no improvement.
Then I used objdump see the dissasembly code, same as before.
Any idea?

```
_attribute_((malloc))
void *my_malloc(PSI_memory_key key, size_t size, myf my_flags)
```

Comment by Georgy Kirichenko [ 2020-10-23 ]

As I discovered the effect may depend on optimization options (I got performance impact using -O3 but not with -O2) I am working right now on finding circumstances allowing gcc to improve performance if the attribute was given.

Generated at Thu Feb 08 09:25:38 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.