dimitriremoiville/cc-mobile/kmm-performance
Claude Code and agent skill by dimitriremoiville/cc-mobile
- community
Checking that the SKILL.md file is still in the repository.
Performance patterns for the shared Kotlin Multiplatform module — Darwin dispatcher choice, ObjC interop allocation costs, Flow/StateFlow bridging into SwiftUI, framework-link size, kotlinx.serialization cost, Ktor engine overhead. Load when investigating shared-module latency, iOS bridging regressions, or shrinking the iOS framework.
Install
npx vskill@latest install dimitriremoiville/cc-mobile/kmm-performanceSKILL.md
The same SKILL.md was published earlier as dimitriremoiville/cc-mobile/kmm-performance.
KMM performance
The shared module's performance story is not the same on the two platforms. JVM/Android perf advice is in android-performance; this skill covers what is specifically a KMP / Darwin concern.
Measure first
Every optimization here needs a number from the consuming side:
- Android: Macrobenchmark + Perfetto from the app module — same as
android-performance. The shared module's cost shows up as time inside repository/use-case frames. - iOS: Instruments (Time Profiler + Allocations). The shared module appears as
libshared.dylibframes; Kotlin function names are demangled when the framework ships debug symbols.
Numbers from a JVM benchmark in commonTest do not predict iOS performance — the Darwin runtime, GC model, and bridging cost are all different. Run on a device, not a simulator.
Dispatcher choice on Darwin
Dispatchers.IO on Darwin is backed by a kotlinx.coroutines worker pool, not the platform GCD. For most repository work this is fine, but:
- UI work must hop to
Dispatchers.Main, which on Darwin is the mainNSRunLoop. Failing to do so will silently break SwiftUI updates. - Heavy CPU work (parsing, crypto) on
Dispatchers.Defaultis fine; don't reach for GCD via cinterop just to "be native." - Avoid `runBlocking` in iOS-facing APIs. It blocks the main thread when called from Swift unless you've explicitly hopped off it. Use suspend functions and let Swift
awaitthem.
ObjC interop allocation costs
Every Kotlin object crossing the boundary into Swift incurs a wrapper allocation. Hot paths to watch:
- Per-frame conversions. A
StateFlow<List<Item>>emitting 60 times/sec creates 60 list wrappers + N item wrappers each. Either throttle on the Kotlin side (sample(16.milliseconds)) or expose a coarser API. - Sealed-class hierarchies as enums. Each Kotlin sealed subclass becomes a separate ObjC class — pattern-matching in Swift requires
is/as?casts that allocate. For tight loops, prefer a plainenum classwith a single payload field. - `Long` and `Int` boxing.
Int(32-bit) on iOS maps toKotlinInt(boxed) when crossing the boundary, notInt32. For numeric-heavy APIs, exposeIntArray/LongArrayor use cinterop, notList<Int>.
Measure with Instruments → Allocations, filter by Kotlin* class names.
Flow / StateFlow bridging into SwiftUI
The shared ViewModel exposes StateFlow<UiState>; SwiftUI doesn't speak Flow natively. Two patterns:
- `SKIE` / Touchlab `KMP-NativeCoroutines` — generates
AsyncSequencewrappers. Pay the codegen cost once, get idiomatic Swift. - Hand-rolled `@Published` bridge — a small Swift
ObservableObjectthat subscribes to theStateFlowvia aCancellable. Cheaper, no plugin, but you write the wrapper per ViewModel.
Whichever you pick, do not call collect { } from Swift in a Task { } without storing the Task — leaked tasks compound and the main thread will eventually stutter.
For one-shot events (Channel<UiEvent> exposed as receiveAsFlow), prefer a single subscribe(onEach:) Swift extension that takes ownership of the cancellation; don't bridge through @Published (events get coalesced).
Framework link size
Every Kotlin dependency you add ships in the iOS framework binary. Three levers:
- Audit transitively.
./gradlew :shared:linkReleaseFrameworkIosArm64thendu -sh shared/build/bin/iosArm64/releaseFramework/shared.framework/shared. Compare before/after when adding a dep. - `isStatic = false` when the framework is consumed via SPM/CocoaPods and the app embeds a Swift wrapper that itself links transitive deps — saves duplicated symbols.
- Strip unused targets. If you don't ship
iosX64oriosSimulatorArm64, don't build them on release CI.
Targets to watch: anything pulling JVM reflection (kotlin-reflect), JSON libraries other than kotlinx.serialization, logging frameworks that include their own coroutine support.
kotlinx.serialization cost
JSON parsing on Darwin is consistently 2-4× slower than on a comparable Android device for the same payload, because the Darwin Kotlin runtime has no equivalent of ART's JIT. Mitigations:
- Lazy fields.
@Transient+ manual decode for fields you rarely read. - Streaming for large payloads.
Json.decodeToSequenceoverdecodeFromStringfor arrays > 1 MB. - Cache the `Json { }` instance. Constructing it allocates and configures a non-trivial graph.
Ktor engine overhead
- Android engine:
OkHttp. Same characteristics asretrofit-networking— connection pooling, HTTP/2 multiplexing for free. - Darwin engine:
NSURLSession. Defaults are fine, but the per-requestURLSessionConfigurationallocation isn't free. Reuse a singleHttpClientinstance for the app lifetime; don't create one per repository.
HttpClient is thread-safe by design; inject the same instance via Koin everywhere.
Cold-start cost on iOS
The shared framework's first invocation pays for:
- Kotlin runtime initialization (
kotlin.native.runtime.GCwarmup, class table setup). - Koin module graph construction (if your
startKoin { }is on the main thread ofapplication(_:didFinishLaunchingWithOptions:)). - First
HttpClientinstantiation (TLS setup, NSURLSession config).
If you see >100 ms in application:didFinishLaunching: traceable to Kotlin frames, the fix is almost always lazy DI — wrap heavy singletons in factory or single { ... } with createdAtStart = false, and let them resolve on first use.
What's not in this skill
- Per-platform UI perf — Compose recomposition, SwiftUI view tree thrash. Those live in
compose-ui/swiftui-viewsand the native*-performanceskills. - Benchmark infrastructure — Macrobenchmark setup is in
android-performance. There is no equivalent benchmark harness for the shared module itself; if you need one, write a JVMkotlinx.benchmarksuite incommonTest-adjacent source — and don't trust the numbers for iOS.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingkmm/.claude/skills/kmm-performance/SKILL.md
Popularity
- Repository stars
- 1
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/dimitriremoiville/cc-mobile/kmm-performance)