You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2021/11/22 05:58:54 UTC

[lucenenet] 01/02: BUG: Lucene.Net.Suggest.Jaspell.JaspellTernarySearchTree: Lazy initialize a J2N.Randomizer() instance so we hit both paths approximately evenly instead of one path when deleting nodes recursively.

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 1835a8ee710b0dc1efd22561cf29b39df79b5c9c
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Nov 21 07:48:46 2021 +0700

    BUG: Lucene.Net.Suggest.Jaspell.JaspellTernarySearchTree: Lazy initialize a J2N.Randomizer() instance so we hit both paths approximately evenly instead of one path when deleting nodes recursively.
---
 .../Suggest/Jaspell/JaspellTernarySearchTrie.cs                   | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/Lucene.Net.Suggest/Suggest/Jaspell/JaspellTernarySearchTrie.cs b/src/Lucene.Net.Suggest/Suggest/Jaspell/JaspellTernarySearchTrie.cs
index 164f97e..ffd844d 100644
--- a/src/Lucene.Net.Suggest/Suggest/Jaspell/JaspellTernarySearchTrie.cs
+++ b/src/Lucene.Net.Suggest/Suggest/Jaspell/JaspellTernarySearchTrie.cs
@@ -33,6 +33,7 @@ using System.Globalization;
 using System.IO;
 using System.IO.Compression;
 using System.Text;
+using System.Threading;
 using JCG = J2N.Collections.Generic;
 
 namespace Lucene.Net.Search.Suggest.Jaspell
@@ -362,6 +363,11 @@ namespace Lucene.Net.Search.Suggest.Jaspell
             }
         }
 
+        // LUCENENET: .NET has no Math.Random() method, so we need to lazy initialize an instance for this purpose.
+        // Note that the J2N.Randomizer.Next() method is threadsafe.
+        private static Random random;
+        private static Random Random => LazyInitializer.EnsureInitialized(ref random, () => new J2N.Randomizer());
+
         /// <summary>
         /// Recursively visits each node to be deleted.
         /// 
@@ -433,7 +439,7 @@ namespace Lucene.Net.Search.Suggest.Jaspell
             TSTNode targetNode;
             if (deltaHi == deltaLo)
             {
-                if (new Random(1).NextDouble() < 0.5)
+                if (Random.NextDouble() < 0.5)
                 {
                     deltaHi++;
                 }