When copying click vectors and appending, the resize will be overkill
Closed this issue · 1 comments
bernardosulzbach commented
Assume that the standard implementation copies into a vector which is exactly as large as required. So copying 3 clicks gets a vector for 3 Position objects. Now, after pushing a new click into it, it grows to 6 Position objects.
Possibly defining an iterator that gets a vector and an object and returns all elements in the vector and then the provided object would allow for proper vector initialization.
bernardosulzbach commented
Not sure this is valid. In the way the code is currently implemented, the derived state will have a vector as long as ever needed (the copy operator does not seem to shrink it), so this reallocation does not seem to happen. An example test is shown below.
#include <iostream>
#include <vector>
void *operator new(size_t size) {
std::cout << "Allocating " << size << " bytes." << '\n';
return malloc(size);
}
void operator delete(void *pointer) {
std::cout << "Freeing memory." << '\n';
free(pointer);
}
int main() {
std::vector<int> v;
for (int i = 1; i <= 9; i++)
v.push_back(i);
auto v_copy = v;
v.pop_back();
v_copy = v;
v.pop_back();
v_copy = v;
return 0;
}