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/09/16 07:44:26 UTC
[lucenenet] 02/03: 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
commit ddfb7b2108f0d72fd03c563c4bfc2f3479cdf91f
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Fri Aug 13 04:40:20 2021 +0700
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.
---
src/Lucene.Net/Support/WeakDictionary.cs | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/src/Lucene.Net/Support/WeakDictionary.cs b/src/Lucene.Net/Support/WeakDictionary.cs
index 1633499..bc2667b 100644
--- a/src/Lucene.Net/Support/WeakDictionary.cs
+++ b/src/Lucene.Net/Support/WeakDictionary.cs
@@ -22,6 +22,7 @@
#if !FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR
using System;
using System.Collections;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using JCG = J2N.Collections.Generic;
@@ -49,7 +50,9 @@ namespace Lucene.Net.Support
private WeakDictionary(int initialCapacity, IEnumerable<KeyValuePair<TKey, TValue>> otherDict)
{
- _hm = new JCG.Dictionary<WeakKey<TKey>, TValue>(initialCapacity);
+ // 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);
foreach (var kvp in otherDict)
{
_hm.Add(new WeakKey<TKey>(kvp.Key), kvp.Value);
@@ -63,19 +66,11 @@ 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 _))
- {
- // 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);
- }
+ if (!kvp.Key.TryGetTarget(out TKey _))
+ _hm.Remove(kvp.Key);
}
- _hm = newHm;
}
private void CleanIfNeeded()