From 3b6a3bbc7f99f8120e2973eb8a2d251cf65745fd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 31 Aug 2024 21:20:13 +1000 Subject: [PATCH] avoid some alloc in config reading (#711) --- src/ConfigReader/ConfigResult.cs | 32 +++++++++++----------- src/ConfigReader/ExcludeToFilterBuilder.cs | 8 +++++- src/ConfigReader/LogBuilder.cs | 25 ++++++++++------- src/MarkdownSnippets.MsBuild/DocoTask.cs | 7 ++++- src/MarkdownSnippets.Tool/Program.cs | 5 ++-- 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/ConfigReader/ConfigResult.cs b/src/ConfigReader/ConfigResult.cs index 510f0071..1a127a00 100644 --- a/src/ConfigReader/ConfigResult.cs +++ b/src/ConfigReader/ConfigResult.cs @@ -1,19 +1,19 @@ public class ConfigResult { - public bool? ReadOnly; - public bool ValidateContent; - public bool OmitSnippetLinks; - public LinkFormat LinkFormat; - public DocumentConvention Convention; - public int TocLevel; - public int MaxWidth; - public List UrlsAsSnippets = []; - public List ExcludeDirectories = []; - public List ExcludeMarkdownDirectories = []; - public List ExcludeSnippetDirectories = []; - public bool? WriteHeader; - public string? Header; - public string? UrlPrefix; - public List TocExcludes = []; - public bool TreatMissingAsWarning; + public bool? ReadOnly { get; init; } + public bool ValidateContent { get; init; } + public bool OmitSnippetLinks { get; init; } + public LinkFormat LinkFormat { get; init; } + public DocumentConvention Convention { get; init; } + public int TocLevel { get; init; } + public int MaxWidth { get; init; } + public List? UrlsAsSnippets { get; init; } + public List? ExcludeDirectories { get; init; } + public List? ExcludeMarkdownDirectories { get; init; } + public List? ExcludeSnippetDirectories { get; init; } + public bool? WriteHeader { get; init; } + public string? Header { get; init; } + public string? UrlPrefix { get; init; } + public List? TocExcludes { get; init; } + public bool TreatMissingAsWarning { get; init; } } \ No newline at end of file diff --git a/src/ConfigReader/ExcludeToFilterBuilder.cs b/src/ConfigReader/ExcludeToFilterBuilder.cs index ed422739..0e249b20 100644 --- a/src/ConfigReader/ExcludeToFilterBuilder.cs +++ b/src/ConfigReader/ExcludeToFilterBuilder.cs @@ -1,6 +1,6 @@ static class ExcludeToFilterBuilder { - public static ShouldIncludeDirectory ExcludesToFilter(List excludes) => + public static ShouldIncludeDirectory ExcludesToFilter(List? excludes) => path => { if (DefaultDirectoryExclusions.ShouldExcludeDirectory(path)) @@ -8,6 +8,12 @@ public static ShouldIncludeDirectory ExcludesToFilter(List excludes) => return false; } + if(excludes == null || + excludes.Count == 0) + { + return false; + } + if (!excludes.Any(path.Contains)) { return true; diff --git a/src/ConfigReader/LogBuilder.cs b/src/ConfigReader/LogBuilder.cs index c5a0d04b..9b9376a1 100644 --- a/src/ConfigReader/LogBuilder.cs +++ b/src/ConfigReader/LogBuilder.cs @@ -37,53 +37,58 @@ public static string BuildConfigLogMessage(string targetDirectory, ConfigResult $" MaxWidth: {maxWidth}"); } - if (config.ExcludeDirectories.Count != 0) + var excludeDirectories = config.ExcludeDirectories; + if (excludeDirectories != null && excludeDirectories.Count != 0) { Polyfill.AppendLine( builder, $""" ExcludeDirectories: - {string.Join("\r\n ", config.ExcludeDirectories)} + {string.Join("\r\n ", excludeDirectories)} """); } - if (config.ExcludeMarkdownDirectories.Count != 0) + var excludeMarkdownDirectories = config.ExcludeMarkdownDirectories; + if (excludeMarkdownDirectories != null && excludeMarkdownDirectories.Count != 0) { Polyfill.AppendLine( builder, $""" ExcludeMarkdownDirectories: - {string.Join("\r\n ", config.ExcludeMarkdownDirectories)} + {string.Join("\r\n ", excludeMarkdownDirectories)} """); } - if (config.ExcludeSnippetDirectories.Count != 0) + var excludeSnippetDirectories = config.ExcludeSnippetDirectories; + if (excludeSnippetDirectories != null && excludeSnippetDirectories.Count != 0) { Polyfill.AppendLine( builder, $""" ExcludeSnippetDirectories: - {string.Join("\r\n ", config.ExcludeSnippetDirectories)} + {string.Join("\r\n ", excludeSnippetDirectories)} """); } - if (config.TocExcludes.Count != 0) + var tocExcludes = config.TocExcludes; + if (tocExcludes != null && tocExcludes.Count != 0) { Polyfill.AppendLine( builder, $""" TocExcludes: - {string.Join("\r\n ", config.TocExcludes)} + {string.Join("\r\n ", tocExcludes)} """); } - if (config.UrlsAsSnippets.Count != 0) + var urlsAsSnippets = config.UrlsAsSnippets; + if (urlsAsSnippets != null && urlsAsSnippets.Count != 0) { Polyfill.AppendLine( builder, $""" UrlsAsSnippets: - {string.Join("\r\n ", config.UrlsAsSnippets)} + {string.Join("\r\n ", urlsAsSnippets)} """); } diff --git a/src/MarkdownSnippets.MsBuild/DocoTask.cs b/src/MarkdownSnippets.MsBuild/DocoTask.cs index 45cfcd72..c82e0ce8 100644 --- a/src/MarkdownSnippets.MsBuild/DocoTask.cs +++ b/src/MarkdownSnippets.MsBuild/DocoTask.cs @@ -78,7 +78,12 @@ public override bool Execute() try { var snippets = new List(); - snippets.AppendUrlsAsSnippets(configResult.UrlsAsSnippets).GetAwaiter().GetResult(); + var urlsAsSnippets = configResult.UrlsAsSnippets; + if (urlsAsSnippets != null) + { + snippets.AppendUrlsAsSnippets(urlsAsSnippets).GetAwaiter().GetResult(); + } + processor.AddSnippets(snippets); var snippetsInError = processor.Snippets.Where(_ => _.IsInError).ToList(); if (snippetsInError.Any()) diff --git a/src/MarkdownSnippets.Tool/Program.cs b/src/MarkdownSnippets.Tool/Program.cs index edc4be6c..2d4720d2 100644 --- a/src/MarkdownSnippets.Tool/Program.cs +++ b/src/MarkdownSnippets.Tool/Program.cs @@ -52,11 +52,12 @@ static async Task Inner(string targetDirectory, ConfigInput configInput) validateContent: configResult.ValidateContent, omitSnippetLinks: configResult.OmitSnippetLinks); - if (configResult.UrlsAsSnippets.Count != 0) + var urlsAsSnippets = configResult.UrlsAsSnippets; + if (urlsAsSnippets != null && urlsAsSnippets.Count != 0) { var snippets = new List(); - await snippets.AppendUrlsAsSnippets(configResult.UrlsAsSnippets); + await snippets.AppendUrlsAsSnippets(urlsAsSnippets); processor.AddSnippets(snippets); }