Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs (C#)

Code before:
                        name = _mangledTypeNames[type];
                    }
                    return name;
                }
            }

            Utf8String mangledName;

            switch (type.Category)
            {
                case TypeFlags.Array:
                    mangledName = new Utf8StringBuilder().Append("__MDArray"u8)
                                  .Append(EnterNameScopeSequence)
                                  .Append(GetMangledTypeName(((ArrayType)type).ElementType))
                                  .Append(DelimitNameScopeSequence)
                                  .Append(((ArrayType)type).Rank.ToStringInvariant())
                                  .Append(ExitNameScopeSequence).ToUtf8String();
                    break;
                case TypeFlags.SzArray:
                    mangledName = new Utf8StringBuilder().Append("__Array"u8)
                        .Append(NestMangledName(GetMangledTypeName(((ArrayType)type).ElementType))).ToUtf8String();
                    break;
                case TypeFlags.ByRef:
                    mangledName = new Utf8StringBuilder()
                        .Append(GetMangledTypeName(((ByRefType)type).ParameterType))
                        .Append(NestMangledName(new Utf8String("ByRef"u8))).ToUtf8String();
                    break;
                case TypeFlags.Pointer:
                    mangledName = new Utf8StringBuilder()
                        .Append(GetMangledTypeName(((PointerType)type).ParameterType))
                        .Append(NestMangledName(new Utf8String("Pointer"u8))).ToUtf8String();
                    break;
                case TypeFlags.FunctionPointer:
                {
                    var fnPtrType = (FunctionPointerType)type;
                    var sb = new Utf8StringBuilder();
                    sb.Append("__FnPtr_"u8).Append(((int)fnPtrType.Signature.Flags).ToString("X2")).Append(EnterNameScopeSequence);
                    sb.Append(GetMangledTypeName(fnPtrType.Signature.ReturnType));

                    sb.Append(EnterNameScopeSequence);
                    for (int i = 0; i < fnPtrType.Signature.Length; i++)
                    {
                        if (i != 0)
                            sb.Append(DelimitNameScopeSequence);
                        sb.Append(GetMangledTypeName(fnPtrType.Signature[i]));
                    }
                    sb.Append(ExitNameScopeSequence);

                    sb.Append(ExitNameScopeSequence);
                    mangledName = sb.ToUtf8String();
                    break;

Review comment (performance):
This has an unnecessary allocation (also `Pointer` below). We could pull `new Utf8String("ByRef"u8)` into a static readonly field to avoid it.

Or better yet, just inline `NestMangledName` into here (then we'd use the flavor of `Append` that takes ReadOnlySpan and avoid materializing the `Utf8String` altogether). All of the remaining uses of `NestMangledName` just cause an unnecessary `Utf8String` allocation.

Code after:
                        name = _mangledTypeNames[type];
                    }
                    return name;
                }
            }

            Utf8String mangledName;

            switch (type.Category)
            {
                case TypeFlags.Array:
                    mangledName = new Utf8StringBuilder().Append("__MDArray"u8)
                                  .Append(EnterNameScopeSequence)
                                  .Append(GetMangledTypeName(((ArrayType)type).ElementType))
                                  .Append(DelimitNameScopeSequence)
                                  .Append(((ArrayType)type).Rank.ToStringInvariant())
                                  .Append(ExitNameScopeSequence).ToUtf8String();
                    break;
                case TypeFlags.SzArray:
                    mangledName = new Utf8StringBuilder().Append("__Array"u8)
                        .Append(EnterNameScopeSequence).Append(GetMangledTypeName(((ArrayType)type).ElementType)).Append(ExitNameScopeSequence).ToUtf8String();
                    break;
                case TypeFlags.ByRef:
                    mangledName = new Utf8StringBuilder()
                        .Append(GetMangledTypeName(((ByRefType)type).ParameterType))
                        .Append(EnterNameScopeSequence).Append("ByRef"u8).Append(ExitNameScopeSequence).ToUtf8String();
                    break;
                case TypeFlags.Pointer:
                    mangledName = new Utf8StringBuilder()
                        .Append(GetMangledTypeName(((PointerType)type).ParameterType))
                        .Append(EnterNameScopeSequence).Append("Pointer"u8).Append(ExitNameScopeSequence).ToUtf8String();
                    break;
                case TypeFlags.FunctionPointer:
                {
                    var fnPtrType = (FunctionPointerType)type;
                    var sb = new Utf8StringBuilder();
                    sb.Append("__FnPtr_"u8).Append(((int)fnPtrType.Signature.Flags).ToString("X2")).Append(EnterNameScopeSequence);
                    sb.Append(GetMangledTypeName(fnPtrType.Signature.ReturnType));

                    sb.Append(EnterNameScopeSequence);
                    for (int i = 0; i < fnPtrType.Signature.Length; i++)
                    {
                        if (i != 0)
                            sb.Append(DelimitNameScopeSequence);
                        sb.Append(GetMangledTypeName(fnPtrType.Signature[i]));
                    }
                    sb.Append(ExitNameScopeSequence);

                    sb.Append(ExitNameScopeSequence);
                    mangledName = sb.ToUtf8String();
                    break;