Skip to content

Commit 161d98d

Browse files
authored
glz::async_map: using proxy rather than allocating unique_ptr (#1433)
1 parent c4dc95c commit 161d98d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

include/glaze/thread/async_map.hpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ namespace glz
6969
std::shared_ptr<std::shared_lock<std::shared_mutex>> shared_lock_ptr;
7070
std::shared_ptr<std::unique_lock<std::shared_mutex>> unique_lock_ptr;
7171

72+
struct proxy
73+
{
74+
std::pair<const K&, V&> p;
75+
76+
std::pair<const K&, V&>* operator->() { return &p; }
77+
};
78+
7279
public:
7380
iterator(typename std::vector<K>::const_iterator key_it, typename std::vector<V>::iterator value_it,
7481
async_map* map, std::shared_ptr<std::shared_lock<std::shared_mutex>> existing_shared_lock = nullptr,
@@ -132,11 +139,9 @@ namespace glz
132139
return tmp;
133140
}
134141

135-
// Dereference
136142
value_type operator*() const { return {*key_it, *value_it}; }
137143

138-
// Arrow Operator
139-
std::unique_ptr<value_type> operator->() const { return std::make_unique<value_type>(*key_it, *value_it); }
144+
proxy operator->() const { return proxy(*key_it, *value_it); }
140145

141146
// Equality Comparison
142147
bool operator==(const iterator& other) const { return key_it == other.key_it; }
@@ -145,7 +150,6 @@ namespace glz
145150
bool operator!=(const iterator& other) const { return !(*this == other); }
146151
};
147152

148-
// Const Iterator Class Definition
149153
class const_iterator
150154
{
151155
public:
@@ -161,6 +165,13 @@ namespace glz
161165
const async_map* map;
162166
std::shared_ptr<std::shared_lock<std::shared_mutex>> shared_lock_ptr;
163167

168+
struct proxy
169+
{
170+
std::pair<const K&, const V&> p;
171+
172+
const std::pair<const K&, const V&>* operator->() const { return &p; }
173+
};
174+
164175
public:
165176
const_iterator(typename std::vector<K>::const_iterator key_it,
166177
typename std::vector<V>::const_iterator value_it, const async_map* map,
@@ -214,11 +225,9 @@ namespace glz
214225
return tmp;
215226
}
216227

217-
// Dereference
218228
value_type operator*() const { return {*key_it, *value_it}; }
219229

220-
// Arrow Operator
221-
std::unique_ptr<value_type> operator->() const { return std::make_unique<value_type>(*key_it, *value_it); }
230+
proxy operator->() const { return proxy(*key_it, *value_it); }
222231

223232
// Equality Comparison
224233
bool operator==(const const_iterator& other) const { return key_it == other.key_it; }

0 commit comments

Comments
 (0)