Pools

Monkey Forums/Monkey Programming/Pools

erebel55(Posted 2014) [#1]
I am maintaining platforms using a list in a fifo manner.

Class Level
    Field platforms:List<Platform> ' using as a queue to hold platforms (fifo)
    
    Method CreatePlatform:Void()
        Local randomX:Int = Rnd(0, 400)
        Local randomY:Int = Rnd(1, 400)
        
        ' add the platform to the queue
        platforms.AddLast(New Platform(randomX, randomY))
    End Method
    
    Method RemovePlatform:Void()
        platforms.RemoveFirst() ' remove the first platform
    End Method
End Class


CreatePlatform is called every x seconds from my game loop. I then call RemovePlatform continuously to remove the first in platform when there are more than MAX_PLATFORMS.

I have read that calling new in the main loop can degrade performance.
So, I am hoping to convert my above code to use a pool instead of new.

Any help with this? I am trying to maintain my fifo logic, so is a List still okay to use?

Thank you


John Galt(Posted 2014) [#2]
Once you got a full list, why not just iterate through it and modify the oldest instance with the values you want for the new platform?

I wouldn't worry about it too much unless it's being called intensively.


erebel55(Posted 2014) [#3]
CreatePlatform is being called continuously throughout my game, a new platform is spawned every second.


Shinkiro1(Posted 2014) [#4]
Every second is actually 'rare' in terms of a game.
Don't worry about it until it becomes a problem (meaning you have identified it using your profiler).

So you should be fine using New.


erebel55(Posted 2014) [#5]
I have identified it as a problem, which is why I'm asking.


Gerry Quinn(Posted 2014) [#6]


I also am surprised that this is a problem, unless creating platforms is very heavyweight. You are not reloading graphics or anything when you construct a platform?

Anyway, the above is the way to use Pool. Set MAXPLATFORMS to as many as you think you will need at once.

If other classes will be creating platforms too, or if Level is not persistent, make the pool global.


John Galt(Posted 2014) [#7]
I have identified it as a problem, which is why I'm asking.
If one alloc/dealloc per second is causing an issue, then there is something weird going on. How did you identify the problem?