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

Code before:
                        sb.Append(GetMangledTypeName(typeDefinition));

                        var inst = type.Instantiation;
                        sb.Append(EnterNameScopeSequence);
                        for (int i = 0; i < inst.Length; i++)
                        {
                            if (i > 0)
                                sb.Append("__"u8);
                            sb.Append(GetMangledTypeName(inst[i]));
                        }
                        sb.Append(ExitNameScopeSequence);
                        mangledName = sb.ToUtf8String();
                    }
                    else if (type is IPrefixMangledMethod)
                    {
                        mangledName = GetPrefixMangledMethodName((IPrefixMangledMethod)type);
                    }
                    else if (type is IPrefixMangledType)
                    {
                        mangledName = GetPrefixMangledTypeName((IPrefixMangledType)type);
                    }
                    else
                    {
                        // This is a type definition. Since we didn't fall in the `is EcmaType` case above,
                        // it's likely a compiler-generated type.
                        mangledName = new Utf8String(SanitizeName(((DefType)type).GetFullName()));
                    }
                    break;
            }

            lock (this)
            {
                // Ensure that name is unique and update our tables accordingly.
                _mangledTypeNames.TryAdd(type, mangledName);
            }

            return mangledName;
        }

        private Dictionary<MethodDesc, Utf8String> _mangledMethodNames = new Dictionary<MethodDesc, Utf8String>();
        private Dictionary<MethodDesc, Utf8String> _unqualifiedMangledMethodNames = new Dictionary<MethodDesc, Utf8String>();

        public override Utf8String GetMangledMethodName(MethodDesc method)
        {
            Utf8String utf8MangledName;
            lock (this)
            {
                if (_mangledMethodNames.TryGetValue(method, out utf8MangledName))
                    return utf8MangledName;
            }


Review comment (security):
This has unnecessary conversions too. Can be something along the lines of:

```suggestion
                        var defType = (DefType)type;
                        var sb = new Utf8StringBuilder();
                        if (defType.Namespace.Length > 0)
                            sb.Append(SanitizeName(defType.Namespace)).Append('_');
                        sb.Append(SanitizeName(defType.Name));
                        mangledName = sb.ToString();
```

Code after:
                        sb.Append(GetMangledTypeName(typeDefinition));

                        var inst = type.Instantiation;
                        sb.Append(EnterNameScopeSequence);
                        for (int i = 0; i < inst.Length; i++)
                        {
                            if (i > 0)
                                sb.Append("__"u8);
                            sb.Append(GetMangledTypeName(inst[i]));
                        }
                        sb.Append(ExitNameScopeSequence);
                        mangledName = sb.ToUtf8String();
                    }
                    else if (type is IPrefixMangledMethod)
                    {
                        mangledName = GetPrefixMangledMethodName((IPrefixMangledMethod)type);
                    }
                    else if (type is IPrefixMangledType)
                    {
                        mangledName = GetPrefixMangledTypeName((IPrefixMangledType)type);
                    }
                    else
                    {
                        // This is a type definition. Since we didn't fall in the `is EcmaType` case above,
                        // it's likely a compiler-generated type.
                        var defType = (DefType)type;
                        var sb = new Utf8StringBuilder();
                        if (defType.Namespace.Length > 0)
                            sb.Append(SanitizeName(defType.Namespace)).Append('_');

                        sb.Append(SanitizeName(defType.Name));
                        mangledName = sb.ToUtf8String();
                    }
                    break;
            }

            lock (this)
            {
                // Ensure that name is unique and update our tables accordingly.
                _mangledTypeNames.TryAdd(type, mangledName);
            }

            return mangledName;
        }

        private Dictionary<MethodDesc, Utf8String> _mangledMethodNames = new Dictionary<MethodDesc, Utf8String>();
        private Dictionary<MethodDesc, Utf8String> _unqualifiedMangledMethodNames = new Dictionary<MethodDesc, Utf8String>();

        public override Utf8String GetMangledMethodName(MethodDesc method)
        {
            Utf8String utf8MangledName;