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

feat: use mimalloc instead of musl's mallocng for static builds #666

Merged
merged 5 commits into from
Apr 23, 2024

Conversation

dunglas
Copy link
Owner

@dunglas dunglas commented Mar 15, 2024

The default musl allocator is slow for multi-threaded apps such as FrankenPHP and triggers weird bugs such as php/php-src#13648.

The trickery in this patch (massively inspired by this article by Tweag) replaces the musl allocator by https://github.com/microsoft/mimalloc, a fast allocator created by Microsoft.

Hopefully, this will also help for #481 (comment).

@dunglas dunglas marked this pull request as draft March 15, 2024 17:15
@withinboredom
Copy link
Collaborator

Oho, gonna test this one real quick.

Copy link
Collaborator

@withinboredom withinboredom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to be much faster.

// libc:
[2024-03-15T17:58:12.419081+00:00] alert: Completed in 15.709 seconds  {"process_id":87753,"memory_peak_usage":"114 MB"}

// this patch
[2024-03-15T18:11:51.670770+00:00] alert: Completed in 40.284 seconds  {"process_id":62743,"memory_peak_usage":"110 MB"}

// vanilla musl
[2024-03-15T18:15:36.365410+00:00] alert: Completed in 51.751 seconds  {"process_id":4149,"memory_peak_usage":"110 MB"}

That being said, I did run into some issues with some caddy modules that needed to be disabled or I would get segfaults randomly.

build-static.sh Show resolved Hide resolved
@dunglas
Copy link
Owner Author

dunglas commented Mar 15, 2024

I also get random segfaults with the OpenSSL extension. This isn't ready for prime time 😅

@withinboredom
Copy link
Collaborator

Could it be the libraries are linked without mimalloc?

@dunglas
Copy link
Owner Author

dunglas commented Apr 3, 2024

@withinboredom could you give more details on the modules sefgfaulting in your tests? It looks like the OpenSSL issue may not be related to malloc after all.

@theofidry
Copy link

@dunglas why can't glibc be used there even if it means shipping something else than Alpine Linux? Or alternatively wouldn't it work with gcompat?

@dunglas
Copy link
Owner Author

dunglas commented Apr 3, 2024

@theofidry I'm not sure to follow. We provide glibc builds too (in the Docker images), but glibc doesn't support creating static builds. We have to use musl for that. See also: crazywhalecc/static-php-cli#376

@andypost
Copy link

andypost commented Apr 4, 2024

Just FYI lots of openssl tests fails in Alpinelinux with openssl (even years ago with libressl)

Current set of disabled openssl tests can be found at repo
At the end of the file new failures for # openssl 3.2.1

ext/openssl/tests/openssl_error_string_basic_openssl3.phpt
ext/openssl/tests/openssl_private_decrypt_basic.phpt
ext/openssl/tests/openssl_x509_parse_basic.phpt

@andypost
Copy link

andypost commented Apr 4, 2024

Checked where alternate *malloc is used in Alpinelinux

@withinboredom
Copy link
Collaborator

I'll give this a test again this weekend.

@arnaud-lb
Copy link

arnaud-lb commented Apr 11, 2024

@withinboredom what benchmark were you running in #666 (review) ? Is there a way I could reproduce this ? In my benchmarks the difference between glibc and musl are not as dramatic (and alternative allocators also don't do as much difference)

@withinboredom
Copy link
Collaborator

It's actually a benchmark of an app I'm building. It results in a lot of syscalls deep under the hood (lots of file access, shm, proxy generation, etc).

@dunglas dunglas marked this pull request as ready for review April 23, 2024 10:07
@dunglas
Copy link
Owner Author

dunglas commented Apr 23, 2024

I added a new experimental flag to build using mimalloc and added a CI pipeline to build a mimalloc variant automatically.

@dunglas dunglas merged commit 498294a into main Apr 23, 2024
41 checks passed
@dunglas dunglas deleted the feat/mimalloc branch April 23, 2024 12:04
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

Successfully merging this pull request may close these issues.

5 participants