Created with help of AI agnet. Ran this on isolated machine therefore these results can be trusted with respect with noise.
python bench_rational_heap_types.py --build
python bench_rational_heap_types.py --core 2 --trials 15Three builds of one standalone .so, same installed numpy behind all three:
| variant | source | flag | size |
|---|---|---|---|
static |
899d0c20b (main), static PyTypeObject |
— | 65632 |
heap |
b0debb3a0 (branch), heap types |
— | 64848 |
abi3 |
b0debb3a0 (branch) |
-DPy_LIMITED_API=0x030d0000 |
60696 |
heap/static is what ships (allow_limited_api is false by default);
abi3/heap is the opt-in flag cost.
15 subprocesses per variant, order rotated and reversed each trial, ~50 ms
batches, min-of-7, median across trials, bootstrap 95% CI. Pinned to core 2,
governor=performance, boost=0. CPython 3.13.14, GCC 14.3.0, Ryzen 7 4800H.
r = rational(3,4), a = np.array([r]*8), big = np.array([r]*100_000).
| Call | static | heap | abi3 | heap/static | 95% CI | abi3/heap |
|---|---|---|---|---|---|---|
r + r |
0.058 us | 0.073 us | 0.065 us | 1.255x | [1.247, 1.266] | 0.891x |
1k construct+free |
132.05 us | 149.87 us | 149.93 us | 1.135x | [1.133, 1.137] | 1.000x |
-r |
0.047 us | 0.052 us | 0.052 us | 1.118x | [1.110, 1.135] | 0.995x |
r * r |
0.068 us | 0.075 us | 0.075 us | 1.099x | [1.095, 1.105] | 0.992x |
R(3, 4) |
0.107 us | 0.114 us | 0.119 us | 1.068x | [1.037, 1.068] | 1.041x |
isinstance(r, R) |
0.028 us | 0.030 us | 0.030 us | 1.056x | [1.046, 1.058] | 1.001x |
np.array([r]*8) |
0.897 us | 0.936 us | 0.944 us | 1.044x | [1.022, 1.065] | 1.008x |
R(r) (fast path) |
0.051 us | 0.051 us | 0.052 us | 1.004x | [1.000, 1.007] | 1.021x |
a[0] = r |
0.177 us | 0.177 us | 0.173 us | 1.000x | [0.996, 1.004] | 0.980x |
a[0] |
0.109 us | 0.110 us | 0.109 us | 1.000x | [0.998, 1.001] | 0.997x |
hash(r) |
0.043 us | 0.043 us | 0.043 us | 1.000x | [0.999, 1.004] | 1.000x |
float(r) |
0.038 us | 0.038 us | 0.038 us | 1.000x | [0.995, 1.004] | 0.998x |
r.n |
0.033 us | 0.033 us | 0.033 us | 0.999x | [0.998, 1.001] | 1.001x |
r == r |
0.035 us | 0.035 us | 0.035 us | 0.999x | [0.998, 1.001] | 1.004x |
| Call | static | heap | abi3 | heap/static | 95% CI |
|---|---|---|---|---|---|
matrix_multiply(mm, mm) |
322441 us | 322421 us | 322401 us | 1.000x | [0.999, 1.000] |
big.sum() |
2996.6 us | 3001.3 us | 2997.7 us | 1.002x | [1.001, 1.002] |
big + big |
1942.5 us | 1942.4 us | 1942.5 us | 1.000x | [0.999, 1.001] |
big.astype(np.float64) |
88.40 us | 88.43 us | 88.37 us | 1.000x | [0.999, 1.001] |
-
The cost tracks allocation.
R(r)takes the early-return branch, allocates nothing, costs nothing (1.004x);R(3,4)allocates and pays 1.068x. Same function, one branch apart. Roughly 5-18 ns per heap-type allocate/free cycle —Py_INCREF(type)inPyType_GenericAlloc,Py_DECREF(tp)in the newtp_dealloc, plus itsPyType_GetFlags()call. -
Quote nanoseconds, not ratios.
r + r(1.255x) anda[0](1.000x) both allocate one rational. The difference is 58 ns of surrounding work versus 109 ns. -
Bulk is unaffected: 1.000x on three of four controls, so the per-call numbers are trustworthy.
big.sum()at 1.002x is 0.2% on a pure C loop with no mechanism to reach it — not quotable. -
The abi3 flag is near-free here, unlike the Cython result in
results.md(13-18%). Hand-written C emits far less refcounting per call than Cython, so it barely pays the out-of-lining tax.
Not a blocker. _rational_tests is test-only, no user path allocates
rationals, and array work is flat.
The result that matters is for row 13: this is the first measurement of
what heap-type conversion costs NumPy's scalar types, and np.float64(1) + np.float64(2) is the same shape as r + r. Budget ~5-18 ns per scalar
allocation, 0% bulk when planning _multiarray_umath.
If the per-call cost ever needs reducing, the PyType_GetFlags() call in
pyrational_dealloc can be short-circuited by a pointer compare against the
two known non-GC types. Left simple deliberately.