Prototype First, Argue Later
How we chose Rust for our SDK core.
Events in · Commands out
Our Android and iOS SDKs were built from the same spec by people who talk to each other every day. When we sat down and compared them closely, we still found somewhere between 80 and 90 places where a publisher could see them behave differently. For example: different error codes for the same failure, a banner resuming refresh one interval later on one platform, and a retry on Android but not on iOS.
None of these was deliberate. Each came from a reasonable local choice, in two codebases that stayed in sync only when someone noticed a difference.
Today, the CloudX SDK beta on both platforms runs its ad logic on one shared core, written in Rust.
Where Steffan left off
In July, my colleague Steffan Chartrand wrote about how cross-platform mobile engineering works at CloudX. Our engineers had stopped staying in their platform lanes. An Android engineer could make a well-scoped change on iOS, a specialist would review it, and a fix on one SDK became a pointer to what needed checking on the other.
That way of working is what made this project possible. You can’t merge two implementations into one unless the team knows both well enough to say what “the same behavior” means, case by case.
Steffan’s post also contains the sentence that stuck with me: many components were “implemented nearly identically” on both platforms, and bugs were often mirrored. The team worked across platforms, but the code was still written twice.
My background
I’ve spent the past decade in ad tech, working on monetization and user acquisition from both the product and the engineering side. I started as an engineer writing SDK code, went on to manage engineering teams at Unity, and then led product and engineering at Playgap before joining CloudX. The thread through all of it has been SDKs: how fast they start, how stable they are, and what that does to revenue.
Some of those SDKs ran on a shared core written in TypeScript: one body of logic for every platform, which could be updated without shipping a new SDK. That approach has real benefits.
It also has costs. A core in a scripting language brings a runtime with it, and you pay for that at startup, in memory, and every time a call crosses into native code. A core that updates itself has to be fetched over the network when it changes, and that request sits in front of the first ad. We’re a guest inside someone else’s app, so those costs matter.
Why Rust
We wanted the core as close to the hardware as we could get it without giving up memory safety. We looked at Kotlin Multiplatform and other cross-platform options, which would have kept us in languages the team already knew. We decided against them in favor of the option with the highest performance ceiling, even though it was the less familiar one.
The core compiles to machine code on both platforms, with no virtual machine and no garbage collector, and we choose what is allocated and when. The compiler also rejects the mistakes that usually come with that control, such as using memory after it has been freed or sharing it unsafely between threads. Our code runs inside a publisher’s app, so a crash in our code crashes their app. We wanted speed and safety.
Instead of weeks of debate about what each option would probably do, we started with a one-day spike: one self-contained piece of the SDK, rewritten in Rust on the side, to check that it would work for us. It did, so we kept going.
The spike also settled a question we would otherwise have argued about. The cautious plan was a dual mode, with the native implementation and the Rust one living side by side behind a remote switch so we could A/B test them. We realized quickly that we didn’t want it. Carrying both implementations made the numbers worse for each, and a remotely configured split was hard to measure cleanly. We chose a straight before-and-after comparison instead.
Over the next week we built a first version that covered effectively the whole core, benchmarked it on real devices, and shipped it in a non-public release to a single publisher’s app. The full build, at parity with the previous SDK, took about one more week.
Most of the second week went into checking the result. The native SDKs were our oracle: we recorded their sessions as golden files, and the core had to replay every one and make the same decisions. An AI agent coordinated subagents to build and run a real-device harness that checked the two SDKs behaved the same on actual phones. That build is the beta now running in the same app. Anthropic, writing about moving its own API to Rust, put it well: “Verification, not generation, had become the bottleneck.”
Having numbers from real devices made the discussions shorter and more concrete.
Rewriting things in Rust is very much the Zeitgeist. Anthropic’s move landed the same season as ours, and like us with dual mode, they found a partial port cost about as much as it gained. Still, I’m wary of doing anything because it’s in the air. What changed is cost. A couple of years ago, a team our size rewriting the core of two production SDKs in a third language, while still shipping both, would have been hard to defend. The expensive parts are now the parts AI tooling is good at: porting well-specified behavior, generating the glue between languages, and checking the result against the old system. It is also why a prototype could come before the debate.
How it works
The core makes the decisions; the platform carries them out.
Shell and core
Events go in, commands come out.
Platform shell · Kotlin / Objective-C
- Network calls
- Partner SDKs
- Views
- Timers
- Storage
- Publisher callbacks
Events
- Bid response arrived
- Ad closed
- Timer fired
Commands
- Load this adapter
- Start this timer
- Tell the publisher the ad is ready
Shared core · Rust
- No network calls
- No threads
- Never reads the clock
Conceptual flow. One schema defines every message, and the Rust, Kotlin, and Objective-C code that reads and writes them is generated from it.
The Kotlin and Objective-C layers still own everything that touches the device. Network calls, partner SDKs, views, timers, storage, and every callback a publisher sees are native code, and the public API hasn’t changed. When something happens, such as a bid response arriving, an ad closing, or a timer firing, the native side reports it to the core as an event. The core updates its state and hands back commands: load this adapter, start this timer, tell the publisher the ad is ready.
Three rules for the core:
- One schema. The messages that cross the boundary are defined once, and the Rust, Kotlin, and Objective-C code that reads and writes them is generated from that definition.
- No side effects in the core. It makes no network calls, starts no threads, and never reads the clock. Time is something the platform tells it.
- Same input, same output. A recorded session can be replayed through the core and must produce exactly the same decisions, on a laptop, on Android, or on iOS.
Laws instead of more tests
Because the core is deterministic and does nothing on its own, we can drive it through every order of events a platform could send and check what comes out.
We write the publisher contract down as laws: properties that must hold for every order of events, not only the orders an example test happens to try. Each load() gets at most one load callback. Each show ends with exactly one callback. Nothing reaches the publisher after destroy(). Revenue is paid at most once per show.
Law explorer
Laws checked against every legal order of events.
1 · Laws
- Each load() gets at most one load callback
- Each show ends with exactly one callback
- Nothing reaches the publisher after destroy()
- Revenue is paid at most once per show
2 · Explorer
- load · filled · show
- load · app stop · expired
- show · displayed · closed
- show · pulled by quality check · network error
181,730sequences from 44 starting states, in about 6 s
3 · Shortest repro
LAW BROKEN
one terminal callback per show
shortest sequence, 3 events:
ad shown
pulled by quality check
network error
got: hidden + display failed for show 1Simplified output. Fullscreen ads, at the depth that runs on every pull request. The explorer walks only the events a correct Android or iOS shell could send next.
An explorer then walks every legal sequence of events, up to a fixed depth, from 44 starting states: loaded, showing, expired and reloading, app in the background, and so on. For fullscreen ads that is about 180,000 sequences, and it finishes in about six seconds on a laptop, so it runs on every pull request. When a law breaks, it prints the shortest sequence that breaks it.
On its own, it rediscovered an Android case we had introduced in the rewrite: if our ad-quality check pulled an ad while it was on screen and the ad network then reported an error, the publisher got two endings for one show, a hidden callback and a failed-to-display callback. Audits of the rest of the core with the same approach surfaced more edge cases, several of them inherited from the native SDKs we replaced. Their fixes ship in follow-up builds.
We expect laws to replace many example-based unit tests. A law states the contract once and checks it against every order of events, where a unit test tries one order. And because the core is shared, each law protects Android and iOS at once.
What it gives us
Two implementations, then one core
Where the ad logic lives, before and after.
Before · The logic written twice
After · The logic written once
Rendering, partner adapters, and how each OS reports what is on screen stay native and still differ by design.
The two SDKs now behave the same, because auctions, caching, refresh, expiry, retries, consent decoding, and telemetry are the same code. A class of fringe issues that only ever showed up on one platform can no longer happen. What still needs checking per platform is the part that should differ: rendering, partner adapters, and how each OS reports what’s on screen.
Changes are also made once. A fix, a feature, or an optimization in the core ships to both platforms together, with no second implementation to update.
What we’ve measured
The goal for the first round was to match the native SDKs or beat them.
Startup and stability
On Android, in one game running the beta in production, median SDK startup went from 1.82 s to 1.56 s compared with the last native-only build that game ran. The share of startups where a partner SDK timed out during initialization fell from 9.4% to 6.8%. Fill, show rate, and show failures stayed level.
First production read
One Android game · Rust core beta vs. last native-only build
Median SDK startup
Time for the SDK to finish initializing
- Native only
- 1.82 s
- Rust core
- 1.56 s
Partner timeouts at startup
Share of startups where a partner SDK timed out during initialization
- Native only
- 9.4%
- Rust core
- 6.8%
On iOS we expected less headroom. Objective-C is fast, and that SDK was already tight. Even with the core’s own startup added, initialization and first load on our test devices matched native.
We saw no crashes in our own code on either platform. This is one app, and results elsewhere will vary, but the direction matches what we benchmarked on real devices before release. An SDK that is ready sooner, with more of its demand partners ready, can fill more of the requests made early in a session.
Memory, measured locally
Tuning used to mean making the same careful change twice, in two languages, and proving both still matched. That cost was high enough that some inefficiencies were simply left alone. With one core, they became worth going after, and the first ones landed within days of the beta. We measured each on our test devices and on a desktop build of the same core.
A new core instance now holds 12 KB before any ad loads, down from 67 KB. Most of the old figure was space reserved for commands that rarely needed it.
A second pass went after layout rather than data. A heap profile showed that much of what the core kept alive was empty space and copies. Large per-ad state sat inline in hash tables, so an empty slot cost as much as a live ad. The buffer that hands commands to the platform kept a copy of every batch and never shrank, so one large batch set the memory floor for the whole session. Our recordings suggested batches of about 2.5 KB; captures from real devices showed 27 to 176 KB. Fixing the layout cut what the core holds in a typical session from 83 KB to 53 KB, and by 22 to 36% across the states we measured. Behavior did not change: every golden recording still replays the same.
The largest saving was on iOS, in code older than the rewrite. Each auction left a 26 KB record behind that was never freed; it now leaves 4 KB. Publishers rarely hold one ad: they keep several formats and placements loaded, often at once, and a long session runs auction after auction. For a single banner refreshing every 30 seconds, memory growth dropped from about 3 MB an hour to about 0.5 MB.
Core and SDK memory
Before and after the first memory work, same tests
Core memory at start
What a new core instance holds before any ad loads
- Before
- 67 KB
- After
- 12 KB
Core memory in a typical session
What the core keeps alive while ads load and show
- Before
- 83 KB
- After
- 53 KB
Memory kept after 20 banner refreshes
Auction records the iOS SDK keeps for one refreshing banner
- Before
- 527 KB
- After
- 82 KB
What comes next
The memory work above ships in follow-up builds. We expect larger gains from the next step: the data itself, meaning how the core holds what it needs to deliver an ad and how that data moves between the core and the platform. Every gain there reaches Android and iOS in the same release.
Try it
The Rust core is available now in the beta releases of the CloudX SDK for Android and iOS. The beta label is a precaution ahead of the holiday season; it becomes the regular release for all publishers after that. If you’d like to integrate it before then, talk to your CloudX account manager first, or reach us at hello@cloudx.io.