Domanda

Ho giocato con le raccolte e il threading e mi sono imbattuto negli eleganti metodi di estensione che le persone hanno creato per facilitare l'uso di ReaderWriterLockSlim consentendo il modello IDisposable.

Tuttavia, credo di aver capito che qualcosa nell'implementazione è un killer delle prestazioni. Mi rendo conto che i metodi di estensione non dovrebbero avere un impatto reale sulle prestazioni, quindi sono rimasto supponendo che qualcosa nell'implementazione sia la causa ... la quantità di strutture monouso create / raccolte?

Ecco un po 'di codice di prova:

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

Ecco un risultato tipico:

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

Come puoi vedere, l'uso delle estensioni rende effettivamente le prestazioni PEGGIORI rispetto al semplice utilizzo di lock (monitor).

È stato utile?

Soluzione

Sembra che sia il prezzo di creare un'istanza di milioni di strutture e il tocco extra di invocazioni.

Vorrei dire che ReaderWriterLockSlim viene utilizzato in modo improprio in questo esempio, un blocco è abbastanza buono in questo caso e il vantaggio prestazionale ottenuto con ReaderWriterLockSlim è trascurabile rispetto al prezzo di spiegare questi concetti a Junior sviluppatori.

Ottieni un enorme vantaggio con i blocchi in stile scrittore del lettore quando ci vuole una quantità non trascurabile di tempo per eseguire letture e scritture. La spinta sarà maggiore quando avrai un sistema prevalentemente basato su lettura.

Prova a inserire un Thread.Sleep (1) mentre vengono acquisiti i blocchi per vedere quanta differenza fa.

Vedi questo 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

Nel mio benchmarking non noto davvero molta differenza tra i due stili, mi sentirei a mio agio nell'usarlo a condizione che avesse una protezione del finalizzatore nel caso in cui le persone dimenticano di smaltire ....

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();
        }
    }
}

Altri suggerimenti

Il codice sembra utilizzare una struttura per evitare il sovraccarico di creazione di oggetti, ma non prende le altre misure necessarie per mantenerlo leggero. Credo che racchiuda il valore restituito da ReadLock e, in tal caso, nega l'intero vantaggio della struttura. Ciò dovrebbe risolvere tutti i problemi ed eseguire altrettanto bene, oltre a non passare attraverso l'interfaccia IDisposable .

Modifica: benchmark richiesti. Questi risultati sono normalizzati quindi il metodo manuale (chiama Enter / ExitReadLock e Enter / ExitWriteLock in linea con il codice protetto) hanno un valore temporale di 1.00. Il metodo originale è lento perché alloca oggetti sull'heap che il metodo manuale non ha. Ho risolto questo problema e in modalità di rilascio anche l'overhead di chiamata del metodo di estensione scompare lasciandolo identico alla velocità del metodo manuale.

Build di debug:

Manual:              1.00
Original Extensions: 1.62
My Extensions:       1.24

Versione build:

Manual:              1.00
Original Extensions: 1.51
My Extensions:       1.00

Il mio codice:

internal static class RWLSExtension
{
    public static ReadLockHelper ReadLock(this ReaderWriterLockSlim readerWriterLock)
    {
        return new ReadLockHelper(readerWriterLock);
    }

    public static UpgradeableReadLockHelper UpgradableReadLock(this ReaderWriterLockSlim readerWriterLock)
    {
        return new UpgradeableReadLockHelper(readerWriterLock);
    }

    public static WriteLockHelper WriteLock(this ReaderWriterLockSlim readerWriterLock)
    {
        return new WriteLockHelper(readerWriterLock);
    }

    public struct ReadLockHelper : IDisposable
    {
        private readonly ReaderWriterLockSlim readerWriterLock;

        public ReadLockHelper(ReaderWriterLockSlim readerWriterLock)
        {
            readerWriterLock.EnterReadLock();
            this.readerWriterLock = readerWriterLock;
        }

        public void Dispose()
        {
            this.readerWriterLock.ExitReadLock();
        }
    }

    public struct UpgradeableReadLockHelper : IDisposable
    {
        private readonly ReaderWriterLockSlim readerWriterLock;

        public UpgradeableReadLockHelper(ReaderWriterLockSlim readerWriterLock)
        {
            readerWriterLock.EnterUpgradeableReadLock();
            this.readerWriterLock = readerWriterLock;
        }

        public void Dispose()
        {
            this.readerWriterLock.ExitUpgradeableReadLock();
        }
    }

    public struct WriteLockHelper : IDisposable
    {
        private readonly ReaderWriterLockSlim readerWriterLock;

        public WriteLockHelper(ReaderWriterLockSlim readerWriterLock)
        {
            readerWriterLock.EnterWriteLock();
            this.readerWriterLock = readerWriterLock;
        }

        public void Dispose()
        {
            this.readerWriterLock.ExitWriteLock();
        }
    }
}

La mia ipotesi (dovresti profilare per verificare) è che il calo delle prestazioni non è dovuto alla creazione delle istanze usa e getta (dovrebbero essere abbastanza economiche, essendo strutture). Invece mi aspetto che provenga dalla creazione dei delegati di Action. Potresti provare a cambiare l'implementazione della tua struttura usa e getta per memorizzare l'istanza di ReaderWriterLockSlim invece di creare un delegato Action.

Modifica: il post di @ 280Z28 conferma che è l'allocazione di heap dei delegati all'azione che sta causando il rallentamento.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top