Time critical execution and QThread
-
Hello,
I have an app that is time critical (adjusting hardware to be able to track things on a conveyor belt with 1mm/ms). So im working with a lot of raw pointers to keep memory allocated and swap the contents in and out.
3 questions:
- Do QSharedPointer and QScopedPointer generate any overhead for allocating the memory?
- Is it possible to assign a certain heap space to a thread so for long running-systems I can minimize the fragmentation to keep up with performance?
What are other options that I have to make my code and calculations execute fast. Neglecting hardware limitations for now.
-
The important thing to have in mind is the cache. It sounds like you have a vector of pointers. One problem with this is that when you iterate of the vector you won't necessarily access adjacent memory. This will totally screw with cache performance. Even if you were to get a huge slice of memory for your thread this slice would still be internally fragmented. On the other hand, if you were to use a regular vector of objects (instead of pointers) it will screw with you real time requirement. Occasionally, you'll have a resize of the vector which will copy all of its data which will slow it down. Only if you can make sure there is no resize happening (as you said you are swapping things out) this could work. Otherwise you need to find some middle ground: something like chunks of memory linked together such that each chunk with several objects/values fits into a cache line (maybe the size of a L2 cache line).
And finally, the most important advice: Use profiling to measure where your performance bottleneck is to optimize in areas where it is actually necessary and helpful.
-
@Redman said in Time critical execution and QThread:
QSharedPointer and QScopedPointer
They do not allocate memory they only make sure that memory is freed when not referenced any-more or when going out of scope. There is not much overhead.
You can always allocate a big chunk of heap memory and use it. You can also use placement new to allocate memory in that chunk (https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new).
-
-
The important thing to have in mind is the cache. It sounds like you have a vector of pointers. One problem with this is that when you iterate of the vector you won't necessarily access adjacent memory. This will totally screw with cache performance. Even if you were to get a huge slice of memory for your thread this slice would still be internally fragmented. On the other hand, if you were to use a regular vector of objects (instead of pointers) it will screw with you real time requirement. Occasionally, you'll have a resize of the vector which will copy all of its data which will slow it down. Only if you can make sure there is no resize happening (as you said you are swapping things out) this could work. Otherwise you need to find some middle ground: something like chunks of memory linked together such that each chunk with several objects/values fits into a cache line (maybe the size of a L2 cache line).
And finally, the most important advice: Use profiling to measure where your performance bottleneck is to optimize in areas where it is actually necessary and helpful.
-
-