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/10/25 17:34:02 UTC

[lucenenet] branch master updated: Revert "Lucene.Net.Support.WeakDictionary: Changed backing collection from J2N.Collections.Generic.Dictionary to ConcurrentDictionary to allow reap to function while doing a forward iteration through the collection."

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


The following commit(s) were added to refs/heads/master by this push:
     new f68cbb3  Revert "Lucene.Net.Support.WeakDictionary: Changed backing collection from J2N.Collections.Generic.Dictionary<TKey, TValue> to ConcurrentDictionary<TKey, TValue> to allow reap to function while doing a forward iteration through the collection."
f68cbb3 is described below

commit f68cbb3ef79e0635397fcc367267ee252e2264c1
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Oct 25 21:59:56 2021 +0700

    Revert "Lucene.Net.Support.WeakDictionary: Changed backing collection from J2N.Collections.Generic.Dictionary<TKey, TValue> to ConcurrentDictionary<TKey, TValue> to allow reap to function while doing a forward iteration through the collection."
    
    This reverts commit ddfb7b2108f0d72fd03c563c4bfc2f3479cdf91f.
    
    This was causing occasional test failures of Lucene.Net.Support.TestWeakDictionary::C_TestMemLeakage().
---
 src/Lucene.Net/Support/WeakDictionary.cs | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/Lucene.Net/Support/WeakDictionary.cs b/src/Lucene.Net/Support/WeakDictionary.cs
index bc2667b..1633499 100644
--- a/src/Lucene.Net/Support/WeakDictionary.cs
+++ b/src/Lucene.Net/Support/WeakDictionary.cs
@@ -22,7 +22,6 @@
 #if !FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR
 using System;
 using System.Collections;
-using System.Collections.Concurrent;
 using System.Collections.Generic;
 using JCG = J2N.Collections.Generic;
 
@@ -50,9 +49,7 @@ namespace Lucene.Net.Support
 
         private WeakDictionary(int initialCapacity, IEnumerable<KeyValuePair<TKey, TValue>> otherDict)
         {
-            // LUCENENET specific - using ConcurrentDictionary here not because we need concurrency, but because
-            // it allows removing items while iterating forward.
-            _hm = new ConcurrentDictionary<WeakKey<TKey>, TValue>(concurrencyLevel: 10, capacity: initialCapacity);
+            _hm = new JCG.Dictionary<WeakKey<TKey>, TValue>(initialCapacity);
             foreach (var kvp in otherDict)
             {
                 _hm.Add(new WeakKey<TKey>(kvp.Key), kvp.Value);
@@ -66,11 +63,19 @@ namespace Lucene.Net.Support
         private void Clean()
         {
             if (_hm.Count == 0) return;
+            var newHm = new JCG.Dictionary<WeakKey<TKey>, TValue>(_hm.Count);
             foreach (var kvp in _hm)
             {
-                if (!kvp.Key.TryGetTarget(out TKey _))
-                    _hm.Remove(kvp.Key);
+                if (kvp.Key.TryGetTarget(out TKey _))
+                {
+                    // LUCENENET: There is a tiny chance that a call to remove the item
+                    // from the dictionary can happen before this line is executed. Therefore,
+                    // just discard the reference and add it as is, even if it is no longer valid
+                    // in this edge case. It is far more efficient to re-use the same instances, anyway.
+                    newHm.Add(kvp.Key, kvp.Value);
+                }
             }
+            _hm = newHm;
         }
 
         private void CleanIfNeeded()