llvm.org GIT mirror llvm / 06bd8ca
Implement copy and move assignment for TinyPtrVector. These try to re-use allocated vectors as much as possible. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161041 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 8 years ago
2 changed file(s) with 223 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
3131 llvm::PointerUnion Val;
3232
3333 TinyPtrVector() {}
34 ~TinyPtrVector() {
35 if (VecTy *V = Val.template dyn_cast())
36 delete V;
37 }
38
3439 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
3540 if (VecTy *V = Val.template dyn_cast())
3641 Val = new VecTy(*V);
3742 }
43 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
44 if (this == &RHS)
45 return *this;
46 if (RHS.empty()) {
47 this->clear();
48 return *this;
49 }
50
51 // Try to squeeze into the single slot. If it won't fit, allocate a copied
52 // vector.
53 if (Val.template is()) {
54 if (RHS.size() == 1)
55 Val = RHS.front();
56 else
57 Val = new VecTy(*RHS.Val.template get());
58 return *this;
59 }
60
61 // If we have a full vector allocated, try to re-use it.
62 if (RHS.Val.template is()) {
63 Val.template get()->clear();
64 Val.template get()->push_back(RHS.front());
65 } else {
66 *Val.template get() = *RHS.Val.template get();
67 }
68 return *this;
69 }
70
3871 #if LLVM_USE_RVALUE_REFERENCES
3972 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
4073 RHS.Val = (EltTy)0;
4174 }
75 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
76 if (this == &RHS)
77 return *this;
78 if (RHS.empty()) {
79 this->clear();
80 return *this;
81 }
82
83 // If this vector has been allocated on the heap, re-use it if cheap. If it
84 // would require more copying, just delete it and we'll steal the other
85 // side.
86 if (VecTy *V = Val.template dyn_cast()) {
87 if (RHS.Val.template is()) {
88 V->clear();
89 V->push_back(RHS.front());
90 return *this;
91 }
92 delete V;
93 }
94
95 Val = RHS.Val;
96 RHS.Val = (EltTy)0;
97 return *this;
98 }
4299 #endif
43 ~TinyPtrVector() {
44 if (VecTy *V = Val.template dyn_cast())
45 delete V;
46 }
47100
48101 // implicit conversion operator to ArrayRef.
49102 operator ArrayRef() const {
172225 }
173226 return end();
174227 }
175
176 private:
177 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
178 #if LLVM_USE_RVALUE_REFERENCES
179 void operator=(TinyPtrVector&&); // NOT IMPLEMENTED YET.
180 #endif
181228 };
182229 } // end namespace llvm
183230
5959 V.push_back(Values[i]);
6060 }
6161
62 void setVectors(ArrayRef Values1, ArrayRef Values2) {
63 V.clear();
64 appendValues(V, Values1);
65 V2.clear();
66 appendValues(V2, Values2);
67 }
68
6269 void expectValues(const VectorT &V, ArrayRef Values) {
6370 EXPECT_EQ(Values.empty(), V.empty());
6471 EXPECT_EQ(Values.size(), V.size());
153160 TypeParam Move(std::move(Copy2));
154161 this->expectValues(Move, this->testArray(42));
155162 this->expectValues(Copy2, this->testArray(0));
163 #endif
164 }
165
166 TYPED_TEST(TinyPtrVectorTest, CopyAndMoveTest) {
167 this->V = this->V2;
168 this->expectValues(this->V, this->testArray(0));
169 this->expectValues(this->V2, this->testArray(0));
170 #if LLVM_USE_RVALUE_REFERENCES
171 this->V = std::move(this->V2);
172 this->expectValues(this->V, this->testArray(0));
173 #endif
174
175 this->setVectors(this->testArray(1), this->testArray(0));
176 this->V = this->V2;
177 this->expectValues(this->V, this->testArray(0));
178 this->expectValues(this->V2, this->testArray(0));
179 #if LLVM_USE_RVALUE_REFERENCES
180 this->setVectors(this->testArray(1), this->testArray(0));
181 this->V = std::move(this->V2);
182 this->expectValues(this->V, this->testArray(0));
183 #endif
184
185 this->setVectors(this->testArray(2), this->testArray(0));
186 this->V = this->V2;
187 this->expectValues(this->V, this->testArray(0));
188 this->expectValues(this->V2, this->testArray(0));
189 #if LLVM_USE_RVALUE_REFERENCES
190 this->setVectors(this->testArray(2), this->testArray(0));
191 this->V = std::move(this->V2);
192 this->expectValues(this->V, this->testArray(0));
193 #endif
194
195 this->setVectors(this->testArray(42), this->testArray(0));
196 this->V = this->V2;
197 this->expectValues(this->V, this->testArray(0));
198 this->expectValues(this->V2, this->testArray(0));
199 #if LLVM_USE_RVALUE_REFERENCES
200 this->setVectors(this->testArray(42), this->testArray(0));
201 this->V = std::move(this->V2);
202 this->expectValues(this->V, this->testArray(0));
203 #endif
204
205 this->setVectors(this->testArray(0), this->testArray(1));
206 this->V = this->V2;
207 this->expectValues(this->V, this->testArray(1));
208 this->expectValues(this->V2, this->testArray(1));
209 #if LLVM_USE_RVALUE_REFERENCES
210 this->setVectors(this->testArray(0), this->testArray(1));
211 this->V = std::move(this->V2);
212 this->expectValues(this->V, this->testArray(1));
213 #endif
214
215 this->setVectors(this->testArray(0), this->testArray(2));
216 this->V = this->V2;
217 this->expectValues(this->V, this->testArray(2));
218 this->expectValues(this->V2, this->testArray(2));
219 #if LLVM_USE_RVALUE_REFERENCES
220 this->setVectors(this->testArray(0), this->testArray(2));
221 this->V = std::move(this->V2);
222 this->expectValues(this->V, this->testArray(2));
223 #endif
224
225 this->setVectors(this->testArray(0), this->testArray(42));
226 this->V = this->V2;
227 this->expectValues(this->V, this->testArray(42));
228 this->expectValues(this->V2, this->testArray(42));
229 #if LLVM_USE_RVALUE_REFERENCES
230 this->setVectors(this->testArray(0), this->testArray(42));
231 this->V = std::move(this->V2);
232 this->expectValues(this->V, this->testArray(42));
233 #endif
234
235 this->setVectors(this->testArray(1), this->testArray(1));
236 this->V = this->V2;
237 this->expectValues(this->V, this->testArray(1));
238 this->expectValues(this->V2, this->testArray(1));
239 #if LLVM_USE_RVALUE_REFERENCES
240 this->V = std::move(this->V2);
241 this->expectValues(this->V, this->testArray(1));
242 #endif
243
244 this->setVectors(this->testArray(1), this->testArray(2));
245 this->V = this->V2;
246 this->expectValues(this->V, this->testArray(2));
247 this->expectValues(this->V2, this->testArray(2));
248 #if LLVM_USE_RVALUE_REFERENCES
249 this->setVectors(this->testArray(1), this->testArray(2));
250 this->V = std::move(this->V2);
251 this->expectValues(this->V, this->testArray(2));
252 #endif
253
254 this->setVectors(this->testArray(1), this->testArray(42));
255 this->V = this->V2;
256 this->expectValues(this->V, this->testArray(42));
257 this->expectValues(this->V2, this->testArray(42));
258 #if LLVM_USE_RVALUE_REFERENCES
259 this->setVectors(this->testArray(1), this->testArray(42));
260 this->V = std::move(this->V2);
261 this->expectValues(this->V, this->testArray(42));
262 #endif
263
264 this->setVectors(this->testArray(2), this->testArray(1));
265 this->V = this->V2;
266 this->expectValues(this->V, this->testArray(1));
267 this->expectValues(this->V2, this->testArray(1));
268 #if LLVM_USE_RVALUE_REFERENCES
269 this->setVectors(this->testArray(2), this->testArray(1));
270 this->V = std::move(this->V2);
271 this->expectValues(this->V, this->testArray(1));
272 #endif
273
274 this->setVectors(this->testArray(2), this->testArray(2));
275 this->V = this->V2;
276 this->expectValues(this->V, this->testArray(2));
277 this->expectValues(this->V2, this->testArray(2));
278 #if LLVM_USE_RVALUE_REFERENCES
279 this->setVectors(this->testArray(2), this->testArray(2));
280 this->V = std::move(this->V2);
281 this->expectValues(this->V, this->testArray(2));
282 #endif
283
284 this->setVectors(this->testArray(2), this->testArray(42));
285 this->V = this->V2;
286 this->expectValues(this->V, this->testArray(42));
287 this->expectValues(this->V2, this->testArray(42));
288 #if LLVM_USE_RVALUE_REFERENCES
289 this->setVectors(this->testArray(2), this->testArray(42));
290 this->V = std::move(this->V2);
291 this->expectValues(this->V, this->testArray(42));
292 #endif
293
294 this->setVectors(this->testArray(42), this->testArray(1));
295 this->V = this->V2;
296 this->expectValues(this->V, this->testArray(1));
297 this->expectValues(this->V2, this->testArray(1));
298 #if LLVM_USE_RVALUE_REFERENCES
299 this->setVectors(this->testArray(42), this->testArray(1));
300 this->V = std::move(this->V2);
301 this->expectValues(this->V, this->testArray(1));
302 #endif
303
304 this->setVectors(this->testArray(42), this->testArray(2));
305 this->V = this->V2;
306 this->expectValues(this->V, this->testArray(2));
307 this->expectValues(this->V2, this->testArray(2));
308 #if LLVM_USE_RVALUE_REFERENCES
309 this->setVectors(this->testArray(42), this->testArray(2));
310 this->V = std::move(this->V2);
311 this->expectValues(this->V, this->testArray(2));
312 #endif
313
314 this->setVectors(this->testArray(42), this->testArray(42));
315 this->V = this->V2;
316 this->expectValues(this->V, this->testArray(42));
317 this->expectValues(this->V2, this->testArray(42));
318 #if LLVM_USE_RVALUE_REFERENCES
319 this->setVectors(this->testArray(42), this->testArray(42));
320 this->V = std::move(this->V2);
321 this->expectValues(this->V, this->testArray(42));
156322 #endif
157323 }
158324