Skip to content

Commit

Permalink
Merge pull request #307 from microsoft/fixYamlPerf
Browse files Browse the repository at this point in the history
Fix startup perf (YAML to messagepack)
  • Loading branch information
AArnott committed Jun 23, 2021
2 parents 2c05bc0 + b6c16c4 commit 690e31d
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 112 deletions.
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -33,7 +34,7 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.216" PrivateAssets="all" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.321" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.333" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
52 changes: 52 additions & 0 deletions src/Microsoft.Windows.CsWin32/ApiDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace ScrapeDocs
{
using System;
using System.Collections.Generic;
using MessagePack;

/// <summary>
/// Captures all the documentation we have available for an API.
/// </summary>
[MessagePackObject]
public class ApiDetails
{
/// <summary>
/// Gets or sets the URL that provides more complete documentation for this API.
/// </summary>
[Key(0)]
public Uri? HelpLink { get; set; }

/// <summary>
/// Gets or sets a summary of what the API is for.
/// </summary>
[Key(1)]
public string? Description { get; set; }

/// <summary>
/// Gets or sets the remarks section of the documentation.
/// </summary>
[Key(2)]
public string? Remarks { get; set; }

/// <summary>
/// Gets a collection of parameter docs, keyed by their names.
/// </summary>
[Key(3)]
public Dictionary<string, string> Parameters { get; } = new();

/// <summary>
/// Gets a collection of field docs, keyed by their names.
/// </summary>
[Key(4)]
public Dictionary<string, string> Fields { get; } = new();

/// <summary>
/// Gets or sets the documentation of the return value of the API, if applicable.
/// </summary>
[Key(5)]
public string? ReturnValue { get; set; }
}
}
30 changes: 6 additions & 24 deletions src/Microsoft.Windows.CsWin32/Docs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Windows.CsWin32
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using MessagePack;
using ScrapeDocs;

internal class Docs
{
Expand All @@ -24,35 +26,15 @@ private Docs(Dictionary<string, ApiDetails> apisAndDocs)

private static Docs Create()
{
using Stream? docsYamlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ThisAssembly.RootNamespace + ".apidocs.yml");
if (docsYamlStream is null)
using Stream? docsStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ThisAssembly.RootNamespace + ".apidocs.msgpack");
if (docsStream is null)
{
////return new Docs(new Dictionary<string, ApiDetails>());
throw new Exception("YAML documentation not found.");
throw new Exception("Documentation not found.");
}

using var yamlTextReader = new StreamReader(docsYamlStream);
var deserializer = new YamlDotNet.Serialization.Deserializer();
var data = deserializer.Deserialize<Dictionary<string, ApiDetails>>(yamlTextReader);

var data = MessagePackSerializer.Deserialize<Dictionary<string, ApiDetails>>(docsStream);
return new Docs(data);
}

#pragma warning disable CA1812 // uninstantiated class is deserialized into
internal class ApiDetails
#pragma warning restore CA1812 // uninstantiated class is deserialized into
{
public Uri? HelpLink { get; set; }

public string? Description { get; set; }

public string? Remarks { get; set; }

public Dictionary<string, string>? Parameters { get; set; }

public Dictionary<string, string>? Fields { get; set; }

public string? ReturnValue { get; set; }
}
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.Windows.CsWin32
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ScrapeDocs;
using static FastSyntaxFactory;

/// <summary>
Expand Down Expand Up @@ -1791,7 +1792,7 @@ static void EmitLine(StringBuilder stringBuilder, string yamlDocSrc)
stringBuilder.Append(yamlDocSrc.Trim());
}

static void EmitDoc(string yamlDocSrc, StringBuilder docCommentsBuilder, Docs.ApiDetails? docs, string docsAnchor)
static void EmitDoc(string yamlDocSrc, StringBuilder docCommentsBuilder, ApiDetails? docs, string docsAnchor)
{
if (yamlDocSrc.Contains('\n'))
{
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.Windows.CsWin32/Microsoft.Windows.CsWin32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<YamlDocOutputPath Include="../../obj/$(Configuration)/apidocs.yml" Visible="false" />
<DocOutputPath Include="../../obj/$(Configuration)/apidocs.msgpack" Visible="false" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,7 +37,7 @@

<ItemGroup>
<EmbeddedResource Include="templates\**\*.cs" />
<EmbeddedResource Include="@(YamlDocOutputPath)" />
<EmbeddedResource Include="@(DocOutputPath)" />
</ItemGroup>

<ItemDefinitionGroup>
Expand All @@ -47,14 +47,14 @@
</ItemDefinitionGroup>

<ItemGroup>
<PackageReference Include="MessagePack" Version="2.2.85" />
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="Nullable" Version="1.3.0" />
<PackageReference Include="System.Memory" Version="4.5.4" PrivateAssets="none" />
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
<PackageReference Include="System.Memory" Version="4.5.4" PrivateAssets="none" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
</metadata>
<files>
<file src="$BaseOutputPath$Microsoft.Windows.CsWin32.dll" target="analyzers\cs\Microsoft.Windows.CsWin32.dll" />
<file src="$BaseOutputPath$YamlDotNet.dll" target="analyzers\cs\YamlDotNet.dll" />
<file src="$BaseOutputPath$MessagePack.dll" target="analyzers\cs\MessagePack.dll" />
<file src="$BaseOutputPath$MessagePack.Annotations.dll" target="analyzers\cs\MessagePack.Annotations.dll" />
<file src="$BaseOutputPath$Microsoft.Bcl.AsyncInterfaces.dll" target="analyzers\cs\Microsoft.Bcl.AsyncInterfaces.dll" />
<file src="$BaseOutputPath$System.Text.Json.dll" target="analyzers\cs\System.Text.Json.dll" />
<file src="$BaseOutputPath$System.Text.Encodings.Web.dll" target="analyzers\cs\System.Text.Encodings.Web.dll" />
Expand Down
11 changes: 6 additions & 5 deletions src/Microsoft.Windows.CsWin32/Microsoft.Windows.CsWin32.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>

<Target Name="GenerateAndConsumeDocsYaml"
<Target Name="GenerateAndConsumeDocs"
DependsOnTargets="ResolveProjectReferences"
BeforeTargets="CoreCompile"
Inputs="@(ScraperTool)"
Outputs="@(YamlDocOutputPath)"
Outputs="@(DocOutputPath)"
Condition=" '$(DesignTimeBuild)' != 'true' ">
<Message Importance="high" Text="Regenerating @(YamlDocOutputPath->'%(FullPath)'). This may take a few minutes..." />
<Exec Command="dotnet @(ScraperTool) ../../ext/sdk-api/sdk-api-src/content @(YamlDocOutputPath)"
<Message Importance="high" Text="Regenerating @(DocOutputPath->'%(FullPath)'). This may take a few minutes..." />
<Exec Command="dotnet @(ScraperTool) ../../ext/sdk-api/sdk-api-src/content @(DocOutputPath)"
StandardOutputImportance="high"/>
</Target>

Expand All @@ -24,7 +24,8 @@
<ItemGroup>
<!-- Analysis of C# projects -->
<TfmSpecificPackageFile Include="$(TargetPath)" PackagePath="analyzers\cs\" />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'YamlDotNet.dll' " />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'MessagePack.dll' " />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'MessagePack.Annotations.dll' " />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'Microsoft.Bcl.AsyncInterfaces.dll' " />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'System.Text.Json.dll' " />
<TfmSpecificPackageFile Include="@(ReferencePath)" PackagePath="analyzers\cs\" Condition=" '%(FileName)%(Extension)' == 'System.Text.Encodings.Web.dll' " />
Expand Down
Loading

0 comments on commit 690e31d

Please sign in to comment.