This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 867834b34068692c8adecff9fee690b9390a4ca5 Author: Shad Storhaug <[email protected]> AuthorDate: Sun Jun 28 21:49:07 2020 +0700 Lucene.Net.Util.Automaton: Fixed State class to initialize and trim more efficiently (#295, #261). Fixes the performance of Lucene.Net.Util.Automaton.TestBasicOperations::TestEmptyLanguageConcatenate(). --- Directory.Build.targets | 8 +++++++- src/Lucene.Net/Util/Automaton/State.cs | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index a0bdb4d..88f406b 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -19,8 +19,10 @@ --> <Project> + <!-- Features in .NET Standard and .NET Core only (no .NET Framework support) --> <PropertyGroup Condition=" $(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('netcoreapp')) "> <DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants> + <DefineConstants>$(DefineConstants);FEATURE_ARRAYEMPTY</DefineConstants> <DebugType>portable</DebugType> </PropertyGroup> @@ -66,7 +68,11 @@ </PropertyGroup> - <!-- Features in .NET Framework 4.5+ only --> + <!-- Features in .NET Framework 4.6+ only --> + <PropertyGroup Condition="$(TargetFramework.StartsWith('net46')) Or $(TargetFramework.StartsWith('net47')) Or $(TargetFramework.StartsWith('net48'))"> + <DefineConstants>$(DefineConstants);FEATURE_ARRAYEMPTY</DefineConstants> + </PropertyGroup> + <PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))"> <DefineConstants>$(DefineConstants);FEATURE_SERIALIZABLE_EXCEPTIONS</DefineConstants> diff --git a/src/Lucene.Net/Util/Automaton/State.cs b/src/Lucene.Net/Util/Automaton/State.cs index 587c403..1df6c98 100644 --- a/src/Lucene.Net/Util/Automaton/State.cs +++ b/src/Lucene.Net/Util/Automaton/State.cs @@ -48,11 +48,15 @@ namespace Lucene.Net.Util.Automaton [SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")] public Transition[] TransitionsArray { - get { return transitionsArray; } + get => transitionsArray; // LUCENENET NOTE: Setter removed because it is apparently not in use outside of this class } - private Transition[] transitionsArray; - internal int numTransitions; // LUCENENET NOTE: Made internal because we already have a public property for access +#if FEATURE_ARRAYEMPTY + private Transition[] transitionsArray = Array.Empty<Transition>(); +#else + private Transition[] transitionsArray = new Transition[0]; +#endif + internal int numTransitions = 0;// LUCENENET NOTE: Made internal because we already have a public property for access internal int number; @@ -64,7 +68,7 @@ namespace Lucene.Net.Util.Automaton /// </summary> public State() { - ResetTransitions(); + //ResetTransitions(); // LUCENENET: Let class initializer set these id = next_id++; } @@ -73,7 +77,11 @@ namespace Lucene.Net.Util.Automaton /// </summary> internal void ResetTransitions() { +#if FEATURE_ARRAYEMPTY + transitionsArray = Array.Empty<Transition>(); +#else transitionsArray = new Transition[0]; +#endif numTransitions = 0; } @@ -242,9 +250,7 @@ namespace Lucene.Net.Util.Automaton { if (numTransitions < transitionsArray.Length) { - Transition[] newArray = new Transition[numTransitions]; - Array.Copy(transitionsArray, 0, newArray, 0, numTransitions); - transitionsArray = newArray; + Array.Resize(ref transitionsArray, numTransitions); // LUCENENET: Resize rather than copy } }
