Skip to content

Commit ca1dfcd

Browse files
committed
Make mimalloc opt-in by default
This commit switches the mimalloc feature to be opt-in by default everywhere. Since building mimalloc requires a C compiler and this does significantly change the requirements for building Qiskit from source, making it opt-in for people that want it is the best course of action for now. This commit adds a new environment variable `QISKIT_BUILD_WITH_MIMALLOC=1` which is used to enable building mimalloc when building Qiskit. The CI jobs are updated to mix mimalloc and not in Python test jobs and also to always enable mimalloc in wheel builds for release.
1 parent c91f1ae commit ca1dfcd

10 files changed

Lines changed: 66 additions & 14 deletions

File tree

.github/workflows/branch-protection.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ jobs:
106106
install-optionals: true
107107
install-from-sdist: false
108108
qiskit-pycache: false
109+
mimalloc: false
109110
- id: "Python newest"
110111
python: ${{ needs.config.outputs.python-new }}
111112
install-optionals: false
@@ -127,6 +128,7 @@ jobs:
127128
- id: "Python oldest"
128129
python: ${{ needs.config.outputs.python-old }}
129130
install-optionals: true
131+
mimalloc: false
130132
- id: "Python newest"
131133
python: ${{ needs.config.outputs.python-new }}
132134
install-optionals: false
@@ -146,6 +148,7 @@ jobs:
146148
- id: "Python oldest"
147149
python: ${{ needs.config.outputs.python-old }}
148150
install-optionals: true
151+
mimalloc: false
149152
- id: "Python newest"
150153
python: ${{ needs.config.outputs.python-new }}
151154
install-optionals: false

.github/workflows/test-linux.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ on:
2929
structures when building Qiskit.
3030
type: boolean
3131
default: true
32+
mimalloc:
33+
description: >
34+
Whether to build with mimalloc or not
35+
type: boolean
36+
default: true
3237

3338
jobs:
3439
unit-tests:
@@ -61,6 +66,9 @@ jobs:
6166
- name: Set gate-caching settings for build
6267
if: ${{ !inputs.qiskit-pycache }}
6368
run: 'echo "QISKIT_NO_CACHE_GATES=1" >> $GITHUB_ENV'
69+
- name: Set mimalloc for build
70+
if: ${{ inputs.mimalloc }}
71+
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
6472
- name: Install Qiskit from sdist
6573
if: ${{ inputs.install-from-sdist }}
6674
run: |

.github/workflows/test-mac.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ on:
88
Python version to currently test
99
type: string
1010
required: true
11-
1211
install-optionals:
1312
description: >
1413
Decides whether we install optyional dependencies
1514
type: boolean
1615
default: false
17-
1816
runner:
1917
description: >
2018
Describes the system this workflow should run on.
2119
type: string
2220
required: true
21+
mimalloc:
22+
description: >
23+
Whether to build with mimalloc or not
24+
type: boolean
25+
default: true
26+
2327
jobs:
2428
tests-mac:
2529
name: ${{ inputs.runner }} - Python ${{ inputs.python-version }}
@@ -42,6 +46,9 @@ jobs:
4246
stestr | "${{ runner.os }}"
4347
stestr
4448
path: .stestr
49+
- name: Set mimalloc for build
50+
if: ${{ inputs.mimalloc }}
51+
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
4552
- name: "Install dependencies"
4653
run: |
4754
set -e

.github/workflows/test-windows.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ on:
88
Python version to currently test
99
type: string
1010
required: true
11-
1211
runner:
1312
description: >
1413
Describes the system this workflow should run on.
1514
type: string
1615
required: true
17-
1816
install-optionals:
1917
description: >
2018
Decides whether we install optyional dependencies
2119
type: boolean
2220
default: false
21+
mimalloc:
22+
description: >
23+
Whether to build with mimalloc or not
24+
type: boolean
25+
default: true
2326
jobs:
2427
test-windows:
2528
name: ${{ inputs.runner }} - Python ${{ inputs.python-version }}
@@ -42,6 +45,9 @@ jobs:
4245
stestr | "${{ runner.os }}"
4346
stestr
4447
path: .stestr
48+
- name: Set mimalloc for build
49+
if: ${{ inputs.mimalloc }}
50+
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
4551
- name: Install dependencies
4652
run: |
4753
python -m venv test-job

.github/workflows/wheels-build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ jobs:
108108
cat >>"$GITHUB_ENV" <<EOF
109109
CIBW_BEFORE_ALL_LINUX=dnf install -y wget && {package}/tools/install_rust.sh
110110
CIBW_BEFORE_BUILD=bash ./tools/build_pgo.sh $PGO_WORK_DIR $PGO_OUT_PATH
111-
CIBW_ENVIRONMENT=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function'
112-
CIBW_ENVIRONMENT_MACOS=MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function'
113-
CIBW_ENVIRONMENT_LINUX=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' PATH="\$PATH:\$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true"
111+
CIBW_ENVIRONMENT=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' QISKIT_BUILD_WITH_MIMALLOC=1
112+
CIBW_ENVIRONMENT_MACOS=MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' QISKIT_BUILD_WITH_MIMALLOC=1
113+
CIBW_ENVIRONMENT_LINUX=RUSTUP_TOOLCHAIN=stable QISKIT_BUILD_WITH_MIMALLOC=1 RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' PATH="\$PATH:\$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true"
114114
EOF
115115
env:
116116
PGO_WORK_DIR: ${{ github.workspace }}/pgo-data

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ to `build_rust` overrides this default.
166166
### Compile time options
167167

168168
When building qiskit from source there are options available to control how
169-
Qiskit is built. Right now the only option is if you set the environment
169+
Qiskit is built. The first option is if you set the
170+
environment variable `QISKIT_BUILD_WITH_MIMALLOC=1` this will enable using
171+
[mimalloc](https://github.com/microsoft/mimalloc) as the global allocator for
172+
Qiskit. This improves the runtime performance of Qiskit but will require having
173+
a C compiler installed when building Qiskit. If you set the environment
170174
variable `QISKIT_NO_CACHE_GATES=1` this will disable runtime caching of
171175
Python gate objects when accessing them from a `QuantumCircuit` or `DAGCircuit`.
172176
This makes a tradeoff between runtime performance for Python access and memory

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ ifneq ($(OS), Windows_NT)
1414
OS := $(shell uname -s)
1515
endif
1616

17+
ifeq ($(QISKIT_BUILD_WITH_MIMALLOC), 1)
18+
MIMALLOC := --features=mimalloc
19+
endif
20+
1721
.PHONY: default ruff env lint lint-incr style black test test_randomized pytest pytest_randomized test_ci coverage coverage_erase clean
1822

1923
default: style lint-incr test ;
@@ -135,7 +139,7 @@ fix_cformat:
135139
# instead.
136140
.PHONY: build-clib build-clib-release build-clib-dev
137141
build-clib:
138-
cargo rustc -p qiskit-cext --features=mimalloc --crate-type cdylib ${C_LIB_CARGO_FLAGS} -- ${C_LIB_RUSTC_FLAGS}
142+
cargo rustc -p qiskit-cext ${MIMALLOC} --crate-type cdylib ${C_LIB_CARGO_FLAGS} -- ${C_LIB_RUSTC_FLAGS}
139143
build-clib-release: C_LIB_CARGO_FLAGS=--release
140144
build-clib-release: build-clib
141145
build-clib-dev: C_LIB_CARGO_FLAGS=--profile dev

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,16 @@ before-test = "pip install --only-binary=numpy,scipy numpy scipy -c constraints.
299299
# specific job override is needed. For example tier 1 platforms locally override
300300
# the before-build and environment configuration to enable PGO,
301301
# see: .github/workflows/wheels.yml for the jobs where this is done
302-
environment = 'RUSTUP_TOOLCHAIN="stable"'
302+
environment = 'RUSTUP_TOOLCHAIN="stable" QISKIT_BUILD_WITH_MIMALLOC=1'
303303

304304
[tool.cibuildwheel.linux]
305305
before-all = "yum install -y wget && {package}/tools/install_rust.sh"
306-
environment = 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable"'
306+
environment = 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable" QISKIT_BUILD_WITH_MIMALLOC=1'
307307
repair-wheel-command = "auditwheel repair -w {dest_dir} {wheel} && pipx run abi3audit --strict --report {wheel}"
308308

309309
[tool.cibuildwheel.macos]
310310
repair-wheel-command = "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel} && pipx run abi3audit --strict --report {wheel}"
311-
environment = "MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable"
311+
environment = "MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable QISKIT_BUILD_WITH_MIMALLOC=1"
312312

313313
[tool.cibuildwheel.windows]
314314
test-command = "cp -r {project}/test . && stestr --test-path test/python run --abbreviate"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
features_misc:
3+
- Qiskit can now optionally be built with `mimalloc <https://github.com/microsoft/mimalloc>`__ as the global
4+
allocator. This improves the runtime performance of Qiskit and also should
5+
reduce the resident set size when running Qiskit. By default the precompiled
6+
wheels on PyPI will are built with mimalloc, when building from source you
7+
need to opt-in and enable using mimalloc. Enabling mimalloc when building
8+
from source will require a C compiler to be installed as mimalloc will be
9+
built from source.
10+
build:
11+
- A new environment variable, ``QISKIT_BUILD_WITH_MIMALLOC`` is used to
12+
control whether to build Qiskit using mimalloc as the global allocator.
13+
When ``QISKIT_BUILD_WITH_MIMALLOC=1`` this will enable using mimalloc for
14+
both building the Python package and using the Makefile to build the
15+
C API as a standalone shared library with ``make c``. If enabled building
16+
Qiskit from source a C compiler is required to build
17+
`mimalloc <https://github.com/microsoft/mimalloc>`__.

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ def get_package_dir(package: str) -> Path:
131131
# is all in rust (default to no caching and make caching an opt-in feature). This is opt-out
132132
# right now to avoid the runtime overhead until we are leveraging the rust gates infrastructure.
133133
if os.getenv("QISKIT_NO_CACHE_GATES") == "1":
134-
features = ["mimalloc"]
134+
features = []
135135
else:
136-
features = ["cache_pygates", "mimalloc"]
136+
features = ["cache_pygates"]
137+
138+
if os.getenv("QISKIT_BUILD_WITH_MIMALLOC") == "1":
139+
features.append("mimalloc")
137140

138141

139142
setup(

0 commit comments

Comments
 (0)