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 2020/07/29 17:11:07 UTC
[lucenenet] 02/14: PERFORMANCE:
Lucene.Net.Facet.Taxonomy.WriterCache.NameInt32CacheLRU: Changed from
Dictionary to ConcurrentDictionary so we can delete items from the cache
while forward iterating through it. (see #261)
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 4bdb4e3f733901cfa6bc84fe13565f18b4e999a4
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Thu Jul 23 10:31:44 2020 +0700
PERFORMANCE: Lucene.Net.Facet.Taxonomy.WriterCache.NameInt32CacheLRU: Changed from Dictionary to ConcurrentDictionary so we can delete items from the cache while forward iterating through it. (see #261)
---
.../Taxonomy/WriterCache/NameIntCacheLRU.cs | 24 +++++++++++++---------
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/NameIntCacheLRU.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/NameIntCacheLRU.cs
index c089885..22baa62 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/NameIntCacheLRU.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/NameIntCacheLRU.cs
@@ -1,6 +1,6 @@
using J2N.Collections.Concurrent;
+using System.Collections.Concurrent;
using System.Collections.Generic;
-using System.Linq;
namespace Lucene.Net.Facet.Taxonomy.WriterCache
{
@@ -39,6 +39,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
internal long nMisses = 0; // for debug
internal long nHits = 0; // for debug
private readonly int maxCacheSize;
+ private readonly object syncLock = new object(); // LUCENENET specific so we don't lock this
internal NameInt32CacheLRU(int limit)
{
@@ -60,11 +61,13 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
{
if (maxSize < int.MaxValue)
{
- cache = new LurchTable<object, int>(1000, LurchTableOrder.Access); //for LRU
+ cache = new LurchTable<object, int>(capacity: 1000, ordering: LurchTableOrder.Access); //for LRU
}
else
{
- cache = new Dictionary<object, int>(1000); //no need for LRU
+ // LUCENENET specific - we use ConcurrentDictionary here because it supports deleting while
+ // iterating through the collection, but Dictionary does not.
+ cache = new ConcurrentDictionary<object, int>(concurrencyLevel: 3, capacity: 1000); //no need for LRU
}
}
@@ -149,7 +152,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
return false;
}
- lock (this)
+ lock (syncLock)
{
// Double-check that another thread didn't beat us to the operation
n = cache.Count - (2 * maxCacheSize) / 3;
@@ -159,13 +162,14 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
}
//System.Diagnostics.Debug.WriteLine("Removing cache entries in MakeRoomLRU");
-
- // LUCENENET: Loop in reverse so we can safely delete
- // a range of items (0 - n) without a
- // "Collection was modified" conflict
- for (int i = n - 1; i >= 0; i--)
+ using (var it = cache.GetEnumerator())
{
- cache.Remove(cache.Keys.ElementAt(i));
+ int i = 0;
+ while (i < n && it.MoveNext())
+ {
+ cache.Remove(it.Current.Key);
+ i++;
+ }
}
}
return true;