Skip to content

Build pyopenms against the python limited/stable ABI if possible #8185

Closed

Description

@jpfeuffer

Report

Such that we do not need to build for 5 py versions all the time. Bioconda is never going to figure this out.. sigh..

Docs how to do it:
https://cython.readthedocs.io/en/stable/src/userguide/limited_api.html

Activity

  1. github-actions commented on Feb 22, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  2. jpfeuffer commented on Mar 10, 2026

    @jpfeuffer
    ContributorAuthor

    We should revisit this with nanobind. I remember in Cython there were some smaller caveats including a little bit of performance impacts.
    We should check what the state for nanobind is and consider it. Would reduce number of builds and wheels to ship significantly.

  3. timosachsenberg commented on Mar 16, 2026

    @timosachsenberg
    Contributor

    Stable ABI Feasibility Analysis (nanobind)

    Following up on @jpfeuffer's suggestion to revisit this with nanobind — here's a research summary.

    nanobind supports stable ABI natively

    nanobind provides a STABLE_ABI flag on nanobind_add_module(), requiring Python 3.12+. Once enabled, extensions get the .abi3.so suffix and a single wheel works across Python minor versions.

    Current pyOpenMS compatibility is high

    • All 13 bind_*.cpp files use zero direct CPython C API — they're pure nanobind abstractions and need no changes
    • arrow_zerocopy.cpp also uses only nanobind abstractions — no changes needed
    • The only files needing changes are 5 type caster headers in bindings/type_casters/

    CPython API usage in type casters is mostly stable-ABI-compatible

    The initial concern was ~248 direct CPython C API calls across the type casters. However, most functions used (PyList_New, PyDict_New, PySequence_GetItem, PyUnicode_AsUTF8AndSize, Py_DECREF, PyXxx_Check, etc.) are all part of the stable ABI as of Python 3.12.

    The only real incompatibility found:

    • PyList_SET_ITEM() (~8 occurrences) → must be replaced with PyList_SetItem() (stable ABI equivalent, also steals references)

    Trade-offs

    Factor Impact
    Minimum Python version Rises from 3.10 → 3.12
    Wheel builds 1 per platform instead of 1 per Python version (5→1)
    Performance Small cost — nanobind can no longer access CPython struct internals directly
    Code changes ~8 PyList_SET_ITEM → PyList_SetItem replacements in type casters + CMake flag additions
    nanobind issue #1286 Vectorcall limitations on Python < 3.14 under stable ABI (minor)

    Open question

    Is dropping Python 3.10/3.11 acceptable? Two options:

    • (a) Raise minimum to 3.12, ship only abi3 wheels (max benefit, simplest)
    • (b) Hybrid: abi3 wheels for 3.12+, version-specific wheels for 3.10/3.11 (reduces builds from 5 to 3)

    Estimated effort

    This is a relatively small change — primarily adding STABLE_ABI to the ~13 nanobind_add_module() calls in CMake and replacing 8 macro calls in type casters. The NB_DOMAIN "pyopenms" cross-module type sharing should work with stable ABI as nanobind handles this internally. Testing across Python versions would be the main effort.

  4. jpfeuffer commented on Mar 16, 2026

    @jpfeuffer
    ContributorAuthor

    What about performance? I know in Cython enabling this comes with some performance hits since it cannot optimize for certain python versions.

  5. jpfeuffer commented on Mar 16, 2026

    @jpfeuffer
    ContributorAuthor

    Ah here:

    Performance Small cost — nanobind can no longer access CPython struct internals directly
  6. timosachsenberg commented on Mar 16, 2026

    @timosachsenberg
    Contributor

    I can try to add a benchmark.

  7. timosachsenberg commented on Mar 16, 2026

    @timosachsenberg
    Contributor

    how to handle conda? there we need to build from scratch right?

  8. timosachsenberg commented on Mar 16, 2026

    @timosachsenberg
    Contributor

    Stable ABI Benchmark Results

    Ran benchmarks sequentially (10 repeats each) on the same machine, comparing non-abi3 vs abi3 builds. Branch: feature/stable-abi-pyopenms.

    Summary

    Overall mean delta: +12.0% — in the yellow zone (5-15% threshold).

    The regression is not in our code changes (type casters, CMake flags). It's in nanobind's per-call dispatch overhead under stable ABI — every Python→C++ call goes through an indirection layer.

    Results by category

    Category Delta Verdict
    Type Casters (our custom casters: DataValue, String, STL) -18% to -30% (faster) PASS
    Type Conversions (getMetaValue, getNativeID, etc.) -8% to -22% (faster) PASS
    Zero-copy paths (get_data_view, np.mean on views) ~0% PASS
    DataFrame Export (to_df bulk operations) -4% to +1% PASS
    Arrow Export (spectra_to_arrow, to_arrow) +20-35% FAIL
    Per-call overhead (Peak1D construction, __getitem__, push_back) +25-50% FAIL
    Spectrum Access (get_peaks, iterate spectra) +23-50% FAIL

    Key observations

    1. Our PyList_SET_ITEM → PyList_SetItem changes have zero negative impact. Type caster round-trips are actually faster under abi3.

    2. The regression is entirely in nanobind's per-call dispatch. Operations that make many small Python→C++ calls (constructing 10k objects, per-element __getitem__, Arrow export which iterates per-spectrum) see 25-50% overhead.

    3. Bulk operations are unaffected. DataFrame exports, large rasterizations, and zero-copy memory views show no regression — the time is spent in C++, not in the binding layer.

    4. Arrow export regression (~25%) is because the C++ Arrow bridge makes per-spectrum calls through nanobind. The Python-side to_df() wrapper (which does bulk work) is unaffected.

    Recommendation

    This is a yellow light result — the overhead is real but concentrated in micro-benchmarks that stress the binding layer. Real-world pyOpenMS workloads that load files, process spectra in bulk, and export DataFrames would see minimal impact. The main pain point is Arrow export (~25% slower) and tight loops constructing many small objects.

    Options:

    • (A) Accept the overhead — the type caster paths (our main concern) are fine, and bulk operations are unaffected. The 25-50% per-call overhead is a known stable ABI trade-off.
    • (B) Wait for nanobind improvements — nanobind is actively improving stable ABI performance (vectorcall on 3.14+, etc.). Re-evaluate after nanobind 2.11+.
    • (C) Hybrid approach — only enable stable ABI on platforms where wheel count is most painful (e.g., Linux), keep full ABI on others.

    Environment

    • Python 3.12.3, nanobind 2.10.0, Linux (WSL2)
    • 10 repeats per benchmark, sequential execution
    • Full results: [compare_benchmarks.py output above]
  9. jpfeuffer commented on Mar 16, 2026

    @jpfeuffer
    ContributorAuthor

    Yes. I expected that pattern. It is more significant than I thought. Maybe caching is better?

  10. timosachsenberg commented on Mar 16, 2026

    @timosachsenberg
    Contributor

    Maybe it is not that bad. A bit hard to benchmark though… will try again.

  11. github-actions commented on Sep 13, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  12. timosachsenberg commented on Sep 21, 2026

    @timosachsenberg
    Contributor

    Resolved on develop (verified @ c5403a8) — pyOpenMS does build against the stable ABI now, so the "build for 5 py versions all the time" problem this issue was about is gone.

    src/pyOpenMS/CMakeLists.txt:44 defines PYOPENMS_SPLIT_MODE ("Build stable-ABI (abi3) extensions in nanobind split mode"), default ON; CMake turns STABLE_ABI_VERSION into Py_LIMITED_API and py-build-cmake turns abi3_minimum_cpython_version into the wheel's cp<N>-abi3 tag (:47-55), with a guard against packaging a non-abi3 local build as abi3.

    The published nightlies match: one cp311-abi3 wheel per platform, covering CPython 3.11+.

    Note this went via nanobind rather than the Cython limited-API route the issue links to, so the wontfix label no longer reflects the state. Proposing to close.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions