Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the purpose of the --no-builtin-malloc flag? #970

Open
LXYan2333 opened this issue Dec 13, 2024 · 2 comments
Open

what is the purpose of the --no-builtin-malloc flag? #970

LXYan2333 opened this issue Dec 13, 2024 · 2 comments

Comments

@LXYan2333
Copy link

At https://github.com/microsoft/mimalloc/blob/91215a5512ed4fa916ec26349f69d44235313308/CMakeLists.txt#L392C27-L392C46 the CMake build system add --no-builtin-malloc flag. However, this make program runs slower.

examples:

https://godbolt.org/z/xsTzj9sTb

https://godbolt.org/z/MEqoaEnqW

the compiler generates unnecessary calls to malloc, free and memset.

However, this is a private link flag and does not propagate to other targets, and there is (almost?) no malloc in this library, so this should no be a big issue.

But I'm still confused, why stop using builtin malloc, while still use the builtin free and calloc etc. (I haven't read the gcc's implementation detail, but it seems mixing user-provided malloc and builtin free is dangerous)

The full gcc builtins can be found here:
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

@daanx
Copy link
Collaborator

daanx commented Dec 16, 2024

  • if you use -no-builtin-malloc flag in gcc, I believe it turns off all the __builtin_xxx functions related to malloc so there won't be a malloc matched with __builtin_free (but just free).

  • I think there is no issue in not giving this flag (when linking statically) -- just more neat to only use mimalloc. There might be an issue with something like realpath that allocates memory where we need to free that later (but I think it is still fine since mimalloc overrides all such calls anyway).

  • I don't think actual programs get slower due to lack of the optimization you point at btw. This kind of thing hardly applies in practice?

@LXYan2333
Copy link
Author

GCC and clang's document about builtin function is poor, and search in google does not provide many useful infomation.

However, tcmalloc dose not use this flag:

https://github.com/search?q=repo%3Agoogle%2Ftcmalloc%20builtin&type=code

and jemalloc only disable builtin function in tests to prevent compiler optimize out malloc calls away:

https://github.com/search?q=repo%3Ajemalloc%2Fjemalloc+builtin&type=code

intel's ProTBB suggest add --no-builtin-malloc but I suspect they misunderstood what builtin-malloc do

https://link.springer.com/book/10.1007/978-1-4842-4398-5

Image

and at least they disable all builtin malloc family.

I don't think actual programs get slower due to lack of the optimization you point at btw.

I agree because this is a private compile flag in mimalloc-obj and do not propagate to other cmake targets. But a common optimize opportunity (which I have seen mutiple times) looks like this:

https://godbolt.org/z/WTEv76vco

peopel malloc at the begining and free at end and use the pointer in the middle, but forget to remove the malloc and free when the code in the middle is refactored and the pointer is not used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants