Skip to content

Conversation

@MichalStrehovsky
Copy link
Member

Removes some unnecessary UTF-8 -> UTF-16 -> UTF-8 conversions.

Before this change, when compiling hello world: 529000 byte[] allocations, 403000 string allocations.
After this change: 552000 byte[] allocations, 302000 string allocations.

Cc @dotnet/ilc-contrib

Copilot AI review requested due to automatic review settings December 2, 2025 23:05
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

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 optimizes the ILCompiler by switching the NameMangler from UTF-16 strings to UTF-8, eliminating unnecessary encoding conversions during compilation. The change reduces string allocations by approximately 101,000 instances when compiling a hello world application (from 403,000 to 302,000), though it increases byte array allocations slightly (from 529,000 to 552,000).

Key Changes:

  • Modified NameMangler.GetMangledTypeName() and related methods to return Utf8String instead of string
  • Implemented UTF-8 versions of name sanitization and disambiguation logic
  • Added UTF-8 string writing overloads in debug info builders and section writers

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/coreclr/tools/Common/Compiler/NameMangler.cs Changed abstract method signatures from string to Utf8String for type and name mangling
src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs Implemented UTF-8 versions of name mangling, sanitization, and disambiguation logic; added CountDigits helper for efficient numeric formatting
src/coreclr/tools/Common/Internal/Text/Utf8String.cs Added Concat method to combine multiple UTF-8 strings without conversion
src/coreclr/tools/Common/Internal/Text/Utf8StringBuilder.cs Added constructor accepting initial capacity parameter
src/coreclr/tools/Common/Compiler/ObjectWriter/SectionWriter.cs Added WriteUtf8String(Utf8String) overload to write UTF-8 strings directly
src/coreclr/tools/Common/TypeSystem/TypesDebugInfoWriter/TypesDebugInfoWriter.cs Changed interface methods and type descriptors to use Utf8String
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfInfoWriter.cs Added WriteStringReference(Utf8String) overload for UTF-8 string references
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfBuilder.cs Changed GetMangledName to return Utf8String
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/CodeView/CodeViewTypesBuilder.cs Updated user-defined types to use Utf8String; added Write(Utf8String) overload
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/CodeView/CodeViewSymbolsBuilder.cs Updated WriteUserDefinedTypes signature and added Write(Utf8String) overload
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/WindowsNodeMangler.cs Added ToString() call to convert UTF-8 result to string for method table names
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UnixNodeMangler.cs Added ToString() call to convert UTF-8 result to string for method table names

Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

Nice! Do we have any unit tests for the name mangler? If not I could have copilot give it a shot

@MichalStrehovsky
Copy link
Member Author

MichalStrehovsky commented Dec 3, 2025

Nice! Do we have any unit tests for the name mangler? If not I could have copilot give it a shot

I validated this by comparing the output object file (it was byte identical). We don't have unit tests; we can try giving it a shot if copilot comes up with something useful. But name mangling is typically not touched so I can only see the unit test getting in the way if we ever change how we want to mangle things (it is an implementation detail and not a contract). A unit test would definitely make this PR more annoying to do.

@MichalStrehovsky MichalStrehovsky merged commit f7a3ad3 into dotnet:main Dec 3, 2025
92 of 96 checks passed
@MichalStrehovsky MichalStrehovsky deleted the uf8mangler branch December 3, 2025 13:16
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.

3 participants