Skip to content

Commit 28f604c

Browse files
authored
Minor perf tweaks (#1851)
* use compiled regex in CSharpDocs, check if replacement needed * proper constant stack allocation size in ConversionUtilities * better replace behavior in JsonReferenceVisitorBase
1 parent d899346 commit 28f604c

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/NJsonSchema/ConversionUtilities.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//-----------------------------------------------------------------------
88

99
using System.Globalization;
10-
using System.Linq;
1110
using System.Text;
1211
using System.Text.RegularExpressions;
1312
using System.Xml.Linq;
@@ -93,7 +92,7 @@ private static string ConvertToCamelCase(string input, bool firstCharacterMustBe
9392
private static string DoFullCamelCaseConversion(string input, bool firstCharacterMustBeAlpha, CamelCaseMode mode)
9493
{
9594
var capacity = input.Length + (firstCharacterMustBeAlpha ? 1 : 0);
96-
var buffer = capacity < 2 ? stackalloc char[capacity] : new char[capacity];
95+
var buffer = capacity <= 256 ? stackalloc char[256] : new char[capacity];
9796

9897
var sb = new ValueStringBuilder(buffer);
9998

@@ -363,20 +362,29 @@ private static void AddPrefixToBeginningOfNonEmptyLines(string input, string tab
363362
}
364363
}
365364

365+
private static readonly char [] _cSharpDocLineBreakChars = ['\r', '\n'];
366+
private static readonly Lazy<Regex> _cSharpDocLineBreakRegex = new(static () => new Regex("^( *)/// ", RegexOptions.Multiline | RegexOptions.Compiled));
367+
366368
/// <summary>Converts all line breaks in a string into '\n' and removes white spaces.</summary>
367369
/// <param name="input">The input.</param>
368370
/// <param name="tabCount">The tab count.</param>
369371
/// <returns>The output.</returns>
370372
public static string ConvertCSharpDocs(string input, int tabCount)
371373
{
372-
input = input?
373-
.Replace("\r", string.Empty)
374-
.Replace("\n", "\n" + string.Join("", Enumerable.Repeat(" ", tabCount)) + "/// ")
375-
?? string.Empty;
374+
input ??= "";
375+
376+
var needsCleanup = input.IndexOfAny(_cSharpDocLineBreakChars) != -1;
377+
378+
if (needsCleanup)
379+
{
380+
input = input
381+
.Replace("\r", string.Empty)
382+
.Replace("\n", "\n" + CreateTabString(tabCount) + "/// ");
383+
}
376384

377385
// TODO: Support more markdown features here
378386
var xml = new XText(input).ToString();
379-
return Regex.Replace(xml, @"^( *)/// ", m => m.Groups[1] + "/// <br/>", RegexOptions.Multiline);
387+
return _cSharpDocLineBreakRegex.Value.Replace(xml, static m => m.Groups[1] + "/// <br/>");
380388
}
381389

382390
private static string CreateTabString(int tabCount)

src/NJsonSchema/Visitors/JsonReferenceVisitorBase.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,25 @@ protected virtual void Visit(object obj, string path, string? typeNameHint, ISet
243243

244244
private static void ReplaceOrDelete<T>(ObservableCollection<T> collection, int index, T obj)
245245
{
246-
collection.RemoveAt(index);
247-
if (obj != null)
246+
if (obj is not null)
248247
{
249-
collection.Insert(index, obj);
248+
collection[index] = obj;
249+
}
250+
else
251+
{
252+
collection.RemoveAt(index);
250253
}
251254
}
252255

253256
private static void ReplaceOrDelete(IList collection, int index, object obj)
254257
{
255-
collection.RemoveAt(index);
256-
if (obj != null)
258+
if (obj is not null)
259+
{
260+
collection[index] = obj;
261+
}
262+
else
257263
{
258-
collection.Insert(index, obj);
264+
collection.RemoveAt(index);
259265
}
260266
}
261267
}

0 commit comments

Comments
 (0)