• Kirill Smelkov's avatar
    X zodb/cache: Switch RCE.ready from chan to sync.WaitGroup · 1f07e51a
    Kirill Smelkov authored
    Currently RCE.ready is chan(struct{}) and is closed to indicate 1->N
    that loading of an RCE has finished. There is a drawback however with
    using channel for this function:
    
    	making RCE.ready allocates and thus adds pressure on GC.
    
    We can use sync.WaitGroup functionality for this purpose of notifying
    that something is ready. Usually sync.WaitGroup is used in N->1 way, but
    as it also correctly works in N->M mode we can use in in our scenario
    where 1 loading goroutine notifies N RCE waiters.
    
    Checking already closed channel was also found to checking already done
    sync.WaitGroup. Reason is that sync.WaitGroup is only a light wrapper
    around runtime.sema, but channel is much more heavyweight.
    
    Speedup with this patch:
    
    name                        old time/op    new time/op    delta
    NoopStorage-4                 56.2ns ± 0%    57.1ns ± 2%     ~     (p=0.079 n=5+5)
    CacheStartup-4                1.34µs ± 7%    1.22µs ± 3%   -8.68%  (p=0.008 n=5+5)
    CacheNoHit/size=0-4           1.67µs ± 4%    1.42µs ± 4%  -15.08%  (p=0.008 n=5+5)
    CacheNoHit/size=16-4          1.73µs ± 7%    1.42µs ± 4%  -17.83%  (p=0.008 n=5+5)
    CacheNoHit/size=128-4         1.68µs ± 5%    1.42µs ± 5%  -15.65%  (p=0.008 n=5+5)
    CacheNoHit/size=512-4         1.62µs ± 4%    1.43µs ± 5%  -12.18%  (p=0.008 n=5+5)
    CacheNoHit/size=4096-4        1.69µs ± 2%    1.44µs ± 0%  -15.03%  (p=0.008 n=5+5)
    CacheHit/size=0-4              158ns ± 1%     130ns ± 2%  -17.47%  (p=0.008 n=5+5)
    CacheHit/size=16-4             147ns ± 2%     124ns ± 7%  -16.01%  (p=0.008 n=5+5)
    CacheHit/size=128-4            149ns ± 0%     126ns ± 5%  -15.30%  (p=0.016 n=4+5)
    CacheHit/size=512-4            151ns ± 0%     126ns ± 1%  -16.56%  (p=0.000 n=4+5)
    CacheHit/size=4096-4           154ns ± 0%     129ns ± 2%  -16.49%  (p=0.008 n=5+5)
    NoopStoragePar-4              32.2ns ± 9%    30.4ns ± 5%     ~     (p=0.175 n=5+5)
    CacheStartupPar-4             1.64µs ± 2%    1.42µs ± 4%  -13.31%  (p=0.008 n=5+5)
    CacheNoHitPar/size=0-4        1.88µs ± 2%    1.63µs ± 3%  -13.33%  (p=0.008 n=5+5)
    CacheNoHitPar/size=16-4       1.87µs ± 1%    1.62µs ± 2%  -13.33%  (p=0.008 n=5+5)
    CacheNoHitPar/size=128-4      1.90µs ± 3%    1.64µs ± 2%  -13.68%  (p=0.008 n=5+5)
    CacheNoHitPar/size=512-4      1.86µs ± 3%    1.62µs ± 1%  -12.91%  (p=0.008 n=5+5)
    CacheNoHitPar/size=4096-4     1.87µs ± 3%    1.70µs ± 3%   -9.21%  (p=0.008 n=5+5)
    CacheHitPar/size=0-4           233ns ± 0%     217ns ± 3%   -6.87%  (p=0.016 n=4+5)
    CacheHitPar/size=16-4          228ns ± 2%     225ns ± 2%     ~     (p=0.119 n=5+5)
    CacheHitPar/size=128-4         232ns ± 4%     214ns ± 1%   -7.92%  (p=0.008 n=5+5)
    CacheHitPar/size=512-4         228ns ± 1%     210ns ± 1%   -7.82%  (p=0.008 n=5+5)
    CacheHitPar/size=4096-4        226ns ± 2%     209ns ± 2%   -7.54%  (p=0.008 n=5+5)
    NoopStorageProc-4             34.1ns ± 6%    34.9ns ±18%     ~     (p=0.690 n=5+5)
    CacheStartupProc-4            1.14µs ± 8%    1.12µs ± 4%     ~     (p=0.802 n=5+5)
    CacheNoHitProc/size=0-4       1.32µs ± 4%    1.10µs ± 6%  -16.92%  (p=0.008 n=5+5)
    CacheNoHitProc/size=16-4      1.32µs ± 2%    1.14µs ± 7%  -13.54%  (p=0.008 n=5+5)
    CacheNoHitProc/size=128-4     1.30µs ± 6%    1.07µs ±10%  -17.99%  (p=0.008 n=5+5)
    CacheNoHitProc/size=512-4     1.26µs ± 5%    1.09µs ± 5%  -13.47%  (p=0.008 n=5+5)
    CacheNoHitProc/size=4096-4    1.27µs ± 3%    1.09µs ± 7%  -14.55%  (p=0.008 n=5+5)
    CacheHitProc/size=0-4         69.5ns ± 6%    56.4ns ±10%  -18.88%  (p=0.008 n=5+5)
    CacheHitProc/size=16-4        75.1ns ± 6%    55.8ns ± 1%  -25.65%  (p=0.008 n=5+5)
    CacheHitProc/size=128-4       74.5ns ± 4%    57.1ns ± 1%  -23.31%  (p=0.008 n=5+5)
    CacheHitProc/size=512-4       69.4ns ± 1%    58.1ns ± 2%  -16.27%  (p=0.008 n=5+5)
    CacheHitProc/size=4096-4      93.3ns ± 5%    63.2ns ± 4%  -32.25%  (p=0.008 n=5+5)
    
    name                        old allocs/op  new allocs/op  delta
    NoopStorage-4                   0.00           0.00          ~     (all equal)
    CacheStartup-4                  6.00 ± 0%      5.00 ± 0%  -16.67%  (p=0.008 n=5+5)
    CacheNoHit/size=0-4             4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHit/size=16-4            4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHit/size=128-4           4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHit/size=512-4           4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHit/size=4096-4          4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheHit/size=0-4               0.00           0.00          ~     (all equal)
    CacheHit/size=16-4              0.00           0.00          ~     (all equal)
    CacheHit/size=128-4             0.00           0.00          ~     (all equal)
    CacheHit/size=512-4             0.00           0.00          ~     (all equal)
    CacheHit/size=4096-4            0.00           0.00          ~     (all equal)
    NoopStoragePar-4                0.00           0.00          ~     (all equal)
    CacheStartupPar-4               5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.008 n=5+5)
    CacheNoHitPar/size=0-4          4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitPar/size=16-4         4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitPar/size=128-4        4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitPar/size=512-4        4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitPar/size=4096-4       4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheHitPar/size=0-4            0.00           0.00          ~     (all equal)
    CacheHitPar/size=16-4           0.00           0.00          ~     (all equal)
    CacheHitPar/size=128-4          0.00           0.00          ~     (all equal)
    CacheHitPar/size=512-4          0.00           0.00          ~     (all equal)
    CacheHitPar/size=4096-4         0.00           0.00          ~     (all equal)
    NoopStorageProc-4               0.00           0.00          ~     (all equal)
    CacheStartupProc-4              6.00 ± 0%      5.00 ± 0%  -16.67%  (p=0.008 n=5+5)
    CacheNoHitProc/size=0-4         4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitProc/size=16-4        4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitProc/size=128-4       4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitProc/size=512-4       4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheNoHitProc/size=4096-4      4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
    CacheHitProc/size=0-4           0.00           0.00          ~     (all equal)
    CacheHitProc/size=16-4          0.00           0.00          ~     (all equal)
    CacheHitProc/size=128-4         0.00           0.00          ~     (all equal)
    CacheHitProc/size=512-4         0.00           0.00          ~     (all equal)
    CacheHitProc/size=4096-4        0.00           0.00          ~     (all equal)
    1f07e51a
cache.go 18 KB