Skip to content

Profile-guided optimization (PGO) for the runtime's native code #2138

Open

Description

@edusperoni

Summary

We ran a spike to see whether profile-guided optimization (PGO) is worth doing for libNativeScript.so.

PGO compiles the library twice:

  1. Build an instrumented library (-fprofile-generate) and run a training workload on a device. The workload is the test suite plus, optionally, the JNI micro-benchmarks.
  2. Recompile with the merged counts (-fprofile-use). The compiler uses them for code layout, inlining and indirect-call promotion.

Only the runtime's own C++ is affected. V8 is linked from a prebuilt archive and does not change.

The result is a roughly 3–9% gain on JNI-bridge calls and field accesses (mostly 5–9%), while cold start is unchanged. Before anything ships, the open question is whether that gain is worth the CI and maintenance cost.

The tooling and a full write-up are on perf/pgo-spike:

  • docs/pgo.md: how to build, train, merge and measure; the results; and a proposed CI design.
  • tools/pgo/: build, train, merge and measurement scripts.
  • Build changes: a gradle pass-through for extra compiler and linker flags (-PnsExtraCxxFlags= and -PnsExtraLinkFlags=), and a __pgoDump(path) hook that only exists in instrumented builds. The test suite and the JNI benchmarks call that hook when they finish.

This issue is for discussion and tracking. It is not a commitment to ship PGO.

Results

Setup:

  • Device: an arm64 phone (SM-G780G, Android 13).
  • Builds: optimized release, AOT-compiled.
  • Method: 6 interleaved rounds. Only relaunches were measured, because the first launch after an install ran about 1.6× slower on this phone.
  • Numbers: the median across launches of each launch's median, in ns per call.
  • Noise floor: an A/A run moved single-call rows by about ±1% (1.6% at most).

Variants compared:

  • none: built without a profile.
  • bench+suite: profile trained on the JNI benchmarks and the full suite (1773 specs).
  • suite: profile trained on the suite only.
Row none bench+suite suite
static.noop() 104.2 95.5 (−8.3%) 94.6 (−9.2%)
static.add(int,int) 186.4 172.0 (−7.7%) 172.1 (−7.7%)
instance.noop() 238.2 224.2 (−5.8%) 226.6 (−4.9%)
instance.getInt() 237.8 223.9 (−5.9%) 225.8 (−5.1%)
instance.echo(String) 678.9 640.0 (−5.7%) 644.8 (−5.0%)
instance.takeObject(Object) 512.8 482.9 (−5.8%) 479.4 (−6.5%)
instance.takeObjects(Object x3) 1070.5 1010.2 (−5.6%) 992.5 (−7.3%)
static.bufferCapacity(ArrayBuffer) 444.1 409.9 (−7.7%) 404.8 (−8.9%)
field.get int 162.6 151.2 (−7.0%) 150.6 (−7.3%)
field.get Object 331.2 319.6 (−3.5%) 322.4 (−2.7%)
static field get int 71.0 65.5 (−7.7%) 67.8 (−4.5%)
  • Discarded rows. Construction, callback and contention rows also improved, some by 9–60%. These rows are bimodal, though, and their A/A gaps reach ±9%, so we do not count them.
  • Cold start. Median of 15 launches: 182 ms (none), 178 ms (bench+suite), 181 ms (suite). The difference is within noise.
  • Overfitting. A profile trained on the suite alone performs about as well as bench+suite. It is not just tuned to the benchmark.
  • Size. The effect on .so size is small in both directions: the spike measured +30–85 KB, and a rebuild of the branch came out 32 KB smaller.
  • Training time. The suite took about 136 s on the phone. 29 specs fail in a release-mode training run, as expected: release builds block remote/HTTP module loading. Those failures do not affect what the profile covers.

Cost and maintenance

  • One profile per ABI. Each needs a training run on hardware or an emulator that executes that ABI. x86 and x86_64 emulators are cheap on CI. arm64-v8a and armeabi-v7a need ARM runners or a device farm.
  • Profiles go stale. Every runtime C++ change does this. Clang ignores profile data for functions whose control flow changed, so the gain decays quietly instead of failing. The -Wno-profile-instr-* flags that keep builds readable also hide that decay, so it has to be surfaced some other way.
  • Release builds would depend on a binary artifact. It has to be kept to reproduce them.
  • CI time per ABI: an instrumented native build, an emulator or device boot, and about 2–3 minutes of suite.

Proposed direction

Not against it, but it should only happen if it is automated in CI:

  • Release (non-next) builds: PGO is mandatory. They train and use a fresh profile per ABI, then attach the profiles to the release so the build can be reproduced.
  • Normal and next builds: these never train. They use the latest release's profiles and fall back to no PGO when none exist.

A few things are still missing before that can work:

  • per-ABI profile selection in CMake (for example <dir>/${ANDROID_ABI}.profdata), since one gradle invocation builds all ABIs;
  • forwarding that property through the root build.gradle;
  • a CI job per ABI.

Caveat: a ~5–10% gain on JNI-call micro-benchmarks may not justify the cost. This is especially true if it relies on emulator runs and profile artifacts that will inevitably go stale. That trade-off is the main thing to settle here.

Open questions

  • Should training run on emulators or real devices for each ABI? Where would arm64-v8a and armeabi-v7a train? Could an x86_64 profile be reused for arm64-v8a? That is untested.
  • Is CI time per ABI acceptable on every release? Estimate: about 2–3 minutes of suite, plus the instrumented build and an emulator boot.
  • Where are profiles stored: as release assets (proposed) or in the repo?
  • How is staleness surfaced? One option is the share of functions with mismatched profile data, reported in the job summary. Should staleness ever block a release?
  • Could V8 ever be included? That needs instrumented V8 builds and a profile tied to the exact V8 revision.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions