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

[C# target] Use Collections.EmptyList to prevent zero length allocations #4762

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions runtime/CSharp/src/Atn/ATN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public class ATN
private readonly PredictionContextCache contextCache = new PredictionContextCache();

[NotNull]
public DFA[] decisionToDFA = new DFA[0];
public DFA[] decisionToDFA = Collections.EmptyList<DFA>();

[NotNull]
public DFA[] modeToDFA = new DFA[0];
public DFA[] modeToDFA = Collections.EmptyList<DFA>();

protected internal readonly ConcurrentDictionary<int, int> LL1Table = new ConcurrentDictionary<int, int>();

Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Dfa/DFA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DFA(DecisionState atnStartState, int decision)
{
this.precedenceDfa = true;
DFAState precedenceState = new DFAState(new ATNConfigSet());
precedenceState.edges = new DFAState[0];
precedenceState.edges = Collections.EmptyList<DFAState>();
precedenceState.isAcceptState = false;
precedenceState.requiresFullContext = false;
this.s0 = precedenceState;
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/LexerInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LexerInterpreter: Lexer

[Obsolete("Use constructor with channelNames argument")]
public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, IEnumerable<string> modeNames, ATN atn, ICharStream input)
: this(grammarFileName, vocabulary, ruleNames, new string[0], modeNames, atn, input)
: this(grammarFileName, vocabulary, ruleNames, Collections.EmptyList<string>(), modeNames, atn, input)
{
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/CSharp/src/Recognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;

namespace Antlr4.Runtime
{
Expand Down Expand Up @@ -286,7 +287,7 @@ public virtual void RemoveErrorListener(IAntlrErrorListener<Symbol> listener)

public virtual void RemoveErrorListeners()
{
_listeners = new IAntlrErrorListener<Symbol>[0];
_listeners = Collections.EmptyList<IAntlrErrorListener<Symbol>>();
}

[NotNull]
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Sharpen/BitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Antlr4.Runtime.Sharpen

public class BitSet
{
private static readonly ulong[] EmptyBits = new ulong[0];
private static readonly ulong[] EmptyBits = Collections.EmptyList<ulong>();
private const int BitsPerElement = 8 * sizeof(ulong);

private ulong[] _data = EmptyBits;
Expand Down
15 changes: 11 additions & 4 deletions runtime/CSharp/src/Sharpen/Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace Antlr4.Runtime.Sharpen

internal static class Collections
{
public static T[] EmptyList<T>()
/// <remarks>
/// Available in .NET as Array.Empty but not to the net45 target.
/// See: https://learn.microsoft.com/dotnet/api/system.array.empty.
/// </remarks>
public static T[] EmptyList<T>()
{
return EmptyListImpl<T>.Instance;
}
Expand All @@ -31,10 +35,13 @@ public static ReadOnlyDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey k

private static class EmptyListImpl<T>
{
public static readonly T[] Instance = new T[0];
}
#pragma warning disable CA1825
// Provides a solution for CA1825.
public static readonly T[] Instance = new T[0];
#pragma warning restore CA1825
}

private static class EmptyMapImpl<TKey, TValue>
private static class EmptyMapImpl<TKey, TValue>
{
public static readonly ReadOnlyDictionary<TKey, TValue> Instance =
new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Vocabulary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Antlr4.Runtime
/// <author>Sam Harwell</author>
public class Vocabulary : IVocabulary
{
private static readonly string[] EmptyNames = new string[0];
private static readonly string[] EmptyNames = Collections.EmptyList<string>();

/// <summary>
/// Gets an empty
Expand Down
Loading