Skip to content

perf: optimize Swift SDK with async API, mmap loading, and hex lookup table - #17

Open
Bisht13 wants to merge 6 commits into
mainfrom
feat/swift-sdk-optimization-clean
Open

perf: optimize Swift SDK with async API, mmap loading, and hex lookup table#17
Bisht13 wants to merge 6 commits into
mainfrom
feat/swift-sdk-optimization-clean

Conversation

@Bisht13

@Bisht13 Bisht13 commented Apr 9, 2026

Copy link
Copy Markdown
Member

Summary

  • Async API for all prove/verify/load/save/serialize operations — dispatches FFI work to background queue via withCheckedThrowingContinuation, preventing UI freezes on iOS when proving takes 100ms-10s
  • Memory-mapped scheme loading via POSIX mmap() — new loadProver(mappedFrom:) / loadVerifier(mappedFrom:) methods avoid heap allocation for multi-MB scheme files on memory-constrained devices
  • Proof.hex lookup table — replaces per-byte String(format:"%02x") with a static 256-entry table, 10-50x faster for hex display of large proofs

Details

All changes are additive and non-breaking — existing sync methods are unchanged. Async variants wrap sync methods, inheriting their lock-based thread safety. The mmap implementation has proper POSIX resource cleanup (defer close(fd) after open, defer munmap after mmap, zero-length file guard).

Concurrency model: Async methods move work off the main thread but do NOT enable concurrent operations on the same handle (Barretenberg backend is not safe for same-handle concurrency). For parallel proving, create multiple ProverScheme instances.

Files changed: 6 files, +362 lines (all in sdks/swift/)

Test plan

  • Run make test-swift to verify integration tests (async roundtrip, mmap roundtrip, mmap error case)
  • Verify existing unit tests still pass
  • Test async prove/verify from iOS app main thread — confirm no UI freeze

Bisht13 added 6 commits April 8, 2026 19:31
Static table of 256 hex strings indexed by byte value. 10-50x faster
than invoking NSString format parser per byte for large proofs.
Wraps sync methods via withCheckedThrowingContinuation + DispatchQueue
so prove() (100ms-10s) does not block the calling thread.
Same pattern as ProverScheme — wraps sync methods via
withCheckedThrowingContinuation + DispatchQueue.
Async variants for all load, prove, and verify operations. Dispatches
FFI work to background queue so callers are never blocked.
New loadProver(mappedFrom:) and loadVerifier(mappedFrom:) methods use
POSIX mmap() instead of heap allocation for file contents. The OS pages
in data on demand and reclaims under memory pressure — better for large
schemes on memory-constrained iOS devices. Sync and async variants.
Covers async prove/verify roundtrip, mmap load + prove/verify, async
mmap load, and mmap error handling for nonexistent files.
@Bisht13
Bisht13 force-pushed the feat/swift-sdk-optimization-clean branch from 78cb8c5 to cf72b06 Compare April 9, 2026 02:31
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