YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/common/id_mapping.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_COMMON_ID_MAPPING_H
33
#define YB_COMMON_ID_MAPPING_H
34
35
#include <vector>
36
37
#include <glog/logging.h>
38
39
namespace yb {
40
41
// Light-weight hashtable implementation for mapping a small number of
42
// integers to other integers.
43
// This is used by Schema to map from Column ID to column index.
44
//
45
// The implementation is an open-addressed hash table with linear probing.
46
// The probing is limited to look only in the initial position and a single
47
// position following. If neither position is free, the hashtable is doubled.
48
// Therefore, the fill rate of the hashtable could be fairly bad, in the worst
49
// case. However, in practice, we expect that most tables will have nearly
50
// sequential column IDs (with only the occasional gap if a column has been removed).
51
// Therefore, we expect to have very few collisions.
52
//
53
// The implementation takes care to only use power-of-2 sized bucket arrays so that
54
// modulo can be calculated using bit masking. This improves performance substantially
55
// since the 'div' instruction can take many cycles.
56
//
57
// NOTE: this map restricts that keys and values are positive. '-1' is used
58
// as a special identifier indicating that the slot is unused or that the key
59
// was not found.
60
class IdMapping {
61
 private:
62
  enum {
63
    kInitialCapacity = 64,
64
    kNumProbes = 2
65
  };
66
  typedef std::pair<int, int> value_type;
67
68
 public:
69
  static const int kNoEntry;
70
71
  IdMapping() :
72
    mask_(kInitialCapacity - 1),
73
45.4M
    entries_(kInitialCapacity) {
74
45.4M
    clear();
75
45.4M
  }
76
77
  explicit IdMapping(const IdMapping& other)
78
  : mask_(other.mask_),
79
0
    entries_(other.entries_) {
80
0
  }
81
82
43.9M
  ~IdMapping() {}
83
84
85.3M
  void clear() {
85
85.3M
    ClearMap(&entries_);
86
85.3M
  }
87
88
  // NOLINT on this function definition because it thinks we're calling
89
  // std::swap instead of defining it.
90
2
  void swap(IdMapping& other) { // NOLINT(*)
91
2
    uint64_t tmp = other.mask_;
92
2
    other.mask_ = mask_;
93
2
    mask_ = tmp;
94
2
    other.entries_.swap(entries_);
95
2
  }
96
97
2.57M
  IdMapping& operator=(const IdMapping& other) {
98
2.57M
    mask_ = other.mask_;
99
2.57M
    entries_ = other.entries_;
100
2.57M
    return *this;
101
2.57M
  }
102
103
178M
  int operator[](int key) const {
104
178M
    return get(key);
105
178M
  }
106
107
178M
  int get(int key) const {
108
178M
    DCHECK_GE(key, 0);
109
178M
    for (int i = 0; i < kNumProbes; 
i++23.5k
) {
110
178M
      int s = slot(key + i);
111
178M
      if (entries_[s].first == key || 
entries_[s].first == kNoEntry2.77k
) {
112
178M
        return entries_[s].second;
113
178M
      }
114
178M
    }
115
9.43k
    return kNoEntry;
116
178M
  }
117
118
83.0M
  void set(int key, int val) {
119
83.0M
    DCHECK_GE(key, 0);
120
83.0M
    DCHECK_GE(val, 0);
121
83.0M
    while (
true83.0M
) {
122
83.0M
      for (int i = 0; 
i < kNumProbes83.0M
;
i++1.99k
) {
123
83.0M
        int s = slot(key + i);
124
83.0M
        CHECK_NE
(entries_[s].first, key) << "Cannot insert duplicate keys"1
;
125
83.0M
        if (entries_[s].first == kNoEntry) {
126
83.0M
          entries_[s].first = key;
127
83.0M
          entries_[s].second = val;
128
83.0M
          return;
129
83.0M
        }
130
83.0M
      }
131
      // Didn't find a spot.
132
18.4E
      DoubleCapacity();
133
18.4E
    }
134
83.0M
  }
135
136
14
  size_t capacity() const {
137
14
    return mask_ + 1;
138
14
  }
139
140
  // Returns the memory usage of this object without the object itself. Should
141
  // be used when embedded inside another object.
142
  size_t memory_footprint_excluding_this() const;
143
144
  // Returns the memory usage of this object including the object itself.
145
  // Should be used when allocated on the heap.
146
  size_t memory_footprint_including_this() const;
147
148
 private:
149
261M
  int slot(int key) const {
150
261M
    return key & mask_;
151
261M
  }
152
153
14
  void DoubleCapacity() {
154
14
    auto new_capacity = capacity() * 2;
155
14
    std::vector<value_type> entries(new_capacity);
156
14
    ClearMap(&entries);
157
14
    mask_ = new_capacity - 1;
158
14
    entries.swap(entries_);
159
160
17.4k
    for (const auto& entry : entries) {
161
17.4k
      if (entry.first != kNoEntry) {
162
2.28k
        set(entry.first, entry.second);
163
2.28k
      }
164
17.4k
    }
165
14
  }
166
167
85.3M
  static void ClearMap(std::vector<value_type>* v) {
168
5.39G
    for (auto& entry : *v) {
169
5.39G
      entry = std::make_pair(kNoEntry, kNoEntry);
170
5.39G
    }
171
85.3M
  }
172
173
  uint64_t mask_;
174
  std::vector<value_type> entries_;
175
};
176
177
} // namespace yb
178
#endif /* YB_COMMON_ID_MAPPING_H */