Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Particle Pool Performance Optimization In C++

I have a particle pools which stored particle pointers

It has a large number of particles

Every particle has the IsActive variable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When partitioning the vector every frame according to their active state, there is high cpu usage

So instead is it a good way to partition the vector every few seconds on a worker thread?

>Solution :

Since the order of the active doesn’t matter, and they don’t become active or inactive very often, you can use a trick to keep the active particles at the front:

Suppose that nActive is the number of active particles.

When an inactive particle becomes active, do std::swap(vector[nParticle], vector[nActive]); nActive++; i.e. make it be the last active particle – and whatever inactive particle was already in that slot, goes to where the particle used to be, and we don’t care, because we don’t care about the order.

When an active particle becomes inactive, do nActive--; std::swap(vector[nParticle], vector[nActive]); i.e. make it be the first inactive particle – and whatever active particle was already in that slot, goes to where the particle used to be, and we don’t care, because we don’t care about the order.

You can remember this trick by remembering that it’s easy to make a particle active or inactive – if we don’t care which one it is – because we just add or subtract 1 from nActive. But then we don’t know which particle we just changed. So we swap that particle with the one we meant to change, and we don’t care where that particle goes, as long as it stays inactive or stays active (doesn’t change).

Like this:

+-----------+-----------+-----------+-----------+
| Particle0 | Particle1 | Particle2 | Particle3 |
| active    | active    | inactive  | inactive  |
+-----------+-----------+-----------+-----------+
                        ^
                        |
                    nActive==2

make particle 3 active: swap particle 3 and 2, then increment nActive:

+-----------+-----------+-----------+-----------+
| Particle0 | Particle1 | Particle3 | Particle2 |
| active    | active    | active    | inactive  |
+-----------+-----------+-----------+-----------+
                                    ^
                                    |
                                nActive==3

make particle 0 inactive: decrement nActive, then swap particle 3 (in slot 2) and 0.

+-----------+-----------+-----------+-----------+
| Particle3 | Particle1 | Particle0 | Particle2 |
| active    | active    | inactive  | inactive  |
+-----------+-----------+-----------+-----------+
                        ^
                        |
                    nActive==2

The particle order is all jumbled up now, but you don’t care about that, right?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading