Skip to content

Commit b8d4f49

Browse files
committed
improve performance of insert() and erase()
1 parent e97e10d commit b8d4f49

File tree

2 files changed

+7
-64
lines changed

2 files changed

+7
-64
lines changed

src/global/ImmOrderedSet.h

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,58 +70,25 @@ struct NODISCARD ImmOrderedSet final
7070
public:
7171
void erase(const Type id)
7272
{
73-
auto it = std::lower_bound(m_vector.begin(), m_vector.end(), id);
73+
const auto it = std::lower_bound(m_vector.begin(), m_vector.end(), id);
7474
if (it != m_vector.end() && *it == id) {
75-
size_t index = static_cast<size_t>(std::distance(m_vector.begin(), it));
76-
m_vector = m_vector.erase(index);
75+
const auto index = static_cast<size_t>(std::distance(m_vector.begin(), it));
76+
m_vector = std::move(m_vector).erase(index);
7777
}
7878
}
7979

8080
void insert(const Type id)
8181
{
82-
auto it = std::lower_bound(m_vector.begin(), m_vector.end(), id);
82+
const auto it = std::lower_bound(m_vector.begin(), m_vector.end(), id);
8383
if (it != m_vector.end() && *it == id) {
8484
return;
8585
}
86-
size_t index = static_cast<size_t>(std::distance(m_vector.begin(), it));
86+
const auto index = static_cast<size_t>(std::distance(m_vector.begin(), it));
8787
if (index == m_vector.size()) {
88-
m_vector = m_vector.push_back(id);
88+
m_vector = std::move(m_vector).push_back(id);
8989
return;
9090
}
91-
m_vector = m_vector.insert(index, id);
92-
}
93-
94-
public:
95-
void insertAll(const ImmOrderedSet &other)
96-
{
97-
if (other.empty() || this == &other) {
98-
return;
99-
}
100-
101-
auto transient = typename Vector::transient_type{};
102-
103-
auto this_it = this->begin();
104-
const auto this_end = this->end();
105-
auto other_it = other.begin();
106-
const auto other_end = other.end();
107-
108-
// Perform a sorted merge, adding unique elements from both vectors.
109-
while (this_it != this_end && other_it != other_end) {
110-
if (*this_it < *other_it) {
111-
transient.push_back(*this_it++);
112-
} else if (*other_it < *this_it) {
113-
transient.push_back(*other_it++);
114-
} else {
115-
transient.push_back(*this_it++);
116-
++other_it;
117-
}
118-
}
119-
120-
// Append any remaining elements from whichever vector was not fully consumed.
121-
immer::copy(this_it, this_end, std::back_inserter(transient));
122-
immer::copy(other_it, other_end, std::back_inserter(transient));
123-
124-
m_vector = transient.persistent();
91+
m_vector = std::move(m_vector).insert(index, id);
12592
}
12693

12794
public:

src/map/RoomIdSet.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,6 @@ void runRoomIdSetTests()
8686
TEST_ASSERT(!defaultConstructorSet.containsElementNotIn(emptyTestSet));
8787
TEST_ASSERT(!setForIteration.containsElementNotIn(setForIteration));
8888

89-
Type set1ForInsertAll;
90-
set1ForInsertAll.insert(RoomId(1));
91-
set1ForInsertAll.insert(RoomId(2));
92-
Type set2ForInsertAll;
93-
set2ForInsertAll.insert(RoomId(2));
94-
set2ForInsertAll.insert(RoomId(3));
95-
set1ForInsertAll.insertAll(set2ForInsertAll);
96-
TEST_ASSERT(set1ForInsertAll.size() == 3ULL);
97-
TEST_ASSERT(set1ForInsertAll.contains(RoomId(1)));
98-
TEST_ASSERT(set1ForInsertAll.contains(RoomId(2)));
99-
TEST_ASSERT(set1ForInsertAll.contains(RoomId(3)));
100-
101-
Type nonEmptySetForInsertAll;
102-
nonEmptySetForInsertAll.insert(RoomId(100));
103-
Type emptySetForInsertAll;
104-
nonEmptySetForInsertAll.insertAll(emptySetForInsertAll);
105-
TEST_ASSERT(nonEmptySetForInsertAll.size() == 1ULL);
106-
TEST_ASSERT(nonEmptySetForInsertAll.contains(RoomId(100)));
107-
108-
Type emptySetAForInsertAll;
109-
Type emptySetBForInsertAll;
110-
emptySetAForInsertAll.insertAll(emptySetBForInsertAll);
111-
TEST_ASSERT(emptySetAForInsertAll.empty());
112-
11389
Type setForFirstLast;
11490
setForFirstLast.insert(RoomId(50));
11591
setForFirstLast.insert(RoomId(30));

0 commit comments

Comments
 (0)