Swift SDK + Swift demo clients + Ninja game fixes/perf/UI polish#4509
Draft
avias8 wants to merge 28 commits intoclockworklabs:masterfrom
Draft
Swift SDK + Swift demo clients + Ninja game fixes/perf/UI polish#4509avias8 wants to merge 28 commits intoclockworklabs:masterfrom
avias8 wants to merge 28 commits intoclockworklabs:masterfrom
Conversation
Author
Author
|
Still working on this pull request. A shout out to #4471 and https://github.com/AndroidPoet, who taught me a lot as I was working on the Swift SDK. Thank you for your work! |
849bbbc to
1f31ac8
Compare
1f31ac8 to
973c6dc
Compare
- Removed @mainactor bottleneck from SpacetimeClient and TableCache - Implemented in-place dictionary mutations and optimized snapshotting (O(N)) - Added specialized BSATN primitive serialization (U32, U64, I64) - Implemented BSATN manual fast-paths for hot-path types - Improved memory efficiency with zero-copy Data slicing - Achieved ~420x speedup in cache operations
…dware decompression - Convert `BSATNReader` and `BSATNStorage` to zero-allocation `~Copyable` structs with Swift 6 Typed Throws. - Add `BSATNFastCopyable` array memcpy optimizations for primitive arrays on little-endian targets. - Replace `UnfairLock` with `Synchronization.Mutex` and eliminate `@unchecked Sendable` across caching and networking layers for strict concurrency. - Switch `ServerMessageFrameDecoder` from `zlib` to Apple's native `Compression` framework for hardware-accelerated GZIP decoding. - Update codegen to conform generated table row structs to `Identifiable` when a primary key is defined.
…ead optimizations - crates/codegen/src/swift.rs: Add SIMD Type Mapping. Rust codegen now detects mathematical types (Vector2, Vector3, Vector4, Quaternion) and generates native Apple `simd_float` and `simd_quatf` structs to utilize hardware vector registers. - TableCache/ClientCache: Implement bulk operations (handleBulkInsert, handleBulkDelete, handleBulkUpdate). This eliminates lock contention by mutating the dictionary in bulk per network frame. - SpacetimeClient: Route heavy GZIP/Brotli decompression and BSATN decoding to a dedicated `.utility` QoS DispatchQueue to maximize efficiency core usage and prevent thermal throttling while unblocking the WebSocket IO thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


What
Native Swift SDK for SpacetimeDB, plus Swift code generation, two end-to-end Swift demo clients, and CI/E2E tooling in the monorepo.
This PR adds first-class Swift support via:
sdks/swiftpure Swift package runtimecrates/codegen+ CLI integration (spacetime generate --lang swift)demo/simple-module/client-swiftdemo/ninja-game/client-swiftWhy
SpacetimeDB needed a native Swift client path for Apple platforms and realtime Swift UI/game workloads without relying on non-Swift client layers.
This PR enables:
Branch Scope
upstream/master973c6dcf9558613,96228Commit Structure
5bda437e1swift-sdk: add native runtime package and tests5b7881f5ccodegen(swift): add generator backend and CLI integration96bddcd76demo(simple-module): add module, generated bindings, and Swift app6cf0e66cademo(ninja-game): add module, generated bindings, and Swift game client973c6dcf9ci(swift): add SDK workflow, E2E checks, and drift guardArchitecture
v2.bsatn.spacetimedbclient/server message support with BSATN binary serializationURLSessionWebSocketTaskwithSec-WebSocket-Protocol: v2.bsatn.spacetimedb@MainActorlifecycle for API/cache-facing stateNone,Gzip,Brotliusing AppleCompression+zlibClientCachetable registration by canonical wire table nameTableCache<T>row-byte keyed multiset model (no blanketIdentifiablecoupling)Features
None,Gzip,Brotli)File Layout
Test Coverage
Swift SDK tests: 37 tests across 3 test files (all passing).
Validation Results (Executed 2026-03-02)
swift test --package-path sdks/swiftswift build --package-path demo/simple-module/client-swiftswift build --package-path demo/ninja-game/client-swiftcargo test -p spacetimedb-codegen --test codegencargo test -p spacetimedb-cli generate:: -- --nocapturetools/check-swift-demo-bindings.shtools/swift-procedure-e2e.shE2E OK(SleepOneSecondProcedurecallback succeeded in ~1.04s)Performance
No dedicated throughput/TPS benchmark suite is included in this PR write-up, so no benchmark claims are made here.
Observed from validation:
SleepOneSecondProcedurecallback observed at ~1.04s, consistent with procedure behavior)Bug Fixes Included
UnsubscribeAppliedSubscriptionErrorIdentityClientConnectionIdScheduleAtSpacetimeResult<Ok, Err>Result<T,E>mapped toSpacetimeResult<T,E>Identifiableemission from generated typesIdentifiablegeneric constraint in Swift cache pathJoin.swift,CombatHitCooldown.swift)Roadmap
Phase 1 — Swift SDK Core Runtime (completed baseline)
Phase 2 — Swift codegen and typed access (completed baseline)
spacetime generate --lang swiftPhase 3 — Event/procedure surface expansion (in progress)
Phase 4 — Observability (planned)
Phase 5 — Platform/productization (planned)
Feature Parity
Other SDK columns are not fully re-audited in this pass; Swift column reflects this PR scope.
Dependencies
Swift runtime/package
6.215+17+FoundationCompressionzlibTooling/CI dependencies used for validation
spacetimeCLI built from this repoBuild
Notes
tools/swift-procedure-e2e.shpublishes a local module and runs a live callback check.wasm-optis unavailable, scripts continue with unoptimized wasm and still complete successfully.changes.mdis intentionally untracked and not part of branch commits.