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

Avoid using Linq as it generates GC each time. #296

Merged
merged 1 commit into from
Jul 7, 2022
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
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;
}
}
}