Skip to content

[native] Generate less relocations in libxamarin-app.so #10195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2025

Conversation

grendello
Copy link
Contributor

@grendello grendello commented Jun 13, 2025

Another in series of PRs which reduces the number of relocations in
libxamarin-app.so.

This PR changes the way compressed assemblies (CoreCLR and MonoVM),
environment variables (CoreCLR) and runtime properties (CoreCLR)
are stored.

Compressed assemblies no longer use one target buffer per assembly,
but instead allocate a single big buffer for all the decompressed
assemblies, addressing the data using indexes into the buffer instead
of pointers.

Environment variables and runtime properties now use string blobs
instead of string pointers.

For the dotnet new maui -sc sample, this PR reduces the number of
relocations in libxamarin-app.so from 394 (in the main branch)
to 136 entries.

Size of libxamarin-app.so is slightly reduced:

  • arm64: from 370936 to 365800 bytes (1.4%)
  • x64: from 362520 to 357328 bytes (1.4%)

Modest improvements in startup time, the sample app starts faster by
0.71% on Pixel 9 with Android 16.

@grendello grendello force-pushed the dev/grendel/clr-host-less-relocs branch from e282048 to 313476d Compare June 16, 2025 16:09
Instead of separate per-assembly buffers, there's now a single buffer
and assembly descriptors use offsets into this buffer to store data.
This removes N + 1 relocs where N is the number of assemblies in the app.
The extra removed relocation is for the pointer which used to point to
the descriptors array, which is now made public.

MonoVM runtime not yet updated.
Eliminates N*2 relocations
@grendello grendello force-pushed the dev/grendel/clr-host-less-relocs branch from 313476d to d0a23d4 Compare June 17, 2025 09:41
@grendello grendello requested a review from Copilot June 17, 2025 15:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors how bundled assemblies, DSO names, environment variables, and runtime properties are represented in the native and CLR stubs—moving from pointer-based structures to index-and-blob layouts to reduce relocations in libxamarin-app.so.

  • Replace CompressedAssemblies pointer fields with separate count, descriptors array, and contiguous data buffer with offsets.
  • Mirror these changes in the CLR stub and update all decompression logic to use buffer offsets plus bounds checks.
  • Update MSBuild native-assembly generators to emit the new index/blobs for assemblies, DSO names, environment variables, and runtime properties.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/native/mono/xamarin-app-stub/xamarin-app.hh Remove data pointer, add buffer_offset and top-level symbols for counts, descriptors[], and data_buffer[].
src/native/mono/xamarin-app-stub/application_dso_stub.cc Stub globals now use compressed_assembly_count, compressed_assembly_descriptors[], and data buffer symbols.
src/native/mono/monodroid/embedded-assemblies.cc Decompression now indexes into a shared buffer via buffer_offset with added bounds checks.
src/native/mono/monodroid/assembly-store.cc Parallel decompression refactor in AssemblyStore: use buffer offsets and bounds checks.
src/native/clr/xamarin-app-stub/application_dso_stub.cc Update CLR stub globals to use index arrays and data blobs instead of pointer arrays.
src/native/clr/include/xamarin-app.hh Update all structs (assemblies, DSO cache, runtime properties, environment vars) to use index fields.
src/native/clr/include/runtime-base/monodroid-dl.hh Add get_dso_name helper to read names from a single blob via name_index.
src/native/clr/host/host.cc Use runtime_properties_data blob and indices in clr_get_runtime_property.
src/native/clr/host/assembly-store.cc Mirror native AssemblyStore refactor for CLR host code, using buffer offsets.
src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrStringBlob.cs Add XML summary for Add and describe its behavior.
src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrGenerator.cs Special-case zero-length arrays when emitting bytes, adjusting indentation/comma logic.
src/Xamarin.Android.Build.Tasks/Utilities/CompressedAssembliesNativeAssemblyGenerator.cs Remove old pointer-based struct providers; emit count, descriptors array, and data buffer globals.
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGeneratorCLR.cs Update CLR generator to emit new blobs and index structs for env vars, DSO names, and runtime props.
Comments suppressed due to low confidence (1)

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrStringBlob.cs:29

  • [nitpick] The XML doc comment for Add is missing <param> and <returns> tags. Consider adding <param name="s">The string to add.</param> and <returns>Tuple of offset and length in the blob.</returns> for completeness.
	/// <summary>

@grendello grendello marked this pull request as ready for review June 17, 2025 15:50
@grendello grendello merged commit f37158e into main Jun 17, 2025
59 checks passed
@grendello grendello deleted the dev/grendel/clr-host-less-relocs branch June 17, 2025 18:57
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.

1 participant