Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize templates resource loading peformance #9090

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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