Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 193003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:28:25+00:00 2026-05-11T16:28:25+00:00

I’ve been playing with collections and threading and came across the nifty extension methods

  • 0

I’ve been playing with collections and threading and came across the nifty extension methods people have created to ease the use of ReaderWriterLockSlim by allowing the IDisposable pattern.

However, I believe I have come to realize that something in the implementation is a performance killer. I realize that extension methods are not supposed to really impact performance, so I am left assuming that something in the implementation is the cause… the amount of Disposable structs created/collected?

Here’s some test code:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

namespace LockPlay {

    static class RWLSExtension {
        struct Disposable : IDisposable {
            readonly Action _action;
            public Disposable(Action action) {
                _action = action;
            }
            public void Dispose() {
                _action();
            }
        } // end struct
        public static IDisposable ReadLock(this ReaderWriterLockSlim rwls) {
            rwls.EnterReadLock();
            return new Disposable(rwls.ExitReadLock);
        }
        public static IDisposable UpgradableReadLock(this ReaderWriterLockSlim rwls) {
            rwls.EnterUpgradeableReadLock();
            return new Disposable(rwls.ExitUpgradeableReadLock);
        }
        public static IDisposable WriteLock(this ReaderWriterLockSlim rwls) {
            rwls.EnterWriteLock();
            return new Disposable(rwls.ExitWriteLock);
        }
    } // end class

    class Program {

        class MonitorList<T> : List<T>, IList<T> {
            object _syncLock = new object();
            public MonitorList(IEnumerable<T> collection) : base(collection) { }
            T IList<T>.this[int index] {
                get {
                    lock(_syncLock)
                        return base[index];
                }
                set {
                    lock(_syncLock)
                        base[index] = value;
                }
            }
        } // end class

        class RWLSList<T> : List<T>, IList<T> {
            ReaderWriterLockSlim _rwls = new ReaderWriterLockSlim();
            public RWLSList(IEnumerable<T> collection) : base(collection) { }
            T IList<T>.this[int index] {
                get {
                    try {
                        _rwls.EnterReadLock();
                        return base[index];
                    } finally {
                        _rwls.ExitReadLock();
                    }
                }
                set {
                    try {
                        _rwls.EnterWriteLock();
                        base[index] = value;
                    } finally {
                        _rwls.ExitWriteLock();
                    }
                }
            }
        } // end class

        class RWLSExtList<T> : List<T>, IList<T> {
            ReaderWriterLockSlim _rwls = new ReaderWriterLockSlim();
            public RWLSExtList(IEnumerable<T> collection) : base(collection) { }
            T IList<T>.this[int index] {
                get {
                    using(_rwls.ReadLock())
                        return base[index];
                }
                set {
                    using(_rwls.WriteLock())
                        base[index] = value;
                }
            }
        } // end class

        static void Main(string[] args) {
            const int ITERATIONS = 100;
            const int WORK = 10000;
            const int WRITE_THREADS = 4;
            const int READ_THREADS = WRITE_THREADS * 3;

            // create data - first List is for comparison only... not thread safe
            int[] copy = new int[WORK];
            IList<int>[] l = { new List<int>(copy), new MonitorList<int>(copy), new RWLSList<int>(copy), new RWLSExtList<int>(copy) };

            // test each list
            Thread[] writeThreads = new Thread[WRITE_THREADS];
            Thread[] readThreads = new Thread[READ_THREADS];
            foreach(var list in l) {
                Stopwatch sw = Stopwatch.StartNew();
                for(int k=0; k < ITERATIONS; k++) {
                    for(int i = 0; i < writeThreads.Length; i++) {
                        writeThreads[i] = new Thread(p => {
                            IList<int> il = p as IList<int>;
                            int c = il.Count;
                            for(int j = 0; j < c; j++) {
                                il[j] = j;
                            }
                        });
                        writeThreads[i].Start(list);
                    }
                    for(int i = 0; i < readThreads.Length; i++) {
                        readThreads[i] = new Thread(p => {
                            IList<int> il = p as IList<int>;
                            int c = il.Count;
                            for(int j = 0; j < c; j++) {
                                int temp = il[j];
                            }
                        });
                        readThreads[i].Start(list);
                    }
                    for(int i = 0; i < readThreads.Length; i++)
                        readThreads[i].Join();
                    for(int i = 0; i < writeThreads.Length; i++)
                        writeThreads[i].Join();
                };
                sw.Stop();
                Console.WriteLine("time: {0} class: {1}", sw.Elapsed, list.GetType());
            }
            Console.WriteLine("DONE");
            Console.ReadLine();
        }
    } // end class
} // end namespace

Here’s a typical result:

time: 00:00:03.0965242 class: System.Collections.Generic.List`1[System.Int32]
time: 00:00:11.9194573 class: LockPlay.Program+MonitorList`1[System.Int32]
time: 00:00:08.9510258 class: LockPlay.Program+RWLSList`1[System.Int32]
time: 00:00:16.9888435 class: LockPlay.Program+RWLSExtList`1[System.Int32]
DONE

As you can see, using the extensions actually makes the performance WORSE than just using lock (monitor).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:28:25+00:00Added an answer on May 11, 2026 at 4:28 pm

    Looks like its the price of instantiating millions of structs and the extra bit of invocations.

    I would go as far as to say that the ReaderWriterLockSlim is being misused in this sample, a lock is good enough in this case and the performance edge you get with the ReaderWriterLockSlim is negligible compared to the price of explaining these concepts to junior devs.

    You get a huge advantage with reader writer style locks when it takes a non-negligable amount of time to perform reads and writes. The boost will be biggest when you have a predominantly read based system.

    Try inserting a Thread.Sleep(1) while the locks are acquired to see how huge a difference it makes.

    See this benchmark:

    Time for Test.SynchronizedList`1[System.Int32] Time Elapsed 12310 ms
    Time for Test.ReaderWriterLockedList`1[System.Int32] Time Elapsed 547 ms
    Time for Test.ManualReaderWriterLockedList`1[System.Int32] Time Elapsed 566 ms
    

    In my benchmarking I do not really notice much of a difference between the two styles, I would feel comfortable using it provided it had some finalizer protection in case people forget to dispose ….

    using System.Threading;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System;
    using System.Linq;
    
    namespace Test {
    
        static class RWLSExtension {
            struct Disposable : IDisposable {
                readonly Action _action;
                public Disposable(Action action) {
                    _action = action;
                }
                public void Dispose() {
                    _action();
                }
            }
    
            public static IDisposable ReadLock(this ReaderWriterLockSlim rwls) {
                rwls.EnterReadLock();
                return new Disposable(rwls.ExitReadLock);
            }
            public static IDisposable UpgradableReadLock(this ReaderWriterLockSlim rwls) {
                rwls.EnterUpgradeableReadLock();
                return new Disposable(rwls.ExitUpgradeableReadLock);
            }
            public static IDisposable WriteLock(this ReaderWriterLockSlim rwls) {
                rwls.EnterWriteLock();
                return new Disposable(rwls.ExitWriteLock);
            }
        }
    
        class SlowList<T> {
    
            List<T> baseList = new List<T>();
    
            public void AddRange(IEnumerable<T> items) {
                baseList.AddRange(items);
            }
    
            public virtual T this[int index] {
                get {
                    Thread.Sleep(1);
                    return baseList[index];
                }
                set {
                    baseList[index] = value;
                    Thread.Sleep(1);
                }
            }
        }
    
        class SynchronizedList<T> : SlowList<T> {
    
            object sync = new object();
    
            public override T this[int index] {
                get {
                    lock (sync) {
                        return base[index];
                    }
    
                }
                set {
                    lock (sync) {
                        base[index] = value;
                    }
                }
            }
        }
    
    
        class ManualReaderWriterLockedList<T> : SlowList<T> {
    
            ReaderWriterLockSlim slimLock = new ReaderWriterLockSlim();
    
            public override T this[int index] {
                get {
                    T item;
                    try {
                        slimLock.EnterReadLock();
                        item = base[index];
                    } finally {
                        slimLock.ExitReadLock();
                    }
                    return item;
    
                }
                set {
                    try {
                        slimLock.EnterWriteLock();
                        base[index] = value;
                    } finally {
                        slimLock.ExitWriteLock();
                    }
                }
            }
        }
    
        class ReaderWriterLockedList<T> : SlowList<T> {
    
            ReaderWriterLockSlim slimLock = new ReaderWriterLockSlim();
    
            public override T this[int index] {
                get {
                    using (slimLock.ReadLock()) {
                        return base[index];
                    }
                }
                set {
                    using (slimLock.WriteLock()) {
                        base[index] = value;
                    }
                }
            }
        }
    
    
        class Program {
    
    
            private static void Repeat(int times, int asyncThreads, Action action) {
                if (asyncThreads > 0) {
    
                    var threads = new List<Thread>();
    
                    for (int i = 0; i < asyncThreads; i++) {
    
                        int iterations = times / asyncThreads;
                        if (i == 0) {
                            iterations += times % asyncThreads;
                        }
    
                        Thread thread = new Thread(new ThreadStart(() => Repeat(iterations, 0, action)));
                        thread.Start();
                        threads.Add(thread);
                    }
    
                    foreach (var thread in threads) {
                        thread.Join();
                    }
    
                } else {
                    for (int i = 0; i < times; i++) {
                        action();
                    }
                }
            }
    
            static void TimeAction(string description, Action func) {
                var watch = new Stopwatch();
                watch.Start();
                func();
                watch.Stop();
                Console.Write(description);
                Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds);
            }
    
            static void Main(string[] args) {
    
                int threadCount = 40;
                int iterations = 200;
                int readToWriteRatio = 60;
    
                var baseList = Enumerable.Range(0, 10000).ToList();
    
                List<SlowList<int>> lists = new List<SlowList<int>>() {
                    new SynchronizedList<int>() ,
                    new ReaderWriterLockedList<int>(),
                    new ManualReaderWriterLockedList<int>()
                };
    
                foreach (var list in lists) {
                    list.AddRange(baseList);
                }
    
    
                foreach (var list in lists) {
                    TimeAction("Time for " + list.GetType().ToString(), () =>
                    {
                        Repeat(iterations, threadCount, () =>
                        {
                            list[100] = 99;
                            for (int i = 0; i < readToWriteRatio; i++) {
                                int ignore = list[i];
                            }
                        });
                    });
                }
    
    
    
                Console.WriteLine("DONE");
                Console.ReadLine();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.