Skip to content

Commit

Permalink
perf: Optimize templates resource loading peformance (#9090)
Browse files Browse the repository at this point in the history
perf: optimize templates resource loading
  • Loading branch information
filzrev authored Aug 16, 2023
1 parent bfefe72 commit 706a10c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/Docfx.Build.Engine/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private List<IDocumentProcessor> LoadSchemaDrivenDocumentProcessors(DocumentBuil
}

var siteHostName = TryGetPublishTargetSiteHostNameFromEnvironment();
var markdigMarkdownService = CreateMarkdigMarkdownService(parameter);
var markdigMarkdownService = CreateMarkdigMarkdownService(parameter, resource);
foreach (var pair in resource.GetResources(@"^schemas/.*\.schema\.json"))
{
var fileName = Path.GetFileName(pair.Path);
Expand Down Expand Up @@ -262,10 +262,8 @@ private List<IDocumentProcessor> LoadSchemaDrivenDocumentProcessors(DocumentBuil
}
}

private MarkdigMarkdownService CreateMarkdigMarkdownService(DocumentBuildParameters parameters)
private MarkdigMarkdownService CreateMarkdigMarkdownService(DocumentBuildParameters parameters, CompositeResourceReader resourceProvider)
{
var resourceProvider = parameters.TemplateManager?.CreateTemplateResource();

return new MarkdigMarkdownService(
new MarkdownServiceParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CompositeResourceReader(IEnumerable<ResourceFileReader> declaredReaders)
{
_readers = declaredReaders.ToArray();
IsEmpty = _readers.Length == 0;
Names = _readers.SelectMany(s => s.Names).Distinct();
Names = _readers.SelectMany(s => s.Names).Distinct().ToArray();
}

public override Stream GetResourceStream(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class LocalFileResourceReader : ResourceFileReader
private static StringComparer ResourceComparer = StringComparer.Ordinal;
private string _directory = null;
private readonly int _maxDepth;
private readonly HashSet<string> NamesHashSet;

public override string Name { get; }
public override IEnumerable<string> Names { get; }
Expand All @@ -24,7 +25,8 @@ public LocalFileResourceReader(string directory, int maxSearchLevel = MaxSearchL
Name = _directory;
_maxDepth = maxSearchLevel;
var includedFiles = GetFiles(_directory, "*", maxSearchLevel);
Names = includedFiles.Select(s => PathUtility.MakeRelativePath(_directory, s)).Where(s => s != null);
Names = includedFiles.Select(s => PathUtility.MakeRelativePath(_directory, s)).Where(s => s != null).ToArray();
NamesHashSet = Names.ToHashSet(ResourceComparer);

IsEmpty = !Names.Any();
}
Expand All @@ -34,7 +36,10 @@ public override Stream GetResourceStream(string name)
if (IsEmpty) return null;

// in case relative path is combined by backslash \
if (!Names.Contains(StringExtension.ToNormalizedPath(name.Trim()), ResourceComparer)) return null;
var normalizedName = StringExtension.ToNormalizedPath(name.Trim());
if (!NamesHashSet.Contains(normalizedName))
return null;

var filePath = Path.Combine(_directory, name);
return new FileStream(filePath, FileMode.Open, FileAccess.Read);
}
Expand Down

0 comments on commit 706a10c

Please sign in to comment.