Commit 0e2be3b3 authored by Kirill Smelkov's avatar Kirill Smelkov

X zodb/cache: Performing GC in separate goroutine was actually a bad idea

Probably channel signalling + goroutine scheduling overhead was taking
too much - see appendix for timings.

Once again - as in 521d3ae5 (X zodb/cache: Also free OCE entries on GC)
we can ignore *Hit/size=0 increase as /size=0 won't happen in practice
and we mostly care about NoHit overhead for which /size=0 also improves.
The HitProc/* increase, as in 521d3ae5, is also withing noise.

---- 8< ----

name                        old time/op    new time/op    delta
NoopStorage                   56.6ns ± 0%    57.9ns ± 1%     +2.36%  (p=0.100 n=3+3)
NoopStorage-4                 56.9ns ± 1%    57.4ns ± 1%       ~     (p=0.200 n=3+3)
CacheStartup                  1.97µs ± 3%    2.13µs ±16%       ~     (p=0.600 n=3+3)
CacheStartup-4                1.29µs ± 4%    1.29µs ± 4%       ~     (p=1.000 n=3+3)
CacheNoHit/size=0             1.34µs ± 1%    0.68µs ± 1%    -49.43%  (p=0.100 n=3+3)
CacheNoHit/size=0-4            941ns ± 1%     664ns ± 1%    -29.38%  (p=0.100 n=3+3)
CacheNoHit/size=16            1.38µs ± 3%    0.79µs ± 0%    -42.49%  (p=0.100 n=3+3)
CacheNoHit/size=16-4           939ns ± 2%     788ns ± 0%    -16.02%  (p=0.100 n=3+3)
CacheNoHit/size=128           1.32µs ± 2%    0.80µs ± 0%    -39.55%  (p=0.100 n=3+3)
CacheNoHit/size=128-4          946ns ± 1%     795ns ± 1%    -16.03%  (p=0.100 n=3+3)
CacheNoHit/size=512           1.37µs ± 3%    0.82µs ± 0%    -40.33%  (p=0.100 n=3+3)
CacheNoHit/size=512-4          977ns ± 2%     803ns ± 0%    -17.75%  (p=0.100 n=3+3)
CacheNoHit/size=4096          1.37µs ± 4%    1.05µs ± 1%    -23.03%  (p=0.100 n=3+3)
CacheNoHit/size=4096-4        1.07µs ± 0%    0.86µs ± 0%    -19.94%  (p=0.100 n=3+3)
CacheHit/size=0                164ns ±29%     664ns ± 0%   +305.08%  (p=0.100 n=3+3)
CacheHit/size=0-4              251ns ± 6%     660ns ± 0%   +162.95%  (p=0.100 n=3+3)
CacheHit/size=16               120ns ± 1%     123ns ± 4%       ~     (p=0.700 n=3+3)
CacheHit/size=16-4             121ns ± 2%     121ns ± 1%       ~     (p=1.000 n=3+3)
CacheHit/size=128              123ns ± 1%     124ns ± 1%       ~     (p=1.000 n=3+3)
CacheHit/size=128-4            124ns ± 0%     124ns ± 1%       ~     (p=1.000 n=3+3)
CacheHit/size=512              125ns ± 0%     126ns ± 1%       ~     (p=0.400 n=3+3)
CacheHit/size=512-4            126ns ± 1%     125ns ± 1%       ~     (p=0.400 n=3+3)
CacheHit/size=4096             128ns ± 1%     127ns ± 1%       ~     (p=1.000 n=3+3)
CacheHit/size=4096-4           128ns ± 1%     128ns ± 1%       ~     (p=1.000 n=3+3)
NoopStoragePar                56.5ns ± 0%    59.2ns ± 1%     +4.84%  (p=0.100 n=3+3)
NoopStoragePar-4              33.1ns ± 3%    31.2ns ± 8%       ~     (p=0.400 n=3+3)
CacheStartupPar               2.01µs ±14%    2.04µs ± 6%       ~     (p=0.800 n=3+3)
CacheStartupPar-4             1.47µs ± 4%    1.48µs ± 3%       ~     (p=1.000 n=3+3)
CacheNoHitPar/size=0          1.35µs ± 3%    0.67µs ± 0%    -50.25%  (p=0.100 n=3+3)
CacheNoHitPar/size=0-4        1.04µs ± 0%    0.90µs ± 0%    -12.83%  (p=0.100 n=3+3)
CacheNoHitPar/size=16         1.32µs ± 2%    0.79µs ± 0%    -40.36%  (p=0.100 n=3+3)
CacheNoHitPar/size=16-4       1.04µs ± 0%    1.03µs ± 0%     -1.28%  (p=0.100 n=3+3)
CacheNoHitPar/size=128        1.33µs ± 1%    0.80µs ± 0%    -40.02%  (p=0.100 n=3+3)
CacheNoHitPar/size=128-4      1.03µs ± 1%    1.04µs ± 0%       ~     (p=0.400 n=3+3)
CacheNoHitPar/size=512        1.34µs ± 2%    0.82µs ± 0%    -38.79%  (p=0.100 n=3+3)
CacheNoHitPar/size=512-4      1.07µs ± 2%    1.07µs ± 0%       ~     (p=0.600 n=3+3)
CacheNoHitPar/size=4096       1.36µs ± 4%    1.06µs ± 1%    -21.87%  (p=0.100 n=3+3)
CacheNoHitPar/size=4096-4     1.16µs ± 2%    1.17µs ± 1%       ~     (p=0.700 n=3+3)
CacheHitPar/size=0             163ns ± 3%     665ns ± 0%   +306.94%  (p=0.100 n=3+3)
CacheHitPar/size=0-4           394ns ± 3%     903ns ± 1%   +129.08%  (p=0.100 n=3+3)
CacheHitPar/size=16            121ns ± 0%     124ns ± 1%     +2.20%  (p=0.100 n=3+3)
CacheHitPar/size=16-4          213ns ± 0%     217ns ± 1%     +2.03%  (p=0.100 n=3+3)
CacheHitPar/size=128           124ns ± 1%     125ns ± 0%       ~     (p=0.400 n=3+3)
CacheHitPar/size=128-4         208ns ± 0%     215ns ± 1%     +3.21%  (p=0.100 n=3+3)
CacheHitPar/size=512           126ns ± 1%     127ns ± 1%       ~     (p=0.300 n=3+3)
CacheHitPar/size=512-4         205ns ± 0%     241ns ± 0%    +17.40%  (p=0.100 n=3+3)
CacheHitPar/size=4096          127ns ± 0%     129ns ± 0%     +1.57%  (p=0.100 n=3+3)
CacheHitPar/size=4096-4        201ns ± 0%     236ns ± 4%    +17.58%  (p=0.100 n=3+3)
NoopStorageProc               54.8ns ± 0%    56.1ns ± 0%     +2.43%  (p=0.100 n=3+3)
NoopStorageProc-4             32.6ns ± 4%    37.1ns ±10%    +14.02%  (p=0.100 n=3+3)
CacheStartupProc              1.91µs ± 7%    1.79µs ± 3%       ~     (p=0.400 n=3+3)
CacheStartupProc-4            1.13µs ± 1%    1.08µs ± 7%       ~     (p=0.400 n=3+3)
CacheNoHitProc/size=0         1.32µs ± 1%    0.67µs ± 0%    -49.38%  (p=0.100 n=3+3)
CacheNoHitProc/size=0-4        649ns ± 1%     414ns ±15%    -36.23%  (p=0.100 n=3+3)
CacheNoHitProc/size=16        1.33µs ± 3%    0.79µs ± 0%    -40.23%  (p=0.100 n=3+3)
CacheNoHitProc/size=16-4       650ns ± 2%     440ns ± 7%    -32.34%  (p=0.100 n=3+3)
CacheNoHitProc/size=128       1.31µs ± 5%    0.81µs ± 0%    -38.41%  (p=0.100 n=3+3)
CacheNoHitProc/size=128-4      663ns ± 1%     467ns ± 8%    -29.58%  (p=0.100 n=3+3)
CacheNoHitProc/size=512       1.30µs ± 2%    0.83µs ± 1%    -35.72%  (p=0.100 n=3+3)
CacheNoHitProc/size=512-4      685ns ± 1%     499ns ± 9%    -27.12%  (p=0.100 n=3+3)
CacheNoHitProc/size=4096      1.35µs ± 3%    1.07µs ± 0%    -20.87%  (p=0.100 n=3+3)
CacheNoHitProc/size=4096-4     684ns ± 2%     629ns ± 3%     -8.04%  (p=0.100 n=3+3)
CacheHitProc/size=0            159ns ± 3%     669ns ± 0%   +320.75%  (p=0.100 n=3+3)
CacheHitProc/size=0-4         75.1ns ±10%   381.0ns ± 2%   +407.10%  (p=0.100 n=3+3)
CacheHitProc/size=16           120ns ± 1%     121ns ± 1%       ~     (p=0.300 n=3+3)
CacheHitProc/size=16-4        58.1ns ± 2%    65.0ns ± 3%    +11.94%  (p=0.100 n=3+3)
CacheHitProc/size=128          123ns ± 0%     124ns ± 0%     +0.81%  (p=0.100 n=3+3)
CacheHitProc/size=128-4       57.6ns ± 0%    74.9ns ± 3%    +30.05%  (p=0.100 n=3+3)
CacheHitProc/size=512          124ns ± 1%     125ns ± 1%       ~     (p=0.300 n=3+3)
CacheHitProc/size=512-4       59.4ns ± 1%    71.5ns ± 2%    +20.38%  (p=0.100 n=3+3)
CacheHitProc/size=4096         127ns ± 0%     132ns ± 6%     +3.94%  (p=0.100 n=3+3)
CacheHitProc/size=4096-4      64.2ns ± 1%    80.5ns ± 7%    +25.34%  (p=0.100 n=3+3)

name                        old alloc/op   new alloc/op   delta
NoopStorage                    0.00B          0.00B            ~     (all equal)
NoopStorage-4                  0.00B          0.00B            ~     (all equal)
CacheStartup                    285B ± 0%      285B ± 0%       ~     (all equal)
CacheStartup-4                  285B ± 0%      285B ± 0%       ~     (all equal)
CacheNoHit/size=0               247B ± 2%      152B ± 0%    -38.38%  (p=0.100 n=3+3)
CacheNoHit/size=0-4             153B ± 0%      152B ± 0%     -0.65%  (p=0.100 n=3+3)
CacheNoHit/size=16              247B ± 2%      153B ± 0%    -37.97%  (p=0.100 n=3+3)
CacheNoHit/size=16-4            153B ± 0%      153B ± 0%       ~     (p=1.000 n=3+3)
CacheNoHit/size=128             241B ± 1%      153B ± 0%    -36.60%  (p=0.100 n=3+3)
CacheNoHit/size=128-4           153B ± 0%      153B ± 0%       ~     (p=1.000 n=3+3)
CacheNoHit/size=512             245B ± 1%      153B ± 0%    -37.64%  (p=0.100 n=3+3)
CacheNoHit/size=512-4           154B ± 0%      153B ± 0%     -0.65%  (p=0.100 n=3+3)
CacheNoHit/size=4096            244B ± 1%      153B ± 0%    -37.21%  (p=0.100 n=3+3)
CacheNoHit/size=4096-4          155B ± 0%      152B ± 0%     -1.72%  (p=0.100 n=3+3)
CacheHit/size=0               5.00B ±100%   152.00B ± 0%  +2940.00%  (p=0.100 n=3+3)
CacheHit/size=0-4              11.0B ± 9%    152.0B ± 0%  +1281.82%  (p=0.100 n=3+3)
CacheHit/size=16               0.00B          0.00B            ~     (all equal)
CacheHit/size=16-4             0.00B          0.00B            ~     (all equal)
CacheHit/size=128              0.00B          0.00B            ~     (all equal)
CacheHit/size=128-4            0.00B          0.00B            ~     (all equal)
CacheHit/size=512              0.00B          0.00B            ~     (all equal)
CacheHit/size=512-4            0.00B          0.00B            ~     (all equal)
CacheHit/size=4096             0.00B          0.00B            ~     (all equal)
CacheHit/size=4096-4           0.00B          0.00B            ~     (all equal)
NoopStoragePar                 0.00B          0.00B            ~     (all equal)
NoopStoragePar-4               0.00B          0.00B            ~     (all equal)
CacheStartupPar                 285B ± 0%      285B ± 0%       ~     (all equal)
CacheStartupPar-4               282B ± 1%      281B ± 0%       ~     (p=0.600 n=3+3)
CacheNoHitPar/size=0            247B ± 2%      152B ± 0%    -38.38%  (p=0.100 n=3+3)
CacheNoHitPar/size=0-4          161B ± 0%      152B ± 0%     -5.59%  (p=0.100 n=3+3)
CacheNoHitPar/size=16           245B ± 1%      153B ± 0%    -37.55%  (p=0.100 n=3+3)
CacheNoHitPar/size=16-4         161B ± 0%      155B ± 0%     -3.93%  (p=0.100 n=3+3)
CacheNoHitPar/size=128          245B ± 2%      153B ± 0%    -37.55%  (p=0.100 n=3+3)
CacheNoHitPar/size=128-4        162B ± 0%      153B ± 0%     -5.36%  (p=0.100 n=3+3)
CacheNoHitPar/size=512          246B ± 3%      153B ± 0%    -37.89%  (p=0.100 n=3+3)
CacheNoHitPar/size=512-4        161B ± 0%      153B ± 0%     -4.97%  (p=0.100 n=3+3)
CacheNoHitPar/size=4096         244B ± 3%      153B ± 0%    -37.38%  (p=0.100 n=3+3)
CacheNoHitPar/size=4096-4       164B ± 0%      153B ± 0%     -6.71%  (p=0.100 n=3+3)
CacheHitPar/size=0             5.00B ±20%   152.00B ± 0%  +2940.00%  (p=0.100 n=3+3)
CacheHitPar/size=0-4           14.0B ± 7%    152.0B ± 0%   +985.71%  (p=0.100 n=3+3)
CacheHitPar/size=16            0.00B          0.00B            ~     (all equal)
CacheHitPar/size=16-4          0.00B          0.00B            ~     (all equal)
CacheHitPar/size=128           0.00B          0.00B            ~     (all equal)
CacheHitPar/size=128-4         0.00B          0.00B            ~     (all equal)
CacheHitPar/size=512           0.00B          0.00B            ~     (all equal)
CacheHitPar/size=512-4         0.00B          0.00B            ~     (all equal)
CacheHitPar/size=4096          0.00B          0.00B            ~     (all equal)
CacheHitPar/size=4096-4        0.00B          0.00B            ~     (all equal)
NoopStorageProc                0.00B          0.00B            ~     (all equal)
NoopStorageProc-4              0.00B          0.00B            ~     (all equal)
CacheStartupProc                285B ± 0%      285B ± 0%       ~     (all equal)
CacheStartupProc-4              285B ± 0%      285B ± 0%       ~     (all equal)
CacheNoHitProc/size=0           243B ± 1%      152B ± 0%    -37.53%  (p=0.100 n=3+3)
CacheNoHitProc/size=0-4         195B ± 1%      152B ± 0%    -22.05%  (p=0.100 n=3+3)
CacheNoHitProc/size=16          247B ± 2%      153B ± 0%    -37.97%  (p=0.100 n=3+3)
CacheNoHitProc/size=16-4        194B ± 0%      153B ± 0%    -21.27%  (p=0.100 n=3+3)
CacheNoHitProc/size=128         242B ± 4%      153B ± 0%    -36.78%  (p=0.100 n=3+3)
CacheNoHitProc/size=128-4       193B ± 0%      153B ± 0%    -20.86%  (p=0.100 n=3+3)
CacheNoHitProc/size=512         240B ± 2%      153B ± 0%    -36.48%  (p=0.100 n=3+3)
CacheNoHitProc/size=512-4       190B ± 1%      153B ± 0%    -19.33%  (p=0.100 n=3+3)
CacheNoHitProc/size=4096        245B ± 2%      153B ± 0%    -37.55%  (p=0.100 n=3+3)
CacheNoHitProc/size=4096-4      181B ± 2%      153B ± 0%    -15.47%  (p=0.100 n=3+3)
CacheHitProc/size=0            4.33B ±15%   152.00B ± 0%  +3407.69%  (p=0.100 n=3+3)
CacheHitProc/size=0-4          3.33B ±50%   152.00B ± 0%  +4460.00%  (p=0.100 n=3+3)
CacheHitProc/size=16           0.00B          0.00B            ~     (all equal)
CacheHitProc/size=16-4         0.00B          0.00B            ~     (all equal)
CacheHitProc/size=128          0.00B          0.00B            ~     (all equal)
CacheHitProc/size=128-4        0.00B          0.00B            ~     (all equal)
CacheHitProc/size=512          0.00B          0.00B            ~     (all equal)
CacheHitProc/size=512-4        0.00B          0.00B            ~     (all equal)
CacheHitProc/size=4096         0.00B          0.00B            ~     (all equal)
CacheHitProc/size=4096-4       0.00B          0.00B            ~     (all equal)

name                        old allocs/op  new allocs/op  delta
NoopStorage                     0.00           0.00            ~     (all equal)
NoopStorage-4                   0.00           0.00            ~     (all equal)
CacheStartup                    5.00 ± 0%      5.00 ± 0%       ~     (all equal)
CacheStartup-4                  5.00 ± 0%      5.00 ± 0%       ~     (all equal)
CacheNoHit/size=0               3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=0-4             3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=16              3.33 ±20%      3.00 ± 0%       ~     (p=1.000 n=3+3)
CacheNoHit/size=16-4            3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=128             3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=128-4           3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=512             3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=512-4           3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=4096            3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHit/size=4096-4          3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheHit/size=0                 0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHit/size=0-4               0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHit/size=16                0.00           0.00            ~     (all equal)
CacheHit/size=16-4              0.00           0.00            ~     (all equal)
CacheHit/size=128               0.00           0.00            ~     (all equal)
CacheHit/size=128-4             0.00           0.00            ~     (all equal)
CacheHit/size=512               0.00           0.00            ~     (all equal)
CacheHit/size=512-4             0.00           0.00            ~     (all equal)
CacheHit/size=4096              0.00           0.00            ~     (all equal)
CacheHit/size=4096-4            0.00           0.00            ~     (all equal)
NoopStoragePar                  0.00           0.00            ~     (all equal)
NoopStoragePar-4                0.00           0.00            ~     (all equal)
CacheStartupPar                 5.00 ± 0%      5.00 ± 0%       ~     (all equal)
CacheStartupPar-4               4.33 ±15%      4.00 ± 0%       ~     (p=1.000 n=3+3)
CacheNoHitPar/size=0            3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=0-4          3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=16           3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=16-4         3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=128          3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=128-4        3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=512          3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=512-4        3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=4096         3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitPar/size=4096-4       3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheHitPar/size=0              0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHitPar/size=0-4            0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHitPar/size=16             0.00           0.00            ~     (all equal)
CacheHitPar/size=16-4           0.00           0.00            ~     (all equal)
CacheHitPar/size=128            0.00           0.00            ~     (all equal)
CacheHitPar/size=128-4          0.00           0.00            ~     (all equal)
CacheHitPar/size=512            0.00           0.00            ~     (all equal)
CacheHitPar/size=512-4          0.00           0.00            ~     (all equal)
CacheHitPar/size=4096           0.00           0.00            ~     (all equal)
CacheHitPar/size=4096-4         0.00           0.00            ~     (all equal)
NoopStorageProc                 0.00           0.00            ~     (all equal)
NoopStorageProc-4               0.00           0.00            ~     (all equal)
CacheStartupProc                5.00 ± 0%      5.00 ± 0%       ~     (all equal)
CacheStartupProc-4              5.00 ± 0%      5.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=0           3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=0-4         3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=16          3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=16-4        3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=128         3.33 ±20%      3.00 ± 0%       ~     (p=1.000 n=3+3)
CacheNoHitProc/size=128-4       3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=512         3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=512-4       3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheNoHitProc/size=4096        3.33 ±20%      3.00 ± 0%       ~     (p=1.000 n=3+3)
CacheNoHitProc/size=4096-4      3.00 ± 0%      3.00 ± 0%       ~     (all equal)
CacheHitProc/size=0             0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHitProc/size=0-4           0.00           3.00 ± 0%      +Inf%  (p=0.100 n=3+3)
CacheHitProc/size=16            0.00           0.00            ~     (all equal)
CacheHitProc/size=16-4          0.00           0.00            ~     (all equal)
CacheHitProc/size=128           0.00           0.00            ~     (all equal)
CacheHitProc/size=128-4         0.00           0.00            ~     (all equal)
CacheHitProc/size=512           0.00           0.00            ~     (all equal)
CacheHitProc/size=512-4         0.00           0.00            ~     (all equal)
CacheHitProc/size=4096          0.00           0.00            ~     (all equal)
CacheHitProc/size=4096-4        0.00           0.00            ~     (all equal)
parent a5d0e83f
...@@ -47,9 +47,6 @@ type Cache struct { ...@@ -47,9 +47,6 @@ type Cache struct {
entryMap map[Oid]*oidCacheEntry // oid -> oid's cache entries entryMap map[Oid]*oidCacheEntry // oid -> oid's cache entries
// garbage collection:
gcCh chan struct{} // signals gc to run
gcMu sync.Mutex gcMu sync.Mutex
lru lruHead // revCacheEntries in LRU order lru lruHead // revCacheEntries in LRU order
size int // cached data size in bytes size int // cached data size in bytes
...@@ -129,34 +126,20 @@ func NewCache(loader StorLoader, sizeMax int) *Cache { ...@@ -129,34 +126,20 @@ func NewCache(loader StorLoader, sizeMax int) *Cache {
c := &Cache{ c := &Cache{
loader: loader, loader: loader,
entryMap: make(map[Oid]*oidCacheEntry), entryMap: make(map[Oid]*oidCacheEntry),
gcCh: make(chan struct{}, 1), // 1 is important - see gcsignal
sizeMax: sizeMax, sizeMax: sizeMax,
} }
c.lru.Init() c.lru.Init()
go c.gcmain()
return c return c
} }
// SetSizeMax adjusts how much RAM cache can use for cached data. // SetSizeMax adjusts how much RAM cache can use for cached data.
func (c *Cache) SetSizeMax(sizeMax int) { func (c *Cache) SetSizeMax(sizeMax int) {
gcrun := false
c.gcMu.Lock() c.gcMu.Lock()
c.sizeMax = sizeMax c.sizeMax = sizeMax
if c.size > c.sizeMax { if c.size > c.sizeMax {
gcrun = true c.gc()
} }
c.gcMu.Unlock() c.gcMu.Unlock()
if gcrun {
c.gcsignal()
}
}
// Close stops cache operation. It is illegal to use cache in any way after call to Close.
// XXX temp - will be gone
func (c *Cache) Close() {
close(c.gcCh)
} }
// Load loads data from database via cache. // Load loads data from database via cache.
...@@ -403,7 +386,6 @@ func (c *Cache) loadRCE(ctx context.Context, rce *revCacheEntry) { ...@@ -403,7 +386,6 @@ func (c *Cache) loadRCE(ctx context.Context, rce *revCacheEntry) {
// update lru & cache size // update lru & cache size
gcrun := false
c.gcMu.Lock() c.gcMu.Lock()
if rcePrevDropped != nil { if rcePrevDropped != nil {
...@@ -414,13 +396,10 @@ func (c *Cache) loadRCE(ctx context.Context, rce *revCacheEntry) { ...@@ -414,13 +396,10 @@ func (c *Cache) loadRCE(ctx context.Context, rce *revCacheEntry) {
} }
c.size += δsize c.size += δsize
if c.size > c.sizeMax { if c.size > c.sizeMax {
gcrun = true c.gc()
} }
c.gcMu.Unlock() c.gcMu.Unlock()
if gcrun {
c.gcsignal()
}
} }
// tryMerge tries to merge rce prev into next // tryMerge tries to merge rce prev into next
...@@ -486,38 +465,12 @@ func tryMerge(prev, next, cur *revCacheEntry) bool { ...@@ -486,38 +465,12 @@ func tryMerge(prev, next, cur *revCacheEntry) bool {
// ---- garbage collection ---- // ---- garbage collection ----
// gcsignal tells cache gc to run
func (c *Cache) gcsignal() {
select {
case c.gcCh <- struct{}{}:
// ok
default:
// also ok - .gcCh is created with size 1 so if we could not
// put something to it - there is already 1 element in there
// and so gc will get signal to run.
}
}
// gcmain is the process that cleans cache by evicting less-needed entries.
func (c *Cache) gcmain() {
for {
select {
case _, ok := <-c.gcCh:
// end of operation
if !ok {
return
}
// someone asks us to run GC
c.gc()
}
}
}
//trace:event traceCacheGCStart(c *Cache) //trace:event traceCacheGCStart(c *Cache)
//trace:event traceCacheGCFinish(c *Cache) //trace:event traceCacheGCFinish(c *Cache)
// gc performs garbage-collection // gc performs garbage-collection.
//
// must be called with .gcMu locked.
func (c *Cache) gc() { func (c *Cache) gc() {
traceCacheGCStart(c) traceCacheGCStart(c)
defer traceCacheGCFinish(c) defer traceCacheGCFinish(c)
...@@ -525,9 +478,7 @@ func (c *Cache) gc() { ...@@ -525,9 +478,7 @@ func (c *Cache) gc() {
//defer fmt.Printf("< gc\n") //defer fmt.Printf("< gc\n")
for { for {
c.gcMu.Lock()
if c.size <= c.sizeMax { if c.size <= c.sizeMax {
c.gcMu.Unlock()
return return
} }
...@@ -562,7 +513,6 @@ func (c *Cache) gc() { ...@@ -562,7 +513,6 @@ func (c *Cache) gc() {
oce.Unlock() oce.Unlock()
h.Delete() h.Delete()
c.gcMu.Unlock()
if oceFree { if oceFree {
c.mu.Lock() c.mu.Lock()
......
...@@ -584,7 +584,7 @@ func TestCache(t *testing.T) { ...@@ -584,7 +584,7 @@ func TestCache(t *testing.T) {
// and still loading works (because even if though rce's are evicted // and still loading works (because even if though rce's are evicted
// they stay live while someone user waits and uses it) // they stay live while someone user waits and uses it)
checkLoad(xidat(1,4), b(hello), 4, nil) go checkLoad(xidat(1,4), b(hello), 4, nil)
tc.Expect(gcstart, gcfinish) tc.Expect(gcstart, gcfinish)
checkOIDV() checkOIDV()
checkMRU(0) checkMRU(0)
...@@ -703,7 +703,6 @@ func benchEachCache(b *testing.B, f func(b *testing.B, c *Cache)) { ...@@ -703,7 +703,6 @@ func benchEachCache(b *testing.B, f func(b *testing.B, c *Cache)) {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) { b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
c := NewCache(s, size) c := NewCache(s, size)
f(b, c) f(b, c)
c.Close()
}) })
} }
} }
...@@ -714,7 +713,6 @@ func BenchmarkCacheStartup(b *testing.B) { ...@@ -714,7 +713,6 @@ func BenchmarkCacheStartup(b *testing.B) {
c := NewCache(s, b.N) c := NewCache(s, b.N)
benchLoad(b, c, b.N) benchLoad(b, c, b.N)
b.StopTimer() b.StopTimer()
c.Close()
} }
// Serially benchmark cache overhead - the additional time cache adds for loading not-yet-cached entries. // Serially benchmark cache overhead - the additional time cache adds for loading not-yet-cached entries.
...@@ -770,7 +768,6 @@ func BenchmarkCacheStartupPar(b *testing.B) { ...@@ -770,7 +768,6 @@ func BenchmarkCacheStartupPar(b *testing.B) {
c := NewCache(s, b.N) c := NewCache(s, b.N)
benchLoadPar(b, c, b.N) benchLoadPar(b, c, b.N)
b.StopTimer() b.StopTimer()
c.Close()
} }
func BenchmarkCacheNoHitPar(b *testing.B) { func BenchmarkCacheNoHitPar(b *testing.B) {
...@@ -833,7 +830,6 @@ func BenchmarkCacheStartupProc(b *testing.B) { ...@@ -833,7 +830,6 @@ func BenchmarkCacheStartupProc(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
// XXX stop timer // XXX stop timer
c.Close()
}) })
} }
...@@ -844,7 +840,6 @@ func benchEachCacheProc(b *testing.B, f func(b *testing.B, pb *testing.PB, c *Ca ...@@ -844,7 +840,6 @@ func benchEachCacheProc(b *testing.B, f func(b *testing.B, pb *testing.PB, c *Ca
s := &noopStorage{} s := &noopStorage{}
c := NewCache(s, size) c := NewCache(s, size)
err := f(b, pb, c) err := f(b, pb, c)
c.Close()
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment