Skip to content

Commit

Permalink
perf: optimize default plugin load speed (#8810)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored May 30, 2023
1 parent e31235d commit a902f31
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Microsoft.DocAsCode.App/Helpers/DocumentBuilderWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Immutable;
using System.Net;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Microsoft.DocAsCode.Build.ConceptualDocuments;
using Microsoft.DocAsCode.Build.Engine;
using Microsoft.DocAsCode.Build.ManagedReference;
Expand Down Expand Up @@ -73,10 +75,10 @@ private static IEnumerable<Assembly> LoadPluginAssemblies(string pluginDirectory
string assemblyName = Path.GetFileNameWithoutExtension(assemblyFile);
if (!string.IsNullOrEmpty(assemblyName))
{
if (assemblyName == "Microsoft.DocAsCode.EntityModel")
if (assemblyName == "Microsoft.DocAsCode.EntityModel" || assemblyName.StartsWith("System."))
{
// work around, don't load assembly Microsoft.DocAsCode.EntityModel.
Logger.LogVerbose("Skipping assembly: Microsoft.DocAsCode.EntityModel.");
Logger.LogVerbose($"Skipping assembly: {assemblyName}");
continue;
}
if (assemblyName == typeof(ValidateBookmark).Assembly.GetName().Name)
Expand All @@ -92,6 +94,12 @@ private static IEnumerable<Assembly> LoadPluginAssemblies(string pluginDirectory
continue;
}

if (!IsDocfxPluginAssembly(assemblyFile))
{
Logger.LogVerbose($"Skipping non-plugin assembly: {assemblyName}.");
continue;
}

Assembly assembly;
try
{
Expand All @@ -114,6 +122,26 @@ private static IEnumerable<Assembly> LoadPluginAssemblies(string pluginDirectory
}
}
}

static bool IsDocfxPluginAssembly(string assemblyFile)
{
try
{
// Determines if the input assembly file is potentially a docfx plugin assembly
// by checking if referenced assemblies contains Microsoft.DocAsCode.Plugins using MetadataReader
using (var stream = File.OpenRead(assemblyFile))
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
return metadataReader.AssemblyReferences.Any(a => metadataReader.GetString(metadataReader.GetAssemblyReference(a).Name) == "Microsoft.DocAsCode.Plugins");
}
}
catch (Exception ex)
{
Logger.LogVerbose($"Skipping file {assemblyFile} due to load failure: {ex.Message}");
return false;
}
}
}

private static List<DocumentBuildParameters> ConfigToParameter(BuildJsonConfig config, BuildOptions options, TemplateManager templateManager, string baseDirectory, string outputDirectory, string templateDir)
Expand Down

0 comments on commit a902f31

Please sign in to comment.