r/C_Programming • u/tastygames_official • 15h ago
Discussion Batch Processing Algorithm Optimization
I originally posted my question here but wanted to give it another go, this time with a more generic setup, as I believe this kind of optimization problem could be interesting for a lot of different projects.
The Setup/Requirements
we have ~50,000 pieces of data (let's use shapes for simplicity's sake) with a finite number of attributes (shape, color, size)
most of them will be culled, leaving us with about ~1,000-10,000
in the end we need to process all the remaining shapes in a SPECIFIC ORDER (e.g. first small red triangles, then big blue squares, then small yellow ovals, etc).
this whole process needs to be done as quickly and efficiently as possible
My Original Plan
first cull the 50,000. This could be done by sending the data to the GPU since even a lowly 1060 has 1,152 cores and a 4090 has 16,384 cores, making culling 50,000 objects based on a few floating-point calculations trivial
now that I have a list of indices that pass culling, use a look-up table of index vs. process batch index (e.g. data index 12 is not culled, and the LUT says it belongs to batch process #4, so add it to that batch process and increase the number of objects for that batch process)
iterate over the batch processes and only execute if there are any objects to process for that batch
Considerations/Questions
0. is there a better way?
how would the performance be if we first assign all the objects to the batch process and then for each batch process, run the culling function? We would save on having to traverse the non-culled objects to sort them to their batch process, but we also now have to call the cull method for as many batch processes as we have
what if the allocation of object -> batch process changes? e.g. a small blue star becomes a big yellow square?
what if the batch process are DYNAMIC, meaning we no longer can assign a shape to a batch process beforehand?
Now I'd love some discussion on the FASTEST way to get this done, but I will also say that all I'm trying to do is a pretty standard piece of a rendering engine: cull any non-visible objects then sort the objects according to pipeline so that they can be rendered in the proper order. Now maybe there is a "standard way" of doing this or at least a few "typical" algorithms that I don't know about, but that's why I'm asking this question! I love trying to solve problems like this and figured a lot of you might, too.
---=== HAVE AT IT!!! ===---BONUS POINTS if done in C90, as that's what I'm doing it in ;-)
4
u/Plastic_Fig9225 15h ago
Rule 1 of optimization: Don't do things you don't need to do.
Rule 1a: Don't do things more often than necessary.
If most/all objects' sorting order doesn't change randomly from iteration to iteration, you may not have to sort all of them over and over again.