sentry-unity icon indicating copy to clipboard operation
sentry-unity copied to clipboard

SDK impact on app size

Open bitsandfoxes opened this issue 1 month ago • 1 comments

The SDK will always have an impact on the game's size, you're adding something after all. The question is how much.

The Problem

The SDK has a noticeable impact on the final binary and we need to figure out where that size increase is coming from. I'm using an .apk here for demonstration purposes. The shipped package adds about 4.4MB to the final size (see comment).

There also is a difference between a local (un-aliased) build and the final package where the SDK's dependencies got hit with the deep renaming via https://github.com/getsentry/dotnet-assembly-alias. I am focusing here on the difference between un-aliased and aliased builds. This is the part we might be able do something about it.

Assembly aliasing adds approximately 4MB to the APK and 13MB to the extracted libil2cpp.so when comparing to an un-aliased build.

Findings

Size impact on the APK

Non-aliased (package-dev): 22MB Aliased (package-release): 26MB Difference: +4MB (18% increase)

Size impact on the Native Library

libil2cpp.so (extracted): Non-aliased: 34MB Aliased: 47MB Difference: +13MB (38% increase)

All other native libraries are identical in size.

Size impact on the IL2CPP Metadata

global-metadata.dat: Non-aliased: 6.3MB Aliased: 7.0MB Difference: +0.7MB

Root Cause: Code Duplication

The assembly aliasing causes IL2CPP to generate duplicate code for all renamed System.* types:

IL2CPP Generated C++ Source

Non-aliased: 377MB Aliased: 504MB Difference: +127MB source code

System-related generated files

Non-aliased: 24 System*.cpp files Aliased: 36 System*.cpp files (including 19 Sentry.System*.cpp) Extra code: 22MB of additional C++ source

Why This Happens

When we're using assembly alias on dependencies like System.Text.Json, System.Collections.Immutable, etc., IL2CPP treats them as completely new types (e.g., Sentry.System.Text.Json) instead of recognizing them as the same types Unity already has.

This prevents code sharing with Unity's built-in System libraries, type unification where the same type from different assemblies can share implementation and linker optimization that would normally deduplicate identical code.

As a result, IL2CPP generates and compiles a complete separate implementation of all the aliased System.* types, adding ~13MB to the final binary.

Additional Notes

Files that are getting generated

The following 19 files exist only in the aliased build:

  • Sentry.System.Buffers.cpp
  • Sentry.System.Collections.Immutable.cpp
  • Sentry.System.Memory.cpp (+ _1)
  • Sentry.System.Numerics.Vectors.cpp (+ _1)
  • Sentry.System.Reflection.Metadata.cpp (+ _1, _2, _3, _4)
  • Sentry.System.Runtime.CompilerServices.Unsafe.cpp
  • Sentry.System.Text.Encodings.Web.cpp
  • Sentry.System.Text.Json.cpp (+ _1, _2, _3, _4)
  • Sentry.System.Threading.Tasks.Extensions.cpp

These represent the aliased dependencies that are being compiled as possibly duplicate code.

bitsandfoxes avatar Nov 11 '25 14:11 bitsandfoxes

UNITY-182

linear[bot] avatar Nov 11 '25 14:11 linear[bot]