Skip to content

Commit

Permalink
Share MetadataIndex instances across multiple generators
Browse files Browse the repository at this point in the history
This should drastically cut down on memory demands (from 1.2GB to store 128 copies of MetadataIndex to just 1 of them). It should presumably improve warmup perf as well, since we only create one index instead of 128.
  • Loading branch information
AArnott committed May 18, 2022
1 parent 1b84b4f commit a478cd4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 154 deletions.
21 changes: 11 additions & 10 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -312,6 +312,7 @@ public class Generator : IDisposable
private readonly TypeSyntaxSettings functionPointerTypeSettings;
private readonly TypeSyntaxSettings errorMessageTypeSettings;

private readonly PEReader peReader;
private readonly GeneratorOptions options;
private readonly CSharpCompilation? compilation;
private readonly CSharpParseOptions? parseOptions;
Expand Down Expand Up @@ -351,6 +352,8 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
this.InputAssemblyName = Path.GetFileNameWithoutExtension(metadataLibraryPath);
this.MetadataIndex = MetadataIndex.Get(metadataLibraryPath, compilation?.Options.Platform);
this.ApiDocs = docs;
this.peReader = new PEReader(MetadataIndex.CreateFileView(metadataLibraryPath));
this.Reader = this.peReader.GetMetadataReader();

this.options = options;
this.options.Validate();
Expand Down Expand Up @@ -419,9 +422,7 @@ private enum FriendlyOverloadOf

internal Docs? ApiDocs { get; }

internal ReadOnlyCollection<TypeDefinition> Apis => this.MetadataIndex.Apis;

internal MetadataReader Reader => this.MetadataIndex.Reader;
internal MetadataReader Reader { get; }

internal LanguageVersion LanguageVersion => this.parseOptions?.LanguageVersion ?? LanguageVersion.CSharp9;

Expand Down Expand Up @@ -713,7 +714,7 @@ public bool TryGetEnumName(string enumValueName, [NotNullWhen(true)] out string?
/// <param name="cancellationToken">A cancellation token.</param>
public void GenerateAllExternMethods(CancellationToken cancellationToken)
{
foreach (MethodDefinitionHandle methodHandle in this.Apis.SelectMany(api => api.GetMethods()))
foreach (MethodDefinitionHandle methodHandle in this.MetadataIndex.Apis.SelectMany(api => this.Reader.GetTypeDefinition(api).GetMethods()))
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -741,7 +742,7 @@ public void GenerateAllExternMethods(CancellationToken cancellationToken)
/// <param name="cancellationToken">A cancellation token.</param>
public void GenerateAllConstants(CancellationToken cancellationToken)
{
foreach (FieldDefinitionHandle fieldDefHandle in this.Apis.SelectMany(api => api.GetFields()))
foreach (FieldDefinitionHandle fieldDefHandle in this.MetadataIndex.Apis.SelectMany(api => this.Reader.GetTypeDefinition(api).GetFields()))
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -772,7 +773,7 @@ public void GenerateAllConstants(CancellationToken cancellationToken)
public bool TryGenerateAllExternMethods(string moduleName, CancellationToken cancellationToken)
{
bool successful = false;
foreach (MethodDefinitionHandle methodHandle in this.Apis.SelectMany(api => api.GetMethods()))
foreach (MethodDefinitionHandle methodHandle in this.MetadataIndex.Apis.SelectMany(api => this.Reader.GetTypeDefinition(api).GetMethods()))
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -1885,7 +1886,7 @@ internal bool TryGetTypeDefHandle(TypeReferenceHandle typeRefHandle, out Qualifi
return this.SuperGenerator.TryGetTypeDefinitionHandle(new QualifiedTypeReferenceHandle(this, typeRefHandle), out typeDefHandle);
}

if (this.MetadataIndex.TryGetTypeDefHandle(typeRefHandle, out TypeDefinitionHandle localTypeDefHandle))
if (this.MetadataIndex.TryGetTypeDefHandle(this.Reader, typeRefHandle, out TypeDefinitionHandle localTypeDefHandle))
{
typeDefHandle = new QualifiedTypeDefinitionHandle(this, localTypeDefHandle);
return true;
Expand All @@ -1895,7 +1896,7 @@ internal bool TryGetTypeDefHandle(TypeReferenceHandle typeRefHandle, out Qualifi
return false;
}

internal bool TryGetTypeDefHandle(TypeReferenceHandle typeRefHandle, out TypeDefinitionHandle typeDefHandle) => this.MetadataIndex.TryGetTypeDefHandle(typeRefHandle, out typeDefHandle);
internal bool TryGetTypeDefHandle(TypeReferenceHandle typeRefHandle, out TypeDefinitionHandle typeDefHandle) => this.MetadataIndex.TryGetTypeDefHandle(this.Reader, typeRefHandle, out typeDefHandle);

internal bool TryGetTypeDefHandle(TypeReference typeRef, out TypeDefinitionHandle typeDefHandle) => this.TryGetTypeDefHandle(typeRef.Namespace, typeRef.Name, out typeDefHandle);

Expand Down Expand Up @@ -2048,7 +2049,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
MetadataIndex.Return(this.MetadataIndex);
this.peReader.Dispose();
}
}

Expand Down
Loading

0 comments on commit a478cd4

Please sign in to comment.