Skip to content

Make normal signing ~30× faster; add opt-in batching - #165

Draft
josusanmartin wants to merge 3 commits into
elliottech:mainfrom
josusanmartin:agent/signing-speedup
Draft

Make normal signing ~30× faster; add opt-in batching#165
josusanmartin wants to merge 3 commits into
elliottech:mainfrom
josusanmartin:agent/signing-speedup

Conversation

@josusanmartin

@josusanmartin josusanmartin commented Aug 6, 2026

Copy link
Copy Markdown

Result: ~30× faster normal sign_create_order

Normal single-order latency: 1.95–2.02 ms → 63.9 µs

Sequential signing throughput: roughly 500 → 15,700 orders/sec

That is approximately 96.7% less latency without changing the public API.

This is the ordinary one-order-at-a-time public API. It requires no batching and no prepared nonces. The normal-path speedup is the purpose of this PR; the explicit batch API is a secondary, opt-in improvement.

The 1.95–2.02 ms baseline is the original warm end-to-end caller measurement on this host. The 63.9 µs result is the public Python signing call with the linked Go and Poseidon changes over 7 × 1,000 sequential orders. Against the currently bundled signer's directly measured 1,644 µs public Python path, it is still 25.7× faster.

Summary

Speed up the ordinary single-order Python signing path, while remaining compatible with older packaged signer binaries.

  • free Go C.CString results directly through the process allocator on POSIX, avoiding extra Python-to-Go callbacks
  • keep exported-Go Free on Windows, where allocation can cross CRT heaps
  • enable the linked Go signer's optional stack-bound callback cache when present
  • publish the signer singleton only after all ctypes signatures and runtime hooks are configured
  • preserve graceful fallback when an older binary lacks the new exports

With the currently bundled Linux signer, changing only response cleanup reduced the public single-order path from 1,644 µs/order to 816.5 µs/order (median, 5 × 500 orders). With the linked Go and Poseidon PRs, the normal public call measured 63.9 µs/order (median, 7 × 1,000 orders), or about 15.7k orders/sec on the same Intel Xeon E-2286G host.

Important

The largest C-to-Go boundary reduction in the linked Go PR requires the private Go _cgo_getstackbound hook. It is unsupported runtime internals, version-gated, and must be revalidated before upgrading the Go toolchain. This Python wrapper treats it as optional and continues to work when the export is absent or disabled.

Secondary: explicit batching and prepared nonces

The new sign_create_orders_batch method is opt-in. A normal sign_create_order call does not batch. The method uses one packed native response allocation, validates the explicit nonce range before either the native or legacy fallback path, and checks that the native result count matches the request count.

On batches of 1,000 orders:

Mode Median Throughput
normal single-order calls 63.9 µs/order 15.7k/sec
prepared single-order calls 14.8 µs/order 67.7k/sec
normal explicit batch 10.5 µs/order 94.9k/sec
prepared explicit batch 3.24 µs/order 308k/sec

Prepared nonces are held in a process-global, in-memory pool by the linked Go signer. They are single-use and must not be persisted, serialized, or shared across a fork.

This PR also fixes the existing CreateOrderTxReq.MarketIndex ctypes field from 32-bit to the native ABI's 16-bit width in a separate commit.

Packaging / merge order

No generated signer binaries are changed in this draft. Please merge/tag the Poseidon PR, update/merge the Go signer PR, regenerate all platform binaries, and then merge this wrapper PR.

Validation

  • pytest -q (80 passed)
  • strict project flake8 error checks
  • concurrent singleton initialization test
  • POSIX/Windows allocator selection tests
  • packed-response truncation, trailing-data, count, and native-result-count tests
  • legacy binary fallback and negative/overflow nonce tests

@josusanmartin josusanmartin changed the title Speed up native signing and add explicit batch signing Make normal signing ~30x faster; add opt-in batching Aug 6, 2026
@josusanmartin josusanmartin changed the title Make normal signing ~30x faster; add opt-in batching Make normal signing ~30× faster; add opt-in batching Aug 6, 2026
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