This time I am not changing LWLock semantics :)

Consider the a hash table partitioned in two different ways.

1. having S partitions, guarded by LWLocks on the same cache line.
2. having a single LWLock.

Which one will give better concurrency?
Which one will cost more to lock/unlock?

I argue that having S partitions is better in several different ways.

a. Concurrency of LW_EXCLUSIVE
b. Reduced contention on LWLockWaitListLock
c. Reduced CAS iterations in LWLockAtttemptLock
d. Reduced waits (that implies wait list access and sleeping)

I included a (pointless) benchmark result.

And you can find a longer discussion in 0002 patch.
Statistics for 20 samples per process, from 1 to 12 processes,
of execution time of

```C
for(int i = 0; i < 100; ++i)
{
      int                       idx = (int) pg_prng_int64_range(&rng, 0, n_part 
- 1);
      LWLock       *lock = locks[idx];

      sink += LWLockAcquire(lock, LW_SHARED) ? 1 : 0;
      LWLockRelease(lock);
}
```


 n_proc | op |  avg  |  med  | std  |  min  |  max  
--------+----+-------+-------+------+-------+-------
      1 | 1  | 16.17 | 15.83 | 1.56 | 15.41 | 22.50
      1 | 2  | 15.06 | 15.00 | 0.24 | 14.58 | 15.42
      1 | 3  | 14.48 | 14.58 | 0.43 | 13.75 | 15.42
      1 | 4  | 13.96 | 13.96 | 0.21 | 13.75 | 14.17
      1 | 5  | 15.60 | 15.83 | 0.70 | 14.17 | 16.67
      1 | 6  | 13.85 | 13.75 | 0.30 | 13.33 | 14.58
      1 | 7  | 13.27 | 13.33 | 0.25 | 12.91 | 13.75



 n_proc | op |  avg   |  med   |  std  |  min  |  max   
--------+----+--------+--------+-------+-------+--------
      2 | 1  | 110.95 | 112.29 | 10.26 | 91.25 | 122.08
      2 | 2  | 114.23 | 119.79 | 12.25 | 89.17 | 126.66
      2 | 3  | 105.54 | 108.34 |  9.45 | 89.17 | 117.09
      2 | 4  | 103.17 | 106.04 | 10.06 | 84.59 | 116.67
      2 | 5  |  83.83 |  86.67 | 10.88 | 56.25 |  95.83
      2 | 6  |  74.69 |  78.96 |  7.97 | 59.17 |  84.58
      2 | 7  |  70.84 |  72.50 |  7.47 | 53.33 |  84.17



 n_proc | op |  avg   |  med   |  std  |  min   |  max   
--------+----+--------+--------+-------+--------+--------
      3 | 1  | 202.20 | 208.96 | 21.93 | 113.34 | 234.16
      3 | 2  | 197.93 | 198.75 | 19.66 | 159.58 | 281.67
      3 | 3  | 180.74 | 180.63 | 12.75 | 152.09 | 249.16
      3 | 4  | 171.51 | 173.54 | 14.39 | 138.33 | 240.00
      3 | 5  | 145.51 | 146.88 |  7.94 | 121.25 | 167.92
      3 | 6  | 133.81 | 131.67 | 15.64 | 115.42 | 205.83
      3 | 7  | 124.40 | 123.55 |  9.83 | 111.66 | 187.92



 n_proc | op |  avg   |  med   |  std   |  min   |   max   
--------+----+--------+--------+--------+--------+---------
      4 | 1  | 328.77 | 301.25 | 262.05 | 166.66 | 2382.92
      4 | 2  | 272.43 | 278.13 | 177.51 |  25.42 | 1661.25
      4 | 3  | 270.32 | 268.13 | 314.53 |  18.33 | 2998.75
      4 | 4  | 235.94 | 252.09 |  59.59 | 129.59 |  535.83
      4 | 5  | 227.15 | 245.21 |  39.19 | 127.92 |  277.08
      4 | 6  | 206.78 | 220.21 |  35.21 | 132.08 |  261.25
      4 | 7  | 189.20 | 207.71 |  42.22 |  25.42 |  238.75



 n_proc | op |  avg   |  med   |   std   |  min   |   max    
--------+----+--------+--------+---------+--------+----------
      5 | 1  | 505.15 | 521.46 |  104.74 | 181.67 |   956.66
      5 | 2  | 448.70 | 455.42 |   83.08 | 292.09 |  1157.08
      5 | 3  | 425.05 | 432.92 |   33.22 | 338.75 |   470.84
      5 | 4  | 454.80 | 395.00 |  729.49 | 190.42 |  7654.58
      5 | 5  | 581.73 | 344.17 | 2371.56 |  30.83 | 24020.00
      5 | 6  | 318.73 | 319.38 |   27.74 | 251.25 |   363.34
      5 | 7  | 304.48 | 307.29 |   29.92 | 242.50 |   404.17



 n_proc | op |  avg   |  med   |  std   |  min   |   max   
--------+----+--------+--------+--------+--------+---------
      6 | 1  | 629.19 | 618.55 |  97.59 | 312.91 |  760.42
      6 | 2  | 573.93 | 567.09 | 115.77 | 299.16 | 1374.16
      6 | 3  | 515.10 | 535.00 |  77.25 | 175.00 |  625.84
      6 | 4  | 482.97 | 498.54 |  89.71 |  23.34 |  922.92
      6 | 5  | 454.85 | 482.92 |  69.80 | 268.33 |  804.58
      6 | 6  | 430.80 | 430.21 | 185.31 | 262.09 | 2369.17
      6 | 7  | 391.96 | 412.92 |  55.61 | 233.75 |  450.42



 n_proc | op |  avg   |  med   |  std   |  min   |   max   
--------+----+--------+--------+--------+--------+---------
      7 | 1  | 818.72 | 801.67 | 647.36 |  23.75 | 7978.33
      7 | 2  | 689.89 | 720.21 | 128.47 |  15.00 |  835.42
      7 | 3  | 651.53 | 676.04 |  72.80 | 335.84 |  725.42
      7 | 4  | 649.07 | 621.67 | 681.96 | 210.00 | 8581.25
      7 | 5  | 545.21 | 535.42 | 191.00 |  29.58 | 2657.50
      7 | 6  | 496.41 | 495.00 |  52.38 | 373.33 |  667.50
      7 | 7  | 472.23 | 477.71 |  49.74 | 320.42 |  550.00



 n_proc | op |  avg   |  med   |   std   |  min  |   max    
--------+----+--------+--------+---------+-------+----------
      8 | 1  | 808.78 | 897.29 |  245.17 | 12.08 |  1079.58
      8 | 2  | 804.72 | 911.04 |  263.41 | 13.75 |  1455.41
      8 | 3  | 725.86 | 789.79 |  209.63 | 14.58 |  1249.17
      8 | 4  | 711.27 | 759.58 |  259.57 | 14.16 |  3036.67
      8 | 5  | 670.01 | 763.34 |  227.69 | 13.75 |  1385.00
      8 | 6  | 916.34 | 679.38 | 3069.38 | 12.91 | 35931.66
      8 | 7  | 527.36 | 638.96 |  197.90 | 12.91 |   706.67



 n_proc | op |   avg   |  med   |   std   |  min  |   max    
--------+----+---------+--------+---------+-------+----------
      9 | 1  |  765.00 | 891.04 |  663.25 | 11.25 |  8188.33
      9 | 2  |  820.31 | 881.46 |  991.92 | 13.33 | 13385.42
      9 | 3  |  820.82 | 819.17 | 1492.09 | 12.91 | 17189.17
      9 | 4  | 1077.87 | 753.96 | 3235.68 | 13.34 | 32688.33
      9 | 5  |  805.22 | 732.08 | 2961.26 | 13.75 | 40145.00
      9 | 6  |  642.98 | 645.42 | 1366.95 | 12.50 | 18573.33
      9 | 7  |  701.65 | 579.79 | 2014.68 | 12.09 | 21728.33



 n_proc | op |   avg   |  med   |   std   |  min  |   max    
--------+----+---------+--------+---------+-------+----------
     10 | 1  | 1059.40 | 926.88 | 4698.34 | 12.08 | 66922.91
     10 | 2  | 1166.22 | 938.75 | 3651.48 | 12.91 | 47069.17
     10 | 3  | 1756.45 | 831.04 | 8438.07 | 12.09 | 83536.66
     10 | 4  |  929.20 | 778.76 | 4466.06 | 12.09 | 63622.08
     10 | 5  |  623.25 | 782.51 |  309.65 | 14.16 |   848.75
     10 | 6  |  651.93 | 674.79 | 1212.02 | 12.92 | 13497.08
     10 | 7  |  528.45 | 651.67 |  253.60 | 12.50 |   713.33   
--------+----+---------+--------+----------+-------+-----------
     11 | 1  |  678.42 | 912.09 |   417.84 | 10.83 |   1184.58
     11 | 2  | 1654.62 | 780.00 | 10701.60 | 12.91 | 119140.00
     11 | 3  | 1403.58 | 799.58 |  8454.71 | 12.50 |  90955.00
     11 | 4  | 1046.45 | 721.25 |  5752.25 | 12.92 |  80720.84
     11 | 5  |  495.42 | 663.54 |   329.99 | 12.92 |   1041.25
     11 | 6  |  537.54 | 641.25 |  1020.01 | 11.66 |  14979.16
     11 | 7  |  410.60 | 475.62 |   271.27 | 12.08 |    701.67  
--------+----+---------+--------+----------+-------+-----------
     12 | 1  | 1985.79 | 838.12 | 12980.48 | 12.08 | 172370.00
     12 | 2  | 1637.70 | 912.50 |  9014.67 | 12.92 | 100103.75
     12 | 3  | 1815.80 | 821.25 | 11141.96 | 12.50 | 101198.75
     12 | 4  | 1352.02 | 757.71 |  9253.80 | 12.50 | 113313.75
     12 | 5  |  998.03 | 768.96 |  7319.46 | 12.50 | 113783.33
     12 | 6  |  468.17 | 668.75 |   316.41 | 11.67 |    828.33
     12 | 7  |  698.87 | 641.88 |  3133.12 | 12.09 |  46641.25

Attachment: v1-0002-introduce-LWLockAligned.patch
Description: Binary data

Attachment: v1-0001-LWLock-struct-layout.patch
Description: Binary data

Reply via email to