Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking my List.filter faster on Scala?
    primarykey
    data
    text
    <p>I'm making a game with Scala.</p> <p>My game has several pools for keeping enemies. They are immutable Lists because they are initialized to a size that should be enough (because creating new enemy instances during the game is very expensive).</p> <p>My game knows if an enemy is alive by asking <code>enemy.isVisible</code>. So my CollisionHandler works like:</p> <ol> <li>Combine all live enemies to a list</li> <li>Get live bullets</li> <li>Space partition these and do collision detection for bullets and enemies in the same space partition</li> </ol> <p>What supprised me was that according to the profiler, step 1 takes most of the time. And what that step does is basically say:</p> <pre><code>def allActiveEnemies = List(enemyType1.getAllActive, enemyType2.getAllActive, ... ).flatten </code></pre> <p>the <code>flatten</code> there doesn't seem expensive but instead it's get getAllActive calls. They are implemented in my Pooled trait like this:</p> <pre><code>trait Pooled[T &lt;: Entity] { var pool = List[T]() val INITIAL_POOL_SIZE:Int def initPool() { for(i&lt;-1 to INITIAL_POOL_SIZE) { addToPool(disable(createNew)) } } def getAllActive:List[T] = pool.filter(e =&gt; e.isVisible) } </code></pre> <p>(I omitted most of the trait because I don't think it's relevant here.)</p> <p>pool.filter is what burns like 45% of the total time spent in CollisionHandler, which seems really strange.</p> <p>Any suggestions to make things faster here?</p> <p>Maybe using ArrayLists instead of List? Maybe using some sorting and mutable collections? Or am I simply doing something horribly wrong?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload