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 2022/01/25 21:32:27 UTC

[lucenenet] 01/02: PERFORMANCE: Lucene.Net.Support.IO.StreamExtensions::Read(Stream, ByteBuffer, long): Use a lock per stream so instances of NIOFSDirectory can run independently on separate threads without competing for a lock.

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 5a235309358bcd04e1095c14969100229f195c03
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Jan 25 21:38:49 2022 +0700

    PERFORMANCE: Lucene.Net.Support.IO.StreamExtensions::Read(Stream, ByteBuffer, long): Use a lock per stream so instances of NIOFSDirectory can run independently on separate threads without competing for a lock.
---
 src/Lucene.Net/Support/IO/StreamExtensions.cs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/Lucene.Net/Support/IO/StreamExtensions.cs b/src/Lucene.Net/Support/IO/StreamExtensions.cs
index e74c3d7..abb8c86 100644
--- a/src/Lucene.Net/Support/IO/StreamExtensions.cs
+++ b/src/Lucene.Net/Support/IO/StreamExtensions.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Support.Threading;
 using System;
 using System.IO;
+using System.Runtime.CompilerServices;
 
 namespace Lucene.Net.Support.IO
 {
@@ -35,8 +36,7 @@ namespace Lucene.Net.Support.IO
     /// </summary>
     internal static class StreamExtensions
     {
-        private static readonly object readLock = new object();
-
+        private static readonly ConditionalWeakTable<Stream, object> lockCache = new ConditionalWeakTable<Stream, object>();
 
         /// <summary>
         /// Reads a sequence of bytes from a <see cref="Stream"/> to the given <see cref="ByteBuffer"/>, starting at the given position.
@@ -82,6 +82,7 @@ namespace Lucene.Net.Support.IO
                 return 0;
 
             int read = 0;
+            object readLock = lockCache.GetOrCreateValue(stream);
             UninterruptableMonitor.Enter(readLock);
             try
             {