PODVector: Add extra capacity#4734
Conversation
Add some extra room when we call `PODVector::resize` and `reserve`. The extra capacity is computed as 3*sqrt(capacity) suggested by @AlexanderSinn, and is capped at 10%. This might help particle codes avoid memory re-allocation.
|
I feel like there should still be a way to resize a vector to the exact capacity that is asked for. Only if the vector is used in a particle container with multiple tiles where particles are exchanged using a thermal process is this a good strategy. If there is only one tile or if the vector is used to store a grid or metadata, the extra capacity is not needed. |
|
What's the downside? |
|
Just a waste of (a little bit of) memory. It also could get very confusing when trying to implement custom allocation size strategies on top of PODVector. |
|
Because it scales as sqrt(n), the extra memory cost is not an issue. As for implementing other growth strategies such as what are in #4735 , here is what I think. |
|
Sounds good. I am fine with merging this PR as is, and after that I will make a follow-up to add exact and geometric strategies to resize and reserve only, using grow_podvector_capacity. I also agree that those are the most important functions, and insert/push_back shouldn't really be used. Should the interface for resize and reserve include an optional argument or add new funcitons? Option 1: vec.reserve(n, amrex::GrowthStrategy::Geometric);
vec.resize(n, amrex::GrowthStrategy::Exact);
vec.resize(n /*, amrex::GrowthStrategy::Poisson*/); // Poisson is the defaultOption 2: vec.reserve_geometric_grow(n);
vec.resize_exact_grow(n);
vec.resize(n); // uses Poisson growOption 3: vec.reserve_geometric(n);
vec.resize_exact(n);
vec.resize(n); // uses Poisson grow |
|
I prefer Option 1. It's easier for the users. |
Add some extra room when we call `PODVector::resize` and `reserve`. The extra capacity is computed as 3*sqrt(capacity) suggested by @AlexanderSinn, and is capped at 10%. This might help particle codes avoid memory re-allocation.
Add some extra room when we call
PODVector::resizeandreserve. The extra capacity is computed as 3*sqrt(capacity) suggested by @AlexanderSinn, and is capped at 10%. This might help particle codes avoid memory re-allocation.