Skip to content

Commit

Permalink
Avoid using Linq as it generates GC each time. 1 Format call was gene…
Browse files Browse the repository at this point in the history
…rating around 2KB.
  • Loading branch information
karljj1 committed Jul 6, 2022
1 parent ed610a9 commit 90d6660
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/SmartFormat/Core/Parsing/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SmartFormat.Core.Settings;
using SmartFormat.Pooling.ObjectPools;
using SmartFormat.Pooling.SmartPools;
Expand Down Expand Up @@ -106,11 +105,12 @@ public void ReturnToPool()

ParentPlaceholder = null;
HasNested = false;

// Return and clear FormatItems we own
foreach (var item in Items.Where(i => ReferenceEquals(this, i.ParentFormatItem)))
foreach (var item in Items)
{
ReturnFormatItemToPool(item);
if (ReferenceEquals(this, item.ParentFormatItem))
ReturnFormatItemToPool(item);
}
Items.Clear();

Expand Down Expand Up @@ -237,9 +237,9 @@ public int IndexOf(char search)
public int IndexOf(char search, int start)
{
start = StartIndex + start;
foreach (var item in Items.Where(item => item.EndIndex >= start))
foreach (var item in Items)
{
if (item is not LiteralText literalItem) continue;
if (item.EndIndex < start || item is not LiteralText literalItem) continue;

if (start < literalItem.StartIndex) start = literalItem.StartIndex;
var literalIndex =
Expand Down Expand Up @@ -420,4 +420,4 @@ public void Dispose()
}

#endregion
}
}
20 changes: 11 additions & 9 deletions src/SmartFormat/Core/Sources/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright SmartFormat Project maintainers and contributors.
// Licensed under the MIT license.

using System.Linq;
using SmartFormat.Core.Parsing;
using SmartFormat.Core.Settings;

Expand All @@ -24,10 +23,7 @@ public abstract class Source : ISource, IInitializer
protected SmartSettings? _smartSettings;

/// <inheritdoc />
public virtual bool TryEvaluateSelector(ISelectorInfo selectorInfo)
{
return false;
}
public abstract bool TryEvaluateSelector(ISelectorInfo selectorInfo);

/// <inheritdoc />
public virtual void Initialize(SmartFormatter smartFormatter)
Expand All @@ -48,9 +44,15 @@ public virtual void Initialize(SmartFormatter smartFormatter)
/// </remarks>
private bool HasNullableOperator(ISelectorInfo selectorInfo)
{
return _smartSettings != null && selectorInfo.Placeholder != null &&
selectorInfo.Placeholder.Selectors.Any(s =>
s.OperatorLength > 1 && s.BaseString[s.OperatorStartIndex] == _smartSettings.Parser.NullableOperator);
if (_smartSettings != null && selectorInfo.Placeholder != null)
{
foreach (var s in selectorInfo.Placeholder.Selectors)
{
if (s.OperatorLength > 1 && s.BaseString[s.OperatorStartIndex] == _smartSettings.Parser.NullableOperator)
return true;
}
}
return false;
}

/// <summary>
Expand All @@ -77,4 +79,4 @@ protected virtual bool TrySetResultForNullableOperator(ISelectorInfo selectorInf

return false;
}
}
}
15 changes: 10 additions & 5 deletions src/SmartFormat/Extensions/ListFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Parsing;
Expand Down Expand Up @@ -300,9 +299,15 @@ private static void WriteSpacer(IFormattingInfo formattingInfo, Format spacer, o
/// </remarks>
private bool HasNullableOperator(IFormattingInfo formattingInfo)
{
return formattingInfo.Placeholder != null &&
formattingInfo.Placeholder.Selectors.Any(s =>
s.OperatorLength > 0 && s.BaseString[s.OperatorStartIndex] == _smartSettings.Parser.NullableOperator);
if (formattingInfo.Placeholder != null)
{
foreach (var s in formattingInfo.Placeholder.Selectors)
{
if (s.OperatorLength > 0 && s.BaseString[s.OperatorStartIndex] == _smartSettings.Parser.NullableOperator)
return true;
}
}
return false;
}

///<inheritdoc />
Expand All @@ -312,4 +317,4 @@ public void Initialize(SmartFormatter smartFormatter)
_smartSettings = smartFormatter.Settings;
_isInitialized = true;
}
}
}

0 comments on commit 90d6660

Please sign in to comment.