Best data structure for a particle system?

2013-11-15 Thread Mikko Ronkainen
What would be the best data structure for handling particles in a particle system in D2? Here's some thoughts: Particles are simple structs. Two lists, one for alive particles, one for dead ones. Memory allocations should be avoided, preallocate everything, no allocations when moving between l

Re: Best data structure for a particle system?

2023-05-02 Thread xbut360 via Digitalmars-d-learn
On Friday, 15 November 2013 at 11:52:44 UTC, Mikko Ronkainen wrote: What would be the best data structure for handling particles in a particle system in D2? Here's some thoughts: Particles are simple structs. Two lists, one for alive particles, one for dead ones. Memory allocations should be a

Re: Best data structure for a particle system?

2013-11-15 Thread Damian
On Friday, 15 November 2013 at 11:52:44 UTC, Mikko Ronkainen wrote: What would be the best data structure for handling particles in a particle system in D2? Here's some thoughts: Particles are simple structs. Two lists, one for alive particles, one for dead ones. Memory allocations should be a

Re: Best data structure for a particle system?

2013-11-15 Thread Mikko Ronkainen
Use just one list with a flag in the particle to see whether the particle is alive or dead, saves swapping between lists and you can use a simple array for fast access. Yes, simplicity, that's a good idea :) I was just wondering how much time would be saved if just iterating over the active pa

Re: Best data structure for a particle system?

2013-11-15 Thread bearophile
On Friday, 15 November 2013 at 11:52:44 UTC, Mikko Ronkainen wrote: What would be the best data structure for handling particles in a particle system in D2? Here's some thoughts: Particles are simple structs. Two lists, one for alive particles, one for dead ones. Memory allocations should be a

Re: Best data structure for a particle system?

2013-11-15 Thread Mike Parker
On 11/15/2013 9:19 PM, Mikko Ronkainen wrote: If relevant, doubly-linked might work better? dcollections LinkList, or maybe DList? I'm asking mostly because I'd like the container to avoid memory allocations while in use. For a particle system, I would avoid lists. A list of particles needs to

Re: Best data structure for a particle system?

2013-11-15 Thread Mikko Ronkainen
Ok, thanks! That linked list cache thrashing was just the thing I knew that I don't know :) Let's say I just use dynamic array and grow it when adding new particles to the system, and when particles die, just set their dead flag on. This means that, when adding a new particle, I need to scan

Re: Best data structure for a particle system?

2013-11-15 Thread bearophile
Mikko Ronkainen: Let's say I just use dynamic array and grow it when adding new particles to the system, and when particles die, just set their dead flag on. This means that, when adding a new particle, I need to scan the array first for possible dead particles that can be reused. Is there so

Re: Best data structure for a particle system?

2013-11-15 Thread Chris Cain
On Friday, 15 November 2013 at 13:32:38 UTC, Mikko Ronkainen wrote: Ok, thanks! That linked list cache thrashing was just the thing I knew that I don't know :) Let's say I just use dynamic array and grow it when adding new particles to the system, and when particles die, just set their dead f

Re: Best data structure for a particle system?

2013-11-15 Thread bearophile
Chris Cain: Instead of having a "dead flag", you could swap ( http://dlang.org/phobos/std_algorithm.html#swap ) the dying particle with the last particle in the list and then decrement the list's length. If the array is long you are accessing a cold part of it to swap with the end. By de

Re: Best data structure for a particle system?

2013-11-15 Thread Chris Cain
On Friday, 15 November 2013 at 14:08:20 UTC, bearophile wrote: The situation is a little more complex, there is a capacity field that I think is kept in a cold place of the array, it's also cached, but only if you append to just one array, etc. An alternative might be hold an array and manage

Re: Best data structure for a particle system?

2013-11-15 Thread Ivan Kazmenko
On Friday, 15 November 2013 at 14:01:36 UTC, Chris Cain wrote: By default (using the default GC and everything), D does not reallocate a dynamic array every time you change the length (even increasing it), so this will still be okay with allocations. Not exactly so. If you decrease the lengt

Re: Best data structure for a particle system?

2013-11-15 Thread Marco Leise
Am Fri, 15 Nov 2013 21:56:15 +0900 schrieb Mike Parker : > On 11/15/2013 9:19 PM, Mikko Ronkainen wrote: > > > > If relevant, doubly-linked might work better? dcollections LinkList, or > > maybe DList? I'm asking mostly because I'd like the container to avoid > > memory allocations while in use. >

Re: Best data structure for a particle system?

2013-11-15 Thread qznc
On Friday, 15 November 2013 at 14:01:36 UTC, Chris Cain wrote: On Friday, 15 November 2013 at 13:32:38 UTC, Mikko Ronkainen wrote: Ok, thanks! That linked list cache thrashing was just the thing I knew that I don't know :) Let's say I just use dynamic array and grow it when adding new particl