Skip to content

Use Java reflection fast paths in Reflector for zero-arg constructors and property getters - #117

Merged
marcprux merged 2 commits into
skiptools:mainfrom
piercifani:reflector-java-reflect-fast-paths
Sep 26, 2026
Merged

marcprux merged 2 commits into
skiptools:mainfrom
piercifani:reflector-java-reflect-fast-paths

Conversation

@piercifani

Copy link
Copy Markdown
Contributor

Follow-up to skiptools/skip-foundation#130 and skiptools/skip-android-bridge#33, chasing the same cost: the first kotlin-reflect touch in a process pays the full reflection-infrastructure init (descriptor deserialization, KClassImpl member scans), which lands on the app's main thread during launch.

With those two fixes shipped, the remaining launch-path entry into kotlin-reflect is the generated per-module Bundle_Support.swift:

try! AnyDynamicObject(className: "…._ModuleBundleAccessor_<Module>").moduleBundle!

which reaches Reflector(reflectingClassName:arguments:) (constructor signature matching over KClass.constructors) and Reflector.propertyValue (memberProperties scan) — both kotlin-reflect, for what is in practice a public zero-arg constructor and a plain getter. Any app that touches Bundle.module before first frame (e.g. reading a localized string or a bundled resource in Application.onCreate) pays this.

This PR adds two java.lang.reflect fast paths, falling back to the existing kotlin-reflect paths for everything else:

  • Reflector(reflectingClassName:arguments:) with no arguments instantiates via a public zero-arg Java constructor when one exists. Kotlin constructors with default parameters compile to synthetic overloads with a DefaultConstructorMarker parameter, so they don't match and fall through to matchConstructor as before.
  • propertyValue first resolves the property's JVM getter (getFoo() / isFoo()) via Class.getMethod and invokes it directly. Java fields (which kotlin-reflect surfaces as properties) have no getter and fall through; non-public Kotlin members are excluded in practice by their module-mangled JVM getter names (getFoo$module). One behavioral nuance: a hypothetical Kotlin member whose getter is JVM-public under a clean name but not KVisibility.PUBLIC would now resolve where the old path threw — I couldn't construct such a case with kotlinc's current name mangling, but flagging it for review.

Measured in the Polymarket app (Pixel 9 emulator, debug build, 12 cold starts, am start -W): median TotalTime 1398ms → 1318ms, and skip.bridge.Reflector frames disappear from the simpleperf startup trace entirely (kotlin-reflect residual drops ~10×, the remainder coming from other libraries).

… and property getters

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cla-bot cla-bot Bot added the cla-signed label Aug 28, 2026
@marcprux
marcprux merged commit 391df3b into skiptools:main Sep 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants