Skip to content

Commit 7ec8d6c

Browse files
committed
perf: optimize the vector performance in favor of small_vector
Signed-off-by: c8ef <c8ef@outlook.com>
1 parent 883d4ea commit 7ec8d6c

6 files changed

Lines changed: 15 additions & 205 deletions

File tree

include/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ bm/bm_sim/queue.h \
8686
bm/bm_sim/queueing.h \
8787
bm/bm_sim/ras.h \
8888
bm/bm_sim/runtime_interface.h \
89-
bm/bm_sim/short_alloc.h \
9089
bm/bm_sim/stateful.h \
9190
bm/bm_sim/switch.h \
9291
bm/bm_sim/simple_pre.h \

include/bm/bm_sim/CPPLINT.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
exclude_files=nn\.h
6-
exclude_files=short_alloc\.h

include/bm/bm_sim/bytecontainer.h

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
#ifndef BM_BM_SIM_BYTECONTAINER_H_
1616
#define BM_BM_SIM_BYTECONTAINER_H_
1717

18-
#include <vector>
1918
#include <iterator>
2019
#include <string>
20+
#include <vector>
2121

22+
#include <boost/container/small_vector.hpp>
2223
#include <boost/functional/hash.hpp>
2324

24-
#include "short_alloc.h"
25-
2625
namespace bm {
2726

2827
//! This class is used everytime a vector of bytes is needed in bmv2. It is most
@@ -32,7 +31,7 @@ class ByteContainer {
3231
static constexpr size_t S = 16u;
3332
static_assert(sizeof(char) == 1, "");
3433
static_assert(alignof(char) == 1, "");
35-
using _vector = std::vector<char, detail::short_alloc<char, S, 1> >;
34+
using _vector = boost::container::small_vector<char, S>;
3635

3736
public:
3837
using iterator = _vector::iterator;
@@ -42,22 +41,19 @@ class ByteContainer {
4241
using size_type = size_t;
4342

4443
public:
45-
ByteContainer()
46-
: bytes(_a) {
47-
bytes.reserve(S);
48-
}
44+
ByteContainer() = default;
4945

5046
//! Constructs the container with \p nbytes copies of elements with value \p c
5147
explicit ByteContainer(const size_t nbytes, const char c = '\x00')
52-
: bytes(nbytes, c, _a) { }
48+
: bytes(nbytes, c) {}
5349

5450
//! Constructs the container by copying the bytes in vector \p bytes
55-
explicit ByteContainer(const std::vector<char> &bytes)
56-
: bytes(bytes.begin(), bytes.end(), _a) { }
51+
explicit ByteContainer(const std::vector<char>& bytes)
52+
: bytes(bytes.begin(), bytes.end()) {}
5753

5854
//! Constructs the container by copying the bytes in this byte array
59-
ByteContainer(const char *bytes, size_t nbytes)
60-
: bytes(bytes, bytes + nbytes, _a) { }
55+
ByteContainer(const char* bytes, size_t nbytes)
56+
: bytes(bytes, bytes + nbytes) {}
6157

6258
static char char2digit(char c) {
6359
if (c >= '0' && c <= '9')
@@ -72,8 +68,7 @@ class ByteContainer {
7268

7369
//! Constructs the container from a hexadecimal string. Parameter \p hexstring
7470
//! can optionally include the `0x` prefix.
75-
explicit ByteContainer(const std::string &hexstring)
76-
: bytes(_a) {
71+
explicit ByteContainer(const std::string& hexstring) {
7772
bytes.reserve(S);
7873
size_t idx = 0;
7974

@@ -97,19 +92,11 @@ class ByteContainer {
9792
}
9893
}
9994

100-
ByteContainer(const ByteContainer &other)
101-
: bytes(other.bytes, _a) { }
102-
103-
ByteContainer &operator=(const ByteContainer &other) {
104-
bytes.assign(other.begin(), other.end());
105-
return *this;
106-
}
107-
10895
//! Returns the number of bytes in the container
10996
size_type size() const noexcept { return bytes.size(); }
11097

11198
//! Clears the contents of the container
112-
void clear() { return bytes.clear(); }
99+
void clear() { bytes.clear(); }
113100

114101
// iterators
115102

@@ -265,7 +252,6 @@ class ByteContainer {
265252
}
266253

267254
private:
268-
_vector::allocator_type::arena_type _a;
269255
_vector bytes;
270256
};
271257

include/bm/bm_sim/short_alloc.h

Lines changed: 0 additions & 168 deletions
This file was deleted.

include/bm/bm_sim/short_alloc.h.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

include/bm/bm_sim/stateful.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
#include <functional>
3131
#include <mutex>
3232
#include <string>
33-
#include <unordered_set>
3433
#include <unordered_map>
34+
#include <unordered_set>
3535
#include <vector>
3636

37+
#include <boost/container/small_vector.hpp>
3738
#include <boost/thread/locks.hpp> // for boost::lock
3839

3940
#include "bignum.h"
4041
#include "data.h"
4142
#include "named_p4object.h"
42-
#include "short_alloc.h"
4343

4444
namespace bm {
4545

@@ -175,19 +175,16 @@ class RegisterSync {
175175
using Lock = RegisterArray::UniqueLock;
176176

177177
template <size_t NumLocks = 4>
178-
using LockVector = std::vector<
179-
Lock, ::detail::short_alloc<Lock, NumLocks * sizeof(Lock), alignof(Lock)> >;
178+
using LockVector = boost::container::small_vector<Lock, NumLocks>;
180179

181180
struct RegisterLocks {
182-
LockVector<>::allocator_type::arena_type a;
183-
LockVector<> v{a};
181+
LockVector<> v{};
184182
};
185183

186184
void add_register_array(const RegisterArray *register_array);
187185

188186
void merge_from(const RegisterSync &other);
189187

190-
// tried NRVO, but RegisterLocks not movable
191188
void lock(RegisterLocks *RL) const {
192189
for (auto m : mutexes) RL->v.emplace_back(*m, std::defer_lock);
193190
boost::lock(RL->v.begin(), RL->v.end());

0 commit comments

Comments
 (0)