• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.cache;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.common.cache.LocalCache.Segment;
23 import org.checkerframework.checker.nullness.qual.Nullable;
24 
25 /**
26  * Benchmark for {@code LocalCache.Segment.removeEntryFromChain}.
27  *
28  * @author Charles Fry
29  */
30 @SuppressWarnings("CheckReturnValue")
31 public class ChainBenchmark {
32 
33   @Param({"1", "2", "3", "4", "5", "6"})
34   int length;
35 
36   private Segment<Object, Object> segment;
37   private ReferenceEntry<Object, Object> head;
38   private @Nullable ReferenceEntry<Object, Object> chain;
39 
40   @SuppressWarnings("GuardedBy")
41   @BeforeExperiment
setUp()42   void setUp() {
43     LocalCache<Object, Object> cache =
44         new LocalCache<>(CacheBuilder.newBuilder().concurrencyLevel(1), null);
45     segment = cache.segments[0];
46     chain = null;
47     for (int i = 0; i < length; i++) {
48       Object key = new Object();
49       // TODO(b/145386688): This access should be guarded by 'this.segment', which is not currently
50       // held
51       chain = segment.newEntry(key, cache.hash(key), chain);
52       if (i == 0) {
53         head = chain;
54       }
55     }
56   }
57 
58   @SuppressWarnings("GuardedBy")
59   @Benchmark
time(int reps)60   int time(int reps) {
61     int dummy = 0;
62     for (int i = 0; i < reps; i++) {
63       // TODO(b/145386688): This access should be guarded by 'this.segment', which is not currently
64       // held
65       segment.removeEntryFromChain(chain, head);
66       dummy += segment.count;
67     }
68     return dummy;
69   }
70 }
71