logs.gokuls.in

2 pull requests merged across 1 repo

Offline-Protocol/offline-protocol-sdk

Merge after #579. This branch does not contain it, and the mobile profile inherits its link-time optimisation from release, which only reaches the library once #579 drops the lib crate type.

What was wrong

ADR 0018, ADR 0019, threat model R11, the C guide's K4, the telemetry sink contract, CLAUDE.md and six code comments said the mobile library shipped under minisize, with panic = "abort". No build ever used minisize: every iOS, Android, desktop and wheel build ran --release. The mobile libraries shipped at opt-level 3 and unwound.

The change

  • A mobile profile: release at opt-level "s", unwinding. The release workflow's Android job, both React Native build scripts (the release's iOS job runs the iOS one) and CI's iOS simulator build use --profile mobile, and read their output from target/<triple>/mobile/.
  • The desktop libraries and the wheels stay on release. minisize is deleted.
  • Every document and comment that claimed abort now says what ships. ADR 0029 records the decision with both tables below.
  • Two guards hold the profile and the four build sites, and keep the desktop builds on --release.

Why unwinding

Every panic seam in the SDK is a catch_unwind: event handler isolation, the telemetry sink dispatch, the document import, and UniFFI's own conversion of a panic into a Swift or Kotlin exception. panic = "abort" would turn each into a SIGABRT attributed to the app, for a further 19% off the shared library.

Why "s" and not "z"

The plan proposed "z". The benches overruled it. Criterion, macOS arm64, change against opt-level 3:

Benchopt-level 3"s""z"
mls_seal_dm88.6 µs+43%+145%
mls_open_dm101.7 µs+37%+115%
send_message4.11 µs-1%+46%
process_loop1.43 µs+8%+58%
message_serialization/10000.70 µs+30%+96%
ble_fragmentation/10000.99 µs+33%+115%
ble_reassembly/10003.26 µs+1%+83%

Keeping 30 crypto and codec crates at opt-level 3 under "z" recovered only a third of the MLS cost, and the engine and serialization paths not at all. The data layer has no bench, so its cost at "z" would ship unmeasured. The static library that iOS and npm carry is no larger at "s" than at "z". Moving to "z" later is one word in Cargo.toml and one in the guard.

Size

Every row with #579's link-time optimisation except the first.

BuildAndroid arm64 .somacOS arm64 .dylib
0.29.0 shape (--release, no LTO)11,843,34411,534,416
#579 alone11,032,35210,932,208
#579 + this (mobile, "s")8,540,4408,538,400
"z", for comparison6,347,712

Verified locally

  • fmt, cargo clippy --workspace -- -D warnings, cargo test --workspace --lib, rustdoc under -D warnings.
  • Both guards, negative-controlled eight ways: panic = "abort" added, minisize back, each of the four mobile sites back on --release, a desktop job on mobile, the profile at "z".
  • The iOS simulator archive built as CI builds it, at the new path, passes the minimum-OS gate.
  • Android arm64 built as the script builds it (NDK 27.1, no C binding) passes the 16 KB alignment gate.

Not verified

  • The full release workflow at the new paths: worth a workflow_dispatch dry run with an -rc.N version after both PRs merge. This is the first release run through the Android and iOS collection steps since they moved.
  • Speed on a phone. The benches ran on an M-series Mac.

What was wrong

The release profile sets lto = true, but cargo runs link-time optimisation on a library only when every crate type in its list can take it. The FFI crate listed ["cdylib", "staticlib", "lib"], and an rlib cannot, so cargo passed no LTO flag at all. Every native library from the UniFFI migration (2025-11-07) to 0.29.0 was built without it, on every platform, and no build said so.

The rule is in cargo's source (CrateType::can_lto and the comment in compiler/lto.rs: "a cdylib/rlib combination won't allow LTO") and is cargo issue rust-lang/cargo#2301, closed in 2017 with a workaround and no warning.

The change

  • The crate's types are now cdylib and staticlib. The release build now passes -C lto.
  • tools/capi-fuzz was the only thing linking the crate as Rust. It now compiles the crate's own lib.rs under its own package, with its own build script generating the scaffolding from the same interface definition. The fuzz target is unchanged.
  • Two guards: the_ffi_crate_builds_no_rust_library refuses a crate type that turns LTO off; the_fuzzer_compiles_the_ffi_crate_with_its_dependencies holds the fuzzer's dependencies and features to the crate's, since the fuzz build only runs nightly.

Measured

macOS arm64, same commit, fresh target directory each, capi on:

beforeafter
liboffline_protocol_uniffi.a137,949,53623,393,288
liboffline_protocol_uniffi.dylib11,534,41610,932,208
exported C and UniFFI functions482482
build time / peak memory1m46 / 1.3 GB2m45 / 2.1 GB

The static library is what the iOS framework and the npm package carry, so that is where nearly all of the win lands. The iOS and Android numbers will be read off the first release artifacts built this way.

Verified locally

  • cargo fmt, cargo clippy --workspace -- -D warnings, clippy without the C binding, cargo test --workspace --lib, rustdoc under -D warnings.
  • The uniffi crate's 263 unit tests all still run: it never had a doctest, so dropping the rlib loses none.
  • The C consumer check (shared, static, exchange, quick start) and scripts/check-c-library.sh macos-arm64 against the LTO release dylib.
  • The Python suite against the LTO release dylib: 1124 passed, 4 skipped.
  • Both guards negative-controlled six ways.
  • The fuzzer: every seed runs, and cargo +nightly fuzz run --sanitizer none for 60 s on macOS ran 1.4 million inputs with coverage rising to 2015 edges. Fuzzing on macOS did not link before this change; fuzz.yml and docs/bridges/c.md now say so.

Not covered here

  • fuzz.yml has never run. I will dispatch it on this branch.
  • The mobile profile (opt-level and panic strategy) is a separate PR: no build has ever used minisize, while six documents say the mobile artifact ships panic = "abort".
  • A Rust consumer of the crate from crates.io (0.29.0 has none) can no longer depend on it.