Faster MREW ?

Home Forums kbmMW Faster MREW ?

Viewing 3 reply threads
  • Author
    Posts
    • #55489
      Babis Michael
      Participant

      Hello Kim,

      Take a look at the 4 Parts of this article: (front page has all the 4 parts)

      https://www.thedelphigeek.com/2020/11/readers-writer-lock-part-1-why.html

    • #55490
      kimbomadsen
      Keymaster

      Hi,

      Yes. I did notice it when part 1 was published. I completely follow the need for a faster MREW than the original ones.

      kbmMW can most likely easily use the new one, although kbmMW’s own has more features, like re-entrant locking from same thread (both read and write), lock escalation from same thread, locking bias towards reader or writer and not least the ability to be given information about which threads holds which locks.

      I have done some tests with the new TLightweightMREW, and TkbmMWMREWLock is significantly faster in my own benchmark. (Running 20 read threads and 20 write threads all running on an AMD 1950x ThreadRipper.

      Source is here: https://drive.google.com/file/d/1M-WHpOLeozt7KhzgPZeVh3mzl8GXd31Q/view?usp=sharing

      Read thread 3 finished.
      Read thread 19 finished.
      Read thread 12 finished.
      Read thread 10 finished.
      Read thread 14 finished.
      Read thread 5 finished.
      Read thread 4 finished.
      Read thread 20 finished.
      Read thread 18 finished.
      Read thread 8 finished.
      Read thread 13 finished.
      Read thread 2 finished.
      Read thread 17 finished.
      Read thread 15 finished.
      Read thread 11 finished.
      Read thread 16 finished.
      Read thread 9 finished.
      Write thread 18 finished.
      Read thread 6 finished.
      Read thread 7 finished.
      Read thread 1 finished.
      Write thread 13 finished.
      Write thread 9 finished.
      Write thread 14 finished.
      Write thread 8 finished.
      Write thread 20 finished.
      Write thread 6 finished.
      Write thread 3 finished.
      Write thread 5 finished.
      Write thread 19 finished.
      Write thread 11 finished.
      Write thread 4 finished.
      Write thread 7 finished.
      Write thread 15 finished.
      Write thread 17 finished.
      Write thread 1 finished.
      Write thread 16 finished.
      Write thread 12 finished.
      Write thread 2 finished.
      Write thread 10 finished.
      Using: TLighterMREW
      Time: 2906
      ——————————————–
      Read thread 3 finished.
      Read thread 20 finished.
      Read thread 15 finished.
      Read thread 18 finished.
      Read thread 10 finished.
      Read thread 11 finished.
      Read thread 13 finished.
      Read thread 14 finished.
      Read thread 16 finished.
      Read thread 19 finished.
      Read thread 6 finished.
      Read thread 9 finished.
      Read thread 8 finished.
      Read thread 5 finished.
      Read thread 17 finished.
      Read thread 7 finished.
      Read thread 12 finished.
      Read thread 2 finished.
      Read thread 4 finished.
      Read thread 1 finished.
      Write thread 18 finished.
      Write thread 11 finished.
      Write thread 1 finished.
      Write thread 16 finished.
      Write thread 12 finished.
      Write thread 9 finished.
      Write thread 4 finished.
      Write thread 13 finished.
      Write thread 7 finished.
      Write thread 20 finished.
      Write thread 19 finished.
      Write thread 6 finished.
      Write thread 3 finished.
      Write thread 2 finished.
      Write thread 17 finished.
      Write thread 8 finished.
      Write thread 14 finished.
      Write thread 5 finished.
      Write thread 15 finished.
      Write thread 10 finished.
      Using: TkbmMWLock
      Time: 1703
      ——————————————–

      I have also benchmarked with the pub/sub benchmark, and it shows some weird numbers imo. The number of reads and writes per second is somewhat slower than TLightweightMREW, but the actual throughput is significantly (mysteriously) higher.
      I’m suspecting the lock escalation methods to play in kbmMW’s favour due to the use of a thread pool. The same thread will many times be reused for doing something, and kbmMW’s lock recognizes that and often lets it thru faster than it otherwise would. I’m unsure about how TTask queues up jobs on the threads, but that could be an explanation of the crazy higher number.

      Benchmarking TLightweightMREW
      Read: 14266776/sec, write: 14492698/sec
      Number of Subscribe/Unsubscribe calls: 20
      Number of Notify calls: 4762
      Notify calls per thread: 309 276 211 248 218 243 232 237 249 261 209 219 234 225 215 231 267 239 220 219
      Maximum level of Notify concurrency: 20

      Benchmarking TkbmMWMREW
      Read: 8304315/sec, write: 12708076/sec
      Number of Subscribe/Unsubscribe calls: 20
      Number of Notify calls: 183030409
      Notify calls per thread: 9295772 8852668 9305011 9786472 9147596 9265295 9075588 8160320 10053970 8731598 8750825 9596873 9335931 9411405 9125036 8741504 9752100 9170672 8468713 9003060
      Maximum level of Notify concurrency: 20

      Benchmarking TMonitor
      Read/write: 13175780/sec
      Number of Subscribe/Unsubscribe calls: 69
      Number of Notify calls: 409
      Notify calls per thread: 64 78 42 8 9 32 9 10 37 7 34 11 7 12 7 9 9 8 7 9
      Maximum level of Notify concurrency: 1

      Benchmarking TCriticalSection
      Read/write: 13730015/sec
      Number of Subscribe/Unsubscribe calls: 20
      Number of Notify calls: 2493
      Notify calls per thread: 512 407 666 276 18 5 6 1 3 3 6 565 4 8 4 3 1 1 2 2
      Maximum level of Notify concurrency: 1

      Benchmarking TMREWSync
      Read: 1160817/sec, write: 661911/sec
      Number of Subscribe/Unsubscribe calls: 122
      Number of Notify calls: 4953
      Notify calls per thread: 139 2183 173 159 134 180 135 134 156 145 134 142 157 151 146 139 141 138 133 134
      Maximum level of Notify concurrency: 20

      • #55491
        Babis Michael
        Participant

        Ryzen 5800x TMonitor is the fastest ?

        Read thread 7 finished.
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 13 finished.
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 16 finished.
        Read thread 12 finished.
        Read thread 9 finished.
        Read thread 1 finished.
        Read thread 17 finished.
        Read thread 11 finished.
        Read thread 4 finished.
        Read thread 10 finished.
        Read thread 18 finished.
        Read thread 3 finished.
        Read thread 2 finished.
        Read thread 19 finished.
        Read thread 20 finished.
        Write thread 2 finished.
        Write thread 13 finished.
        Write thread 1 finished.
        Write thread 8 finished.
        Write thread 18 finished.
        Write thread 19 finished.
        Write thread 10 finished.
        Write thread 4 finished.
        Write thread 11 finished.
        Write thread 12 finished.
        Write thread 3 finished.
        Write thread 6 finished.
        Write thread 14 finished.
        Write thread 16 finished.
        Write thread 5 finished.
        Write thread 9 finished.
        Write thread 17 finished.
        Write thread 15 finished.
        Write thread 7 finished.
        Write thread 20 finished.
        Using: TSlimReaderWriter
        Time: 640
        ——————————————–
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 8 finished.
        Read thread 13 finished.
        Read thread 4 finished.
        Read thread 7 finished.
        Read thread 15 finished.
        Read thread 16 finished.
        Read thread 20 finished.
        Read thread 12 finished.
        Read thread 10 finished.
        Read thread 18 finished.
        Read thread 17 finished.
        Read thread 9 finished.
        Read thread 2 finished.
        Read thread 1 finished.
        Read thread 3 finished.
        Read thread 11 finished.
        Write thread 9 finished.
        Write thread 5 finished.
        Read thread 19 finished.
        Write thread 8 finished.
        Write thread 7 finished.
        Write thread 13 finished.
        Write thread 17 finished.
        Write thread 14 finished.
        Write thread 2 finished.
        Write thread 4 finished.
        Write thread 12 finished.
        Write thread 3 finished.
        Write thread 16 finished.
        Write thread 18 finished.
        Write thread 19 finished.
        Write thread 20 finished.
        Write thread 6 finished.
        Write thread 10 finished.
        Write thread 11 finished.
        Write thread 15 finished.
        Write thread 1 finished.
        Using: TMultiReadExclusiveWriteSynchronizer
        Time: 1375
        ——————————————–
        Write thread 11 finished.
        Write thread 17 finished.
        Write thread 18 finished.
        Write thread 20 finished.
        Write thread 16 finished.
        Write thread 19 finished.
        Write thread 12 finished.
        Write thread 10 finished.
        Write thread 7 finished.
        Write thread 1 finished.
        Write thread 9 finished.
        Write thread 13 finished.
        Write thread 4 finished.
        Write thread 2 finished.
        Write thread 6 finished.
        Read thread 6 finished.
        Write thread 3 finished.
        Read thread 14 finished.
        Write thread 8 finished.
        Read thread 16 finished.
        Read thread 13 finished.
        Read thread 5 finished.
        Write thread 14 finished.
        Read thread 12 finished.
        Read thread 19 finished.
        Read thread 7 finished.
        Read thread 4 finished.
        Read thread 11 finished.
        Read thread 3 finished.
        Write thread 5 finished.
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 1 finished.
        Read thread 9 finished.
        Read thread 2 finished.
        Read thread 18 finished.
        Read thread 20 finished.
        Read thread 17 finished.
        Read thread 10 finished.
        Write thread 15 finished.
        Using: TCriticalSection
        Time: 1844
        ——————————————–
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 16 finished.
        Read thread 7 finished.
        Read thread 13 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 5 finished.
        Read thread 19 finished.
        Read thread 12 finished.
        Read thread 11 finished.
        Read thread 20 finished.
        Write thread 10 finished.
        Write thread 19 finished.
        Write thread 17 finished.
        Read thread 2 finished.
        Write thread 18 finished.
        Write thread 3 finished.
        Write thread 20 finished.
        Write thread 5 finished.
        Write thread 11 finished.
        Read thread 17 finished.
        Write thread 12 finished.
        Read thread 1 finished.
        Write thread 13 finished.
        Read thread 9 finished.
        Write thread 4 finished.
        Read thread 4 finished.
        Write thread 1 finished.
        Write thread 6 finished.
        Write thread 2 finished.
        Read thread 18 finished.
        Read thread 10 finished.
        Write thread 9 finished.
        Write thread 15 finished.
        Write thread 7 finished.
        Write thread 8 finished.
        Read thread 3 finished.
        Write thread 16 finished.
        Write thread 14 finished.
        Using: TMonitor
        Time: 453
        ——————————————–
        Read thread 5 finished.
        Read thread 14 finished.
        Read thread 13 finished.
        Read thread 6 finished.
        Read thread 7 finished.
        Read thread 8 finished.
        Read thread 12 finished.
        Read thread 15 finished.
        Read thread 3 finished.
        Read thread 11 finished.
        Read thread 17 finished.
        Read thread 10 finished.
        Read thread 9 finished.
        Read thread 20 finished.
        Read thread 16 finished.
        Read thread 18 finished.
        Read thread 4 finished.
        Write thread 13 finished.
        Write thread 4 finished.
        Read thread 19 finished.
        Read thread 1 finished.
        Read thread 2 finished.
        Write thread 8 finished.
        Write thread 6 finished.
        Write thread 14 finished.
        Write thread 19 finished.
        Write thread 15 finished.
        Write thread 10 finished.
        Write thread 7 finished.
        Write thread 2 finished.
        Write thread 20 finished.
        Write thread 1 finished.
        Write thread 9 finished.
        Write thread 17 finished.
        Write thread 3 finished.
        Write thread 5 finished.
        Write thread 11 finished.
        Write thread 18 finished.
        Write thread 12 finished.
        Write thread 16 finished.
        Using: TkbmMWLock
        Time: 1062
        ——————————————–
        Read thread 15 finished.
        Read thread 8 finished.
        Read thread 13 finished.
        Read thread 16 finished.
        Read thread 7 finished.
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 19 finished.
        Read thread 11 finished.
        Read thread 3 finished.
        Read thread 9 finished.
        Read thread 20 finished.
        Read thread 17 finished.
        Read thread 1 finished.
        Read thread 10 finished.
        Read thread 12 finished.
        Read thread 2 finished.
        Read thread 4 finished.
        Read thread 18 finished.
        Write thread 9 finished.
        Write thread 7 finished.
        Write thread 18 finished.
        Write thread 4 finished.
        Write thread 15 finished.
        Write thread 17 finished.
        Write thread 19 finished.
        Write thread 1 finished.
        Write thread 16 finished.
        Write thread 20 finished.
        Write thread 11 finished.
        Write thread 13 finished.
        Write thread 8 finished.
        Write thread 5 finished.
        Write thread 12 finished.
        Write thread 14 finished.
        Write thread 6 finished.
        Write thread 3 finished.
        Write thread 2 finished.
        Write thread 10 finished.
        Using: TLighterMREW
        Time: 578
        ——————————————–

      • #55492
        Babis Michael
        Participant

        Ryzen 5800x

        Read thread 7 finished.
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 13 finished.
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 16 finished.
        Read thread 12 finished.
        Read thread 9 finished.
        Read thread 1 finished.
        Read thread 17 finished.
        Read thread 11 finished.
        Read thread 4 finished.
        Read thread 10 finished.
        Read thread 18 finished.
        Read thread 3 finished.
        Read thread 2 finished.
        Read thread 19 finished.
        Read thread 20 finished.
        Write thread 2 finished.
        Write thread 13 finished.
        Write thread 1 finished.
        Write thread 8 finished.
        Write thread 18 finished.
        Write thread 19 finished.
        Write thread 10 finished.
        Write thread 4 finished.
        Write thread 11 finished.
        Write thread 12 finished.
        Write thread 3 finished.
        Write thread 6 finished.
        Write thread 14 finished.
        Write thread 16 finished.
        Write thread 5 finished.
        Write thread 9 finished.
        Write thread 17 finished.
        Write thread 15 finished.
        Write thread 7 finished.
        Write thread 20 finished.
        Using: TSlimReaderWriter
        Time: 640
        ——————————————–
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 8 finished.
        Read thread 13 finished.
        Read thread 4 finished.
        Read thread 7 finished.
        Read thread 15 finished.
        Read thread 16 finished.
        Read thread 20 finished.
        Read thread 12 finished.
        Read thread 10 finished.
        Read thread 18 finished.
        Read thread 17 finished.
        Read thread 9 finished.
        Read thread 2 finished.
        Read thread 1 finished.
        Read thread 3 finished.
        Read thread 11 finished.
        Write thread 9 finished.
        Write thread 5 finished.
        Read thread 19 finished.
        Write thread 8 finished.
        Write thread 7 finished.
        Write thread 13 finished.
        Write thread 17 finished.
        Write thread 14 finished.
        Write thread 2 finished.
        Write thread 4 finished.
        Write thread 12 finished.
        Write thread 3 finished.
        Write thread 16 finished.
        Write thread 18 finished.
        Write thread 19 finished.
        Write thread 20 finished.
        Write thread 6 finished.
        Write thread 10 finished.
        Write thread 11 finished.
        Write thread 15 finished.
        Write thread 1 finished.
        Using: TMultiReadExclusiveWriteSynchronizer
        Time: 1375
        ——————————————–
        Write thread 11 finished.
        Write thread 17 finished.
        Write thread 18 finished.
        Write thread 20 finished.
        Write thread 16 finished.
        Write thread 19 finished.
        Write thread 12 finished.
        Write thread 10 finished.
        Write thread 7 finished.
        Write thread 1 finished.
        Write thread 9 finished.
        Write thread 13 finished.
        Write thread 4 finished.
        Write thread 2 finished.
        Write thread 6 finished.
        Read thread 6 finished.
        Write thread 3 finished.
        Read thread 14 finished.
        Write thread 8 finished.
        Read thread 16 finished.
        Read thread 13 finished.
        Read thread 5 finished.
        Write thread 14 finished.
        Read thread 12 finished.
        Read thread 19 finished.
        Read thread 7 finished.
        Read thread 4 finished.
        Read thread 11 finished.
        Read thread 3 finished.
        Write thread 5 finished.
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 1 finished.
        Read thread 9 finished.
        Read thread 2 finished.
        Read thread 18 finished.
        Read thread 20 finished.
        Read thread 17 finished.
        Read thread 10 finished.
        Write thread 15 finished.
        Using: TCriticalSection
        Time: 1844
        ——————————————–
        Read thread 8 finished.
        Read thread 15 finished.
        Read thread 16 finished.
        Read thread 7 finished.
        Read thread 13 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 5 finished.
        Read thread 19 finished.
        Read thread 12 finished.
        Read thread 11 finished.
        Read thread 20 finished.
        Write thread 10 finished.
        Write thread 19 finished.
        Write thread 17 finished.
        Read thread 2 finished.
        Write thread 18 finished.
        Write thread 3 finished.
        Write thread 20 finished.
        Write thread 5 finished.
        Write thread 11 finished.
        Read thread 17 finished.
        Write thread 12 finished.
        Read thread 1 finished.
        Write thread 13 finished.
        Read thread 9 finished.
        Write thread 4 finished.
        Read thread 4 finished.
        Write thread 1 finished.
        Write thread 6 finished.
        Write thread 2 finished.
        Read thread 18 finished.
        Read thread 10 finished.
        Write thread 9 finished.
        Write thread 15 finished.
        Write thread 7 finished.
        Write thread 8 finished.
        Read thread 3 finished.
        Write thread 16 finished.
        Write thread 14 finished.
        Using: TMonitor
        Time: 453
        ——————————————–
        Read thread 5 finished.
        Read thread 14 finished.
        Read thread 13 finished.
        Read thread 6 finished.
        Read thread 7 finished.
        Read thread 8 finished.
        Read thread 12 finished.
        Read thread 15 finished.
        Read thread 3 finished.
        Read thread 11 finished.
        Read thread 17 finished.
        Read thread 10 finished.
        Read thread 9 finished.
        Read thread 20 finished.
        Read thread 16 finished.
        Read thread 18 finished.
        Read thread 4 finished.
        Write thread 13 finished.
        Write thread 4 finished.
        Read thread 19 finished.
        Read thread 1 finished.
        Read thread 2 finished.
        Write thread 8 finished.
        Write thread 6 finished.
        Write thread 14 finished.
        Write thread 19 finished.
        Write thread 15 finished.
        Write thread 10 finished.
        Write thread 7 finished.
        Write thread 2 finished.
        Write thread 20 finished.
        Write thread 1 finished.
        Write thread 9 finished.
        Write thread 17 finished.
        Write thread 3 finished.
        Write thread 5 finished.
        Write thread 11 finished.
        Write thread 18 finished.
        Write thread 12 finished.
        Write thread 16 finished.
        Using: TkbmMWLock
        Time: 1062
        ——————————————–
        Read thread 15 finished.
        Read thread 8 finished.
        Read thread 13 finished.
        Read thread 16 finished.
        Read thread 7 finished.
        Read thread 5 finished.
        Read thread 6 finished.
        Read thread 14 finished.
        Read thread 19 finished.
        Read thread 11 finished.
        Read thread 3 finished.
        Read thread 9 finished.
        Read thread 20 finished.
        Read thread 17 finished.
        Read thread 1 finished.
        Read thread 10 finished.
        Read thread 12 finished.
        Read thread 2 finished.
        Read thread 4 finished.
        Read thread 18 finished.
        Write thread 9 finished.
        Write thread 7 finished.
        Write thread 18 finished.
        Write thread 4 finished.
        Write thread 15 finished.
        Write thread 17 finished.
        Write thread 19 finished.
        Write thread 1 finished.
        Write thread 16 finished.
        Write thread 20 finished.
        Write thread 11 finished.
        Write thread 13 finished.
        Write thread 8 finished.
        Write thread 5 finished.
        Write thread 12 finished.
        Write thread 14 finished.
        Write thread 6 finished.
        Write thread 3 finished.
        Write thread 2 finished.
        Write thread 10 finished.
        Using: TLighterMREW
        Time: 578
        ——————————————–

    • #55493
      kimbomadsen
      Keymaster

      Hi,

      Yes TMonitor is fast, but it is also significantly simpler.

      Try to disable lock escalation support in kbmMWLock and you will see it come closer to TMonitor speed.

      It also seems the performance numbers depends on CPU. On my 1950x kbmMWLock consistently is faster than TLightweightMREW, both with or without lock escalation support. If you disable lockescalation support in kbmMWLock, TLightweightMREW takes almost 2.5 times the time of kbmMWLock, while your 64 core beast seems to favour TLightweightMREW.

      As many things can be finetuned in TkbmMWLock at creation time, I wonder if those things can be finetuned towards your beast too.

      What it shows is that synthetic benchmarks rarely shows the full picture.

      MW:=TkbmMWLock.Create(mwlmReaderBiased,100,1,64,true); // Create lock with standard settings but without lock escalation support.

      best regards

      Kim/C4D

    • #55496
      Babis Michael
      Participant

      Kim,

      For some reason i can not see my benchmark posts i can see only your posts.

      Anyway, you can use “defines” to finetune according to the CPU architecture as i did in the old FastCode if you wish! πŸ™‚

      btw, Ryzen 5800x is 8 core 16 thread CPU .

      • #55499
        kimbomadsen
        Keymaster

        Of some reason your posts were perceived as spam. I have “unspammed” them πŸ™‚ So you should be able to see them now.

        best regards

        Kim/C4D

Viewing 3 reply threads
  • You must be logged in to reply to this topic.