Skip to content

Reduce cnativeapi shared library footprint via hidden visibility, section GC, and -Os - #44

Merged
lijy91 merged 5 commits into
mainfrom
fix/issue-25-cnativeapi-footprint
Nov 22, 2026
Merged

lijy91 merged 5 commits into
mainfrom
fix/issue-25-cnativeapi-footprint

Conversation

@lijy91

@lijy91 lijy91 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #25
Depends on libnativeapi/nativeapi-core#98

Reduce the native-asset shared library's general overhead while retaining the complete C API. On the same core sources, Linux x64 library size falls by 57.6% and macOS arm64 by 50.6%.

  • Optimize GCC/Clang builds for size, hide internal C++ symbols, discard unused sections and strip local/debug symbols.
  • Restrict ELF exports with a version script, retaining the C ABI (including legacy free_c_str), Dart event-loop shim, protocol host bridge and Android JNI bridge. Track the map as a build-hook dependency.
  • Keep Windows' existing MSVC optimization settings.
  • Update the C ABI generator's visibility annotations and core pointer to fdbe282, which includes current core main and the matching export annotations.
  • Add tools/verify_native_exports.py to resolve every exported C declaration and check for leaked C++ symbols.

Measured with identical core fdbe282 and Dart 3.14.0; Linux uses Ubuntu Clang 18.1.3. The baseline uses workspace main's build hook; the optimized build uses this PR.

Platform Build Library bytes gzip bytes Dynamic symbols Internal C++ symbols
Linux x64 Baseline 2,883,896 777,842 4,387 3,690
Linux x64 Optimized 1,222,128 427,997 685 0
macOS arm64 Baseline 2,145,984 534,579 1,916 1,154
macOS arm64 Optimized 1,060,896 350,663 694 0

gzip measures this library alone, rather than the complete application archive. Builds retain all modules; per-feature builds are not introduced.

Validation:

  • ./codegen check: 77 C ABI and 386 binding files up to date.
  • Hook formatting and dart analyze bindings/dart/cnativeapi: passed.
  • macOS nativeapi_flutter tests: 11 passed.
  • Actual native-asset builds and display enumeration passed on macOS and ubuntu-nuc (Linux x64).
  • python3 tools/verify_native_exports.py: all 685 required entries resolve, including the string deallocator; zero internal C++ exports on both platforms.
  • Android/iOS runtime validation was not performed; the CI build matrix covers their build compatibility.

Reproduce from the workspace root:

dart run bindings/dart/nativeapi/example/nativeapi_example.dart
python3 tools/verify_native_exports.py

Use --library <path> --allow-internal with the verifier to measure an unhidden baseline.

@lijy91
lijy91 merged commit f41a82d into main Nov 22, 2026
26 checks passed
@lijy91
lijy91 deleted the fix/issue-25-cnativeapi-footprint branch November 22, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux shared library footprint: selective linking or stripping unused native features?

1 participant