According to the Performance penalty from the Static Linux SDK post I would like to provide a step-by-step instruction of how to patch static-linux-sdk with mimalloc
It is important for swift apps – especially for server-side apps – to be performant
Great question! Maybe in the future — who knows?
If you're from the future, you can check your SDK by running
llvm-nm libc.a | grep 'mi_'If it returns nothing, then mimalloc hasn't been integrated yet — and you can integrate it by following this instruction
If it returns a lot of mi_, then congrats — you've already got mimalloc in your SDK 🎉
You are doing it at your own risk 😱
x86_64 and arm64 ubuntu/debian environment
Or docker with QEMU, but I had no luck with it yet. So I used x86_64 and arm64 machines on Hetzner Cloud
static-linux-sdk gives you the ability to compile into x86 and ARM architectures, it means that it has two versions of libc.a, one for each platform
We have to patch both
I assume you already have it installed on your machine, but if not then install or just download it
swift sdk install <URL> --checksum <CHECKSUM>- Go to swift.org
- Choose Ubuntu or Debian
- Choose version of OS
4.1. Hit the
Download Linux Static SDKbutton 4.2 Or copy link and download it withcurle.g.curl -LO <URL>
Let's assume you downloaded swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz
Then, execute
tar -xzf swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gzCreate x86_64 and aarch64 directories
mkdir x86_64 && mkdir aarch64Copy both versions of libc.a into these directories
If you have it installed then it is located at ~/.swiftpm/swift-sdks
If not then it is where you extracted it
cp swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle/swift-6.1-RELEASE_static-linux-0.0.1/swift-linux-musl/musl-1.2.5.sdk/x86_64/usr/lib/libc.a ./x86_64/libc.a
cp swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle/swift-6.1-RELEASE_static-linux-0.0.1/swift-linux-musl/musl-1.2.5.sdk/aarch64/usr/lib/libc.a ./aarch64/libc.aDo the following steps on both x86_64 and arm64 machines
# install required packages
apt update
apt install -y llvm software-properties-common build-essential libssl-dev clangNote
mimalloc requires cmake >= 3.18.0
Check your cmake version
cmake --versionIf it is lower, then install 3.27.9 from sources by following these steps
# download cmake
curl -LO https://github.com/Kitware/CMake/releases/download/v3.27.9/cmake-3.27.9.tar.gz
# extract cmake
tar -xf cmake-3.27.9.tar.gz
# go to cmake dir
cd cmake-3.27.9
# compile cmake
./bootstrap --prefix=$HOME/.local
make -j$(nproc)
make install
# add cmake into PATH
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# check cmake version
cmake --version
# go back to the parent dir
cd ../- Download
mimalloc_v2.1.7.diffandpatch_musl.sh
curl -LO https://gist.github.com/MihaelIsaev/fbb0135e9b1bb1d2691bb71a30528bc6/raw/4448da386e6d0dbd704716091be4818e56ba3d28/mimalloc_v2.1.7.diff
curl -LO https://gist.github.com/MihaelIsaev/fbb0135e9b1bb1d2691bb71a30528bc6/raw/4448da386e6d0dbd704716091be4818e56ba3d28/patch_musl.sh- Make
patch_musl.shexecutable
chmod +x ./patch_musl.sh- Upload
libc.afor the current CPU architecture
It will create libc.a.bak backup of the original file
./patch_musl.sh🥳 Now you have successfully patched libc.a with mimalloc
Tip
Don't forget to patch it for both architectures
I assume you have it installed, so copy proper libc.a files into their usr/lib directories
No, mimalloc has been merged into libc.a
I tried to do it but had no luck yet. You are welcome to implement it and update this instruction 🤝
Credit to Sebastian Toivonen, aka MarSe32m and his post
🫡 Enjoy!