又是一个被升级搞到失眠的夜晚
刚好 GitHub 通知收到了 mimalloc_rust 项目发新的 release 的通知,就顺着一路看下去,没想到这一趟收获不小:
首先惊讶地发现 mimalloc 已经在开发 v3 版本了。新版本的优化方向如下:
然后重新追了一下 mimalloc 将本地 patch 往上游 cpython 同步的进度,发现至少当前识别到的 patch 都移植完了,而且 Python 已经在 3.13 正式打包了 mimalloc
再一看https://github.com/astral-sh/python-build-standalone 这边已经显式地要求 3.13 以后所有的构建使用 mimalloc 了。扫了一眼 commit history,把看起来有点意思的 commits 都打开看了下,从下面这条 commit 注意到一件事:
https://github.com/astral-sh/python-build-standalone/commit/cddb9c19fcbb656b5cda420dcd6c3bf196e53beb (Disable unsafe identical code folding in BOLT)
稍微追了下上下文,明白了原来编译时指定 --icf=all 可能导致什么样的问题(简单来说这一优化其实违反了 C/C++ 的语言规范。规范中,取两个不同函数的指针必须得到两个不同的指针值。然而 --icf=all 会激进地把所有能合并的函数都合并了,所以不同地方的相同函数会取到相同的指针值
编译时指定 --icf=all 是很早以前就被提及的最佳实践了,我抄了很多年,没想到现在最佳实践又变了……现在就去把我指定过 --icf=all 的地方都找出来改了
刚好 GitHub 通知收到了 mimalloc_rust 项目发新的 release 的通知,就顺着一路看下去,没想到这一趟收获不小:
首先惊讶地发现 mimalloc 已经在开发 v3 版本了。新版本的优化方向如下:
This version simplifies the lock-free ownership of previous versions, and improves sharing of memory between threads. On certain large workloads this version may use (much) less memory.
然后重新追了一下 mimalloc 将本地 patch 往上游 cpython 同步的进度,发现至少当前识别到的 patch 都移植完了,而且 Python 已经在 3.13 正式打包了 mimalloc
再一看https://github.com/astral-sh/python-build-standalone 这边已经显式地要求 3.13 以后所有的构建使用 mimalloc 了。扫了一眼 commit history,把看起来有点意思的 commits 都打开看了下,从下面这条 commit 注意到一件事:
https://github.com/astral-sh/python-build-standalone/commit/cddb9c19fcbb656b5cda420dcd6c3bf196e53beb (Disable unsafe identical code folding in BOLT)
稍微追了下上下文,明白了原来编译时指定 --icf=all 可能导致什么样的问题(简单来说这一优化其实违反了 C/C++ 的语言规范。规范中,取两个不同函数的指针必须得到两个不同的指针值。然而 --icf=all 会激进地把所有能合并的函数都合并了,所以不同地方的相同函数会取到相同的指针值
Identical Code Folding (ICF) is a powerful optimization to reduce the size of a linker output. It merges functions that happen to be compiled to the identical machine code that behave exactly the same.
The downside of doing this is the optimization per se violates the specification of the C/C++ language specs. In these languages, taking pointers of two distinctive functions must result in two non-equivalent pointer values. However, if we optimize two distinctive functions into a single function, that resulting two pointers will have the same value.
编译时指定 --icf=all 是很早以前就被提及的最佳实践了,我抄了很多年,没想到现在最佳实践又变了……现在就去把我指定过 --icf=all 的地方都找出来改了