https://en.cppreference.com/w/cpp/named_req/Allocator
I am referring to this ^^^^ kinds of custom of memory allocator (if there is a different kind of allocator let me know)
I am asking because I have a certain allocation and deallocation pattern where I do lot of allocations and then free them all at once (this pattern repeats many times),
and I am looking for a way to optimize away the alloc and free cost, and I am looking for what is the "proper" way to write that out
One possible solution (which I am leaning towards) is simply making a std::vector (which stores the objects)
and pushing in all the "new" objects and when I am done simply clearing the vector (aka marking the memory as unused)
The bigest downside from what i can tell using this method (which does not apply to my case) is that if you have objects of different sizes you can not use the same vector
A second solution would be to use a linear allocator from (
https://github.com/mtrebi/memory-allocators) which acts similarly to the std::vector example as shown above
A third option (which is probably not a valid option because I misunderstand what an allocator is) is using a STL complaint allocator every time I am calling new
Here is a snippet of code to better understand my problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main() {
for ( 1000+ times){
...
for (many iter){
...
new some_object
...//compute heavy stuff
}
... //other compute heavy stuff
for (many iter){
delete some_object
}
}
}
| |
Lines 6 and 11 take up a large portion of total execution time
P.S. there is probably a better title for this question, if you have one let me know