Skip to content

0.2.0: float and numpy sizes run in C++ - #2

Merged
p-ramcharan-dev merged 2 commits into
mainfrom
float-numpy-0.2.0
Nov 10, 2026
Merged

p-ramcharan-dev merged 2 commits into
mainfrom
float-numpy-0.2.0

Conversation

@p-ramcharan-dev

Copy link
Copy Markdown
Owner

What changes

Sizes given as Python float, numpy.float64 or numpy.int64, in any mix with int, now run in C++. Results match rectpack in value and in type. In 0.1.0 these fell back to the original Python code.

This covers how gdsfactory calls rectpack: integer rectangle sizes in a numpy.float64 bin.

Input (one bin, MaxRectsBlsf, no rotation) Rectangles rectpack fastrectpack Speedup
int rectangles, numpy.float64 bin 300 0.633 s 0.0022 s 284x
int rectangles, numpy.float64 bin 1,000 20.612 s 0.0173 s 1193x
float rectangles and bin 1,000 20.272 s 0.0148 s 1374x
numpy.int64 rectangles, int bin 1,000 27.063 s 0.0156 s 1734x

One Windows laptop, best of interleaved rounds; details in BENCHMARKS.md. The integer path shows the same ratios as before.

How

  • The algorithm is one C++ template, instantiated for int64 and for a tagged double that records which Python type each value has. See "The mixed path" in docs/DESIGN.md.
  • The build passes -ffp-contract=off (GCC, Clang) and /fp:precise (MSVC) so float results match Python bit for bit.

The "fallback is 2x slower" known issue

It was a measurement artifact, not a defect. The fallback makes 0.007% more Python function calls than rectpack and takes the same time when the two are timed in alternation. Evidence: research/fallback_slowdown.py, results/fallback_slowdown.txt.

Checks

  • 1,038 tests pass locally on Windows: Python 3.13 with numpy 2.5, Python 3.11 with numpy 1.26 and 2.4.
  • Mutation check: eleven injected bugs, all caught.
  • ruff and clang-format pass.

What this PR needs from CI

The float path has not run on Linux or macOS yet. macOS arm64 is the one to watch, because Clang there can fuse multiply-adds. If the mixed-type tests fail on that platform only, that is the cause.

Still falling back

Decimal, Fraction, bool, numpy.float32, numpy integers other than numpy.int64, and sizes above 2^30 (2^26 for MaxRectsBaf with floats).

A substantial part of this change was written with an AI assistant.

🤖 Generated with Claude Code

p-ramcharan-dev and others added 2 commits November 10, 2026 17:48
Sizes given as Python float, numpy.float64 or numpy.int64, in any mix with
int, now stay on the native path. Results match rectpack in value and in
type. This covers the way gdsfactory calls rectpack (int rectangles in a
numpy.float64 bin), which 0.1.0 handed to the original Python code.

- _native.cpp: the algorithm is one template, instantiated for int64 and
  for a tagged double (Num) that tracks the Python type of each value.
- CMake: -ffp-contract=off / /fp:precise so float results match Python.
- tests: test_mixed_types.py; snapshots now record the type of every number.
- bench/run_mixed.py and results/mixed.*: 284x at 300 rectangles and 1193x
  at 1,000 for gdsfactory-style input on the reference laptop.
- The "fallback is 2x slower" known issue was a measurement artifact:
  research/fallback_slowdown.py, results/fallback_slowdown.txt.
- mutation_check.py: five new mutations, one stale mutation repaired.

Co-Authored-By: Claude <[email protected]>
@p-ramcharan-dev
p-ramcharan-dev merged commit 67af62b into main Nov 10, 2026
12 checks passed
@p-ramcharan-dev
p-ramcharan-dev deleted the float-numpy-0.2.0 branch November 10, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant