Coverage Report

Created: 2023-03-01 07:33

/src/spirv-tools/source/util/small_vector.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2018 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef SOURCE_UTIL_SMALL_VECTOR_H_
16
#define SOURCE_UTIL_SMALL_VECTOR_H_
17
18
#include <cassert>
19
#include <iostream>
20
#include <memory>
21
#include <utility>
22
#include <vector>
23
24
#include "source/util/make_unique.h"
25
26
namespace spvtools {
27
namespace utils {
28
29
// The |SmallVector| class is intended to be a drop-in replacement for
30
// |std::vector|.  The difference is in the implementation. A |SmallVector| is
31
// optimized for when the number of elements in the vector are small.  Small is
32
// defined by the template parameter |small_size|.
33
//
34
// Note that |SmallVector| is not always faster than an |std::vector|, so you
35
// should experiment with different values for |small_size| and compare to
36
// using and |std::vector|.
37
//
38
// TODO: I have implemented the public member functions from |std::vector| that
39
// I needed.  If others are needed they should be implemented. Do not implement
40
// public member functions that are not defined by std::vector.
41
template <class T, size_t small_size>
42
class SmallVector {
43
 public:
44
  using iterator = T*;
45
  using const_iterator = const T*;
46
47
  SmallVector()
48
      : size_(0),
49
        small_data_(reinterpret_cast<T*>(buffer)),
50
87.0M
        large_data_(nullptr) {}
spvtools::utils::SmallVector<unsigned int, 2ul>::SmallVector()
Line
Count
Source
50
86.2M
        large_data_(nullptr) {}
spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::SmallVector()
Line
Count
Source
50
774k
        large_data_(nullptr) {}
51
52
30.7M
  SmallVector(const SmallVector& that) : SmallVector() { *this = that; }
53
54
25.9M
  SmallVector(SmallVector&& that) : SmallVector() { *this = std::move(that); }
55
56
149k
  SmallVector(const std::vector<T>& vec) : SmallVector() {
57
149k
    if (vec.size() > small_size) {
58
57
      large_data_ = MakeUnique<std::vector<T>>(vec);
59
149k
    } else {
60
149k
      size_ = vec.size();
61
298k
      for (uint32_t i = 0; i < size_; i++) {
62
149k
        new (small_data_ + i) T(vec[i]);
63
149k
      }
64
149k
    }
65
149k
  }
66
67
  template <class InputIt>
68
12.4M
  SmallVector(InputIt first, InputIt last) : SmallVector() {
69
12.4M
    insert(end(), first, last);
70
12.4M
  }
71
72
0
  SmallVector(std::vector<T>&& vec) : SmallVector() {
73
0
    if (vec.size() > small_size) {
74
0
      large_data_ = MakeUnique<std::vector<T>>(std::move(vec));
75
0
    } else {
76
0
      size_ = vec.size();
77
0
      for (uint32_t i = 0; i < size_; i++) {
78
0
        new (small_data_ + i) T(std::move(vec[i]));
79
0
      }
80
0
    }
81
0
    vec.clear();
82
0
  }
83
84
16.9M
  SmallVector(std::initializer_list<T> init_list) : SmallVector() {
85
16.9M
    if (init_list.size() < small_size) {
86
33.8M
      for (auto it = init_list.begin(); it != init_list.end(); ++it) {
87
16.9M
        new (small_data_ + (size_++)) T(std::move(*it));
88
16.9M
      }
89
16.9M
    } else {
90
0
      large_data_ = MakeUnique<std::vector<T>>(std::move(init_list));
91
0
    }
92
16.9M
  }
93
94
  SmallVector(size_t s, const T& v) : SmallVector() { resize(s, v); }
95
96
87.0M
  virtual ~SmallVector() {
97
141M
    for (T* p = small_data_; p < small_data_ + size_; ++p) {
98
54.5M
      p->~T();
99
54.5M
    }
100
87.0M
  }
spvtools::utils::SmallVector<unsigned int, 2ul>::~SmallVector()
Line
Count
Source
96
86.2M
  virtual ~SmallVector() {
97
140M
    for (T* p = small_data_; p < small_data_ + size_; ++p) {
98
54.5M
      p->~T();
99
54.5M
    }
100
86.2M
  }
spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::~SmallVector()
Line
Count
Source
96
774k
  virtual ~SmallVector() {
97
774k
    for (T* p = small_data_; p < small_data_ + size_; ++p) {
98
0
      p->~T();
99
0
    }
100
774k
  }
101
102
30.7M
  SmallVector& operator=(const SmallVector& that) {
103
30.7M
    assert(small_data_);
104
30.7M
    if (that.large_data_) {
105
28.0k
      if (large_data_) {
106
0
        *large_data_ = *that.large_data_;
107
28.0k
      } else {
108
28.0k
        large_data_ = MakeUnique<std::vector<T>>(*that.large_data_);
109
28.0k
      }
110
30.7M
    } else {
111
30.7M
      large_data_.reset(nullptr);
112
30.7M
      size_t i = 0;
113
      // Do a copy for any element in |this| that is already constructed.
114
30.7M
      for (; i < size_ && i < that.size_; ++i) {
115
174
        small_data_[i] = that.small_data_[i];
116
174
      }
117
118
30.7M
      if (i >= that.size_) {
119
        // If the size of |this| becomes smaller after the assignment, then
120
        // destroy any extra elements.
121
174
        for (; i < size_; ++i) {
122
0
          small_data_[i].~T();
123
0
        }
124
30.7M
      } else {
125
        // If the size of |this| becomes larger after the assignement, copy
126
        // construct the new elements that are needed.
127
61.5M
        for (; i < that.size_; ++i) {
128
30.7M
          new (small_data_ + i) T(that.small_data_[i]);
129
30.7M
        }
130
30.7M
      }
131
30.7M
      size_ = that.size_;
132
30.7M
    }
133
30.7M
    return *this;
134
30.7M
  }
135
136
31.7M
  SmallVector& operator=(SmallVector&& that) {
137
31.7M
    if (that.large_data_) {
138
209
      large_data_.reset(that.large_data_.release());
139
31.7M
    } else {
140
31.7M
      large_data_.reset(nullptr);
141
31.7M
      size_t i = 0;
142
      // Do a move for any element in |this| that is already constructed.
143
37.5M
      for (; i < size_ && i < that.size_; ++i) {
144
5.82M
        small_data_[i] = std::move(that.small_data_[i]);
145
5.82M
      }
146
147
31.7M
      if (i >= that.size_) {
148
        // If the size of |this| becomes smaller after the assignment, then
149
        // destroy any extra elements.
150
5.82M
        for (; i < size_; ++i) {
151
0
          small_data_[i].~T();
152
0
        }
153
25.9M
      } else {
154
        // If the size of |this| becomes larger after the assignement, move
155
        // construct the new elements that are needed.
156
51.9M
        for (; i < that.size_; ++i) {
157
25.9M
          new (small_data_ + i) T(std::move(that.small_data_[i]));
158
25.9M
        }
159
25.9M
      }
160
31.7M
      size_ = that.size_;
161
31.7M
    }
162
163
    // Reset |that| because all of the data has been moved to |this|.
164
31.7M
    that.DestructSmallData();
165
31.7M
    return *this;
166
31.7M
  }
167
168
  template <class OtherVector>
169
1.19M
  friend bool operator==(const SmallVector& lhs, const OtherVector& rhs) {
170
1.19M
    if (lhs.size() != rhs.size()) {
171
14
      return false;
172
14
    }
173
174
1.19M
    auto rit = rhs.begin();
175
2.13M
    for (auto lit = lhs.begin(); lit != lhs.end(); ++lit, ++rit) {
176
1.19M
      if (*lit != *rit) {
177
257k
        return false;
178
257k
      }
179
1.19M
    }
180
940k
    return true;
181
1.19M
  }
bool spvtools::utils::operator==<spvtools::utils::SmallVector<unsigned int, 2ul> >(spvtools::utils::SmallVector<unsigned int, 2ul> const&, spvtools::utils::SmallVector<unsigned int, 2ul> const&)
Line
Count
Source
169
1.19M
  friend bool operator==(const SmallVector& lhs, const OtherVector& rhs) {
170
1.19M
    if (lhs.size() != rhs.size()) {
171
14
      return false;
172
14
    }
173
174
1.19M
    auto rit = rhs.begin();
175
2.13M
    for (auto lit = lhs.begin(); lit != lhs.end(); ++lit, ++rit) {
176
1.19M
      if (*lit != *rit) {
177
257k
        return false;
178
257k
      }
179
1.19M
    }
180
940k
    return true;
181
1.19M
  }
Unexecuted instantiation: bool spvtools::utils::operator==<std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > >(spvtools::utils::SmallVector<unsigned int, 2ul> const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&)
182
183
// Avoid infinite recursion from rewritten operators in C++20
184
#if __cplusplus <= 201703L
185
  friend bool operator==(const std::vector<T>& lhs, const SmallVector& rhs) {
186
    return rhs == lhs;
187
  }
188
#endif
189
190
0
  friend bool operator!=(const SmallVector& lhs, const std::vector<T>& rhs) {
191
0
    return !(lhs == rhs);
192
0
  }
193
194
  friend bool operator!=(const std::vector<T>& lhs, const SmallVector& rhs) {
195
    return rhs != lhs;
196
  }
197
198
24.2M
  T& operator[](size_t i) {
199
24.2M
    if (!large_data_) {
200
24.2M
      return small_data_[i];
201
24.2M
    } else {
202
0
      return (*large_data_)[i];
203
0
    }
204
24.2M
  }
205
206
709M
  const T& operator[](size_t i) const {
207
709M
    if (!large_data_) {
208
709M
      return small_data_[i];
209
709M
    } else {
210
0
      return (*large_data_)[i];
211
0
    }
212
709M
  }
213
214
624M
  size_t size() const {
215
624M
    if (!large_data_) {
216
624M
      return size_;
217
624M
    } else {
218
13.7k
      return large_data_->size();
219
13.7k
    }
220
624M
  }
221
222
26.7M
  iterator begin() {
223
26.7M
    if (large_data_) {
224
49.2k
      return large_data_->data();
225
26.7M
    } else {
226
26.7M
      return small_data_;
227
26.7M
    }
228
26.7M
  }
spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::begin()
Line
Count
Source
222
1.79M
  iterator begin() {
223
1.79M
    if (large_data_) {
224
0
      return large_data_->data();
225
1.79M
    } else {
226
1.79M
      return small_data_;
227
1.79M
    }
228
1.79M
  }
spvtools::utils::SmallVector<unsigned int, 2ul>::begin()
Line
Count
Source
222
24.9M
  iterator begin() {
223
24.9M
    if (large_data_) {
224
49.2k
      return large_data_->data();
225
24.9M
    } else {
226
24.9M
      return small_data_;
227
24.9M
    }
228
24.9M
  }
229
230
15.7M
  const_iterator begin() const {
231
15.7M
    if (large_data_) {
232
710k
      return large_data_->data();
233
14.9M
    } else {
234
14.9M
      return small_data_;
235
14.9M
    }
236
15.7M
  }
237
238
716k
  const_iterator cbegin() const { return begin(); }
239
240
28.5M
  iterator end() {
241
28.5M
    if (large_data_) {
242
0
      return large_data_->data() + large_data_->size();
243
28.5M
    } else {
244
28.5M
      return small_data_ + size_;
245
28.5M
    }
246
28.5M
  }
spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::end()
Line
Count
Source
240
3.59M
  iterator end() {
241
3.59M
    if (large_data_) {
242
0
      return large_data_->data() + large_data_->size();
243
3.59M
    } else {
244
3.59M
      return small_data_ + size_;
245
3.59M
    }
246
3.59M
  }
spvtools::utils::SmallVector<unsigned int, 2ul>::end()
Line
Count
Source
240
24.9M
  iterator end() {
241
24.9M
    if (large_data_) {
242
0
      return large_data_->data() + large_data_->size();
243
24.9M
    } else {
244
24.9M
      return small_data_ + size_;
245
24.9M
    }
246
24.9M
  }
247
248
15.4M
  const_iterator end() const {
249
15.4M
    if (large_data_) {
250
710k
      return large_data_->data() + large_data_->size();
251
14.7M
    } else {
252
14.7M
      return small_data_ + size_;
253
14.7M
    }
254
15.4M
  }
255
256
716k
  const_iterator cend() const { return end(); }
257
258
  T* data() { return begin(); }
259
260
  const T* data() const { return cbegin(); }
261
262
  T& front() { return (*this)[0]; }
263
264
618M
  const T& front() const { return (*this)[0]; }
265
266
  iterator erase(const_iterator pos) { return erase(pos, pos + 1); }
267
268
  iterator erase(const_iterator first, const_iterator last) {
269
    if (large_data_) {
270
      size_t start_index = first - large_data_->data();
271
      size_t end_index = last - large_data_->data();
272
      auto r = large_data_->erase(large_data_->begin() + start_index,
273
                                  large_data_->begin() + end_index);
274
      return large_data_->data() + (r - large_data_->begin());
275
    }
276
277
    // Since C++11, std::vector has |const_iterator| for the parameters, so I
278
    // follow that.  However, I need iterators to modify the current container,
279
    // which is not const.  This is why I cast away the const.
280
    iterator f = const_cast<iterator>(first);
281
    iterator l = const_cast<iterator>(last);
282
    iterator e = end();
283
284
    size_t num_of_del_elements = last - first;
285
    iterator ret = f;
286
    if (first == last) {
287
      return ret;
288
    }
289
290
    // Move |last| and any elements after it their earlier position.
291
    while (l != e) {
292
      *f = std::move(*l);
293
      ++f;
294
      ++l;
295
    }
296
297
    // Destroy the elements that were supposed to be deleted.
298
    while (f != l) {
299
      f->~T();
300
      ++f;
301
    }
302
303
    // Update the size.
304
    size_ -= num_of_del_elements;
305
    return ret;
306
  }
307
308
0
  void push_back(const T& value) {
309
0
    if (!large_data_ && size_ == small_size) {
310
0
      MoveToLargeData();
311
0
    }
312
313
0
    if (large_data_) {
314
0
      large_data_->push_back(value);
315
0
      return;
316
0
    }
317
318
0
    new (small_data_ + size_) T(value);
319
0
    ++size_;
320
0
  }
321
322
1.79M
  void push_back(T&& value) {
323
1.79M
    if (!large_data_ && size_ == small_size) {
324
0
      MoveToLargeData();
325
0
    }
326
327
1.79M
    if (large_data_) {
328
0
      large_data_->push_back(std::move(value));
329
0
      return;
330
0
    }
331
332
1.79M
    new (small_data_ + size_) T(std::move(value));
333
1.79M
    ++size_;
334
1.79M
  }
335
336
1.79M
  void pop_back() {
337
1.79M
    if (large_data_) {
338
0
      large_data_->pop_back();
339
1.79M
    } else {
340
1.79M
      --size_;
341
1.79M
      small_data_[size_].~T();
342
1.79M
    }
343
1.79M
  }
344
345
  template <class InputIt>
346
12.4M
  iterator insert(iterator pos, InputIt first, InputIt last) {
347
12.4M
    size_t element_idx = (pos - begin());
348
12.4M
    size_t num_of_new_elements = std::distance(first, last);
349
12.4M
    size_t new_size = size_ + num_of_new_elements;
350
12.4M
    if (!large_data_ && new_size > small_size) {
351
49.2k
      MoveToLargeData();
352
49.2k
    }
353
354
12.4M
    if (large_data_) {
355
49.2k
      typename std::vector<T>::iterator new_pos =
356
49.2k
          large_data_->begin() + element_idx;
357
49.2k
      large_data_->insert(new_pos, first, last);
358
49.2k
      return begin() + element_idx;
359
49.2k
    }
360
361
    // Move |pos| and all of the elements after it over |num_of_new_elements|
362
    // places.  We start at the end and work backwards, to make sure we do not
363
    // overwrite data that we have not moved yet.
364
12.4M
    for (iterator i = begin() + new_size - 1, j = end() - 1; j >= pos;
365
12.4M
         --i, --j) {
366
0
      if (i >= begin() + size_) {
367
0
        new (i) T(std::move(*j));
368
0
      } else {
369
0
        *i = std::move(*j);
370
0
      }
371
0
    }
372
373
    // Copy the new elements into position.
374
12.4M
    iterator p = pos;
375
24.9M
    for (; first != last; ++p, ++first) {
376
12.5M
      if (p >= small_data_ + size_) {
377
12.5M
        new (p) T(*first);
378
12.5M
      } else {
379
0
        *p = *first;
380
0
      }
381
12.5M
    }
382
383
    // Update the size.
384
12.4M
    size_ += num_of_new_elements;
385
12.4M
    return pos;
386
12.4M
  }
387
388
  bool empty() const {
389
    if (large_data_) {
390
      return large_data_->empty();
391
    }
392
    return size_ == 0;
393
  }
394
395
  void clear() {
396
    if (large_data_) {
397
      large_data_->clear();
398
    } else {
399
      DestructSmallData();
400
    }
401
  }
402
403
  template <class... Args>
404
  void emplace_back(Args&&... args) {
405
    if (!large_data_ && size_ == small_size) {
406
      MoveToLargeData();
407
    }
408
409
    if (large_data_) {
410
      large_data_->emplace_back(std::forward<Args>(args)...);
411
    } else {
412
      new (small_data_ + size_) T(std::forward<Args>(args)...);
413
      ++size_;
414
    }
415
  }
416
417
  void resize(size_t new_size, const T& v) {
418
    if (!large_data_ && new_size > small_size) {
419
      MoveToLargeData();
420
    }
421
422
    if (large_data_) {
423
      large_data_->resize(new_size, v);
424
      return;
425
    }
426
427
    // If |new_size| < |size_|, then destroy the extra elements.
428
    for (size_t i = new_size; i < size_; ++i) {
429
      small_data_[i].~T();
430
    }
431
432
    // If |new_size| > |size_|, the copy construct the new elements.
433
    for (size_t i = size_; i < new_size; ++i) {
434
      new (small_data_ + i) T(v);
435
    }
436
437
    // Update the size.
438
    size_ = new_size;
439
  }
440
441
 private:
442
  // Moves all of the element from |small_data_| into a new std::vector that can
443
  // be access through |large_data|.
444
49.2k
  void MoveToLargeData() {
445
49.2k
    assert(!large_data_);
446
0
    large_data_ = MakeUnique<std::vector<T>>();
447
49.2k
    for (size_t i = 0; i < size_; ++i) {
448
0
      large_data_->emplace_back(std::move(small_data_[i]));
449
0
    }
450
49.2k
    DestructSmallData();
451
49.2k
  }
Unexecuted instantiation: spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::MoveToLargeData()
spvtools::utils::SmallVector<unsigned int, 2ul>::MoveToLargeData()
Line
Count
Source
444
49.2k
  void MoveToLargeData() {
445
49.2k
    assert(!large_data_);
446
0
    large_data_ = MakeUnique<std::vector<T>>();
447
49.2k
    for (size_t i = 0; i < size_; ++i) {
448
0
      large_data_->emplace_back(std::move(small_data_[i]));
449
0
    }
450
49.2k
    DestructSmallData();
451
49.2k
  }
452
453
  // Destroys all of the elements in |small_data_| that have been constructed.
454
31.8M
  void DestructSmallData() {
455
63.5M
    for (size_t i = 0; i < size_; ++i) {
456
31.7M
      small_data_[i].~T();
457
31.7M
    }
458
31.8M
    size_ = 0;
459
31.8M
  }
spvtools::utils::SmallVector<unsigned int, 2ul>::DestructSmallData()
Line
Count
Source
454
31.8M
  void DestructSmallData() {
455
63.5M
    for (size_t i = 0; i < size_; ++i) {
456
31.7M
      small_data_[i].~T();
457
31.7M
    }
458
31.8M
    size_ = 0;
459
31.8M
  }
Unexecuted instantiation: spvtools::utils::SmallVector<spvtools::opt::analysis::Type const*, 8ul>::DestructSmallData()
460
461
  // The number of elements in |small_data_| that have been constructed.
462
  size_t size_;
463
464
  // The pointed used to access the array of elements when the number of
465
  // elements is small.
466
  T* small_data_;
467
468
  // The actual data used to store the array elements.  It must never be used
469
  // directly, but must only be accessed through |small_data_|.
470
  typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type
471
      buffer[small_size];
472
473
  // A pointer to a vector that is used to store the elements of the vector when
474
  // this size exceeds |small_size|.  If |large_data_| is nullptr, then the data
475
  // is stored in |small_data_|.  Otherwise, the data is stored in
476
  // |large_data_|.
477
  std::unique_ptr<std::vector<T>> large_data_;
478
};  // namespace utils
479
480
}  // namespace utils
481
}  // namespace spvtools
482
483
#endif  // SOURCE_UTIL_SMALL_VECTOR_H_