Continuing from my previous question: std::priority_queue pre-allocate memory memory error
I'm working on a deterministic real time project which has an execution cycle per iteration of couple of hundred micro-seconds.
I'm facing issue where by the push operation on a std::queue and std::priority_queue randomly is taking large amount of time. Push operation generally takes around 5-25 micro-seconds but randomly takes 300 micro-seconds to 5 milli-seconds.
To overcome this issue, I decided to pre-allocate memory for my std::queue and std::priority_queue.
Development OS: Ubuntu 24.04 LTS.
My target OS: vxWorks 7
Language: C++17
Code:
struct DemoStruct
{
int a;
float b;
std::string data;
bool operator>(const DemoStruct& object) const //Check if greater
{
return this->a > object.a;
}
};
std::vector<DemoStruct> container;
container.reserve(1000);
std::priority_queue
<
DemoStruct,
std::vector<DemoStruct>,
std::greater<void>
>
mReceiveDataQueue {
{}, std::move(this->container)
};
while (i < 10)
{
DemoStruct d = {
.a = 10 - i,
.b = i * 2.0f,
.data = "Hello, World!!!"; //Will mostly hold data with 85 characters
};
mReceiveDataQueue.push(d); //or mReceiveDataQueue.push(std::move(d))
}
What I wish to understand, will reserving memory help? From what I understand, every-time in the while loop DemoStruct constructor will be called and (re)allocate memory. With move schematics wouldn't the compiler just move the memory to std::priority_queue or std::queue. So is reserving memory helpful at all or will it be counter-productive? If I use some other data-structure like boost::circular_queue what would be the effect?
container.reserve(1000). (There would be multiple memory allocations forstd::string, unless optimized away by small string optimization).