ArrayLists and Receding Hairline

Hi again,

I have built a (theoretically) wonderful gfx framework for my upcoming games to make life easier, that was the purpose anyway. I’ll explain what it’s all about.
I read a book on ActionScript 3 the other day and there I found a rather good solution for chaining Sprites together, they call it DisplayList in AS. The whole idea is that you’ve got the “stage” where each and every one of the DisplayObjects belong. Then you’ve got DisplayObjectContainers that can hold other DisplayObjectContainers or DisplayObjects. It’s very easy to add or remove sprites to/ from this displayList, swap objects, etc. I copied this idea and I built my own little DislpayList in Java. Works perfectly, it is very comfortable to use, I am a hundred percent satisified with it apart from one thing: it’s fuckin slow.

The problem is ArrayList: I’m using it to keep references of children objects. So every time the program attempts to repaint what’s on the displaylist it has to iterate through all the members which process proved to be a lot slower than what I expected. (I tested it with and without arrayLists, if I use arraylists with only 2-3 objects added to the list I lose 8-10 frames per second… and there are meant to be DOZENS of objects in the displayList, not 2-3!!! )
I KNOW how much quicker simple arrays are but they’ve got a fixed size which makes them absolutely useless in this case. I need a flexible solution but a lot quicker than this stupid arraylist.

Any solution?
Or did anyone understand this gibberish post at all?

Well, any answers are welcome
Greg

ArrayLists are only marginally (probably max factor 2) slower
than plain old arrays. Only removing from an ArrayList is slow,
but it would be slow on an array too, as you have to shift a lot
of items ‘to the left’. Are you sure you’re barking up the right tree?

Did you profile your app to see where the bottleneck is?

How are you rendering? Java2D, OpenGL? Maybe you did
something that disabled hardware acceleration? Show us
some code!

Hmm you made a point here Riven, I actually didn’t profile it, I might do it in the next 2 minutes … However due to my calculations using ArrayList appears to be the root of the problem. If I replace ArrayList with Arrays my program produces almost 20% better performance. I don’t use JOGL, I’m working with plain old J2D. Even 2D is quite confusing to me but hey that’s the thing about java, it’s meant to be confusing to keep the bloody “flashers” away, innit :)) No hardware acceleration so far, but I’m planning to keep sprites in VolatileImages. Right now the program keeps everything in BufferedImages. I also use double buffering, which is a good thing apparently.

Java2D is way slow if hardware acceleration is not happening. In almost all Java2D games I’ve made, the bottleneck is in the rendering, not the logic. See how fast it goes if you turn off drawing.

If you think arraylist is your trouble then you can use another collection class. I used a few HashMaps in one of my games for caching objects.
But make sure you understand when to use a collection over another.

Are you doing any searching in these ArrayLists? That is the only expense I can see with using them ArrayLists. How are you measuring the performance? Did you profile it?

I also agree that (typically) the real expense should be the graphics code, not the logic. If your back end lists are that slow then your doing something silly with them.

I also use a very similar system for my games, works perfectly.

If switching from ArrayLists to arrays really gives you that much of a speed increase then something is very wrong or you’re doing something daft. Post code so we can actually see wft you’re doing.

Probably removing from the list.

If jMonkeyEngine can implement their scene graph using lists of nodes (see http://code.google.com/p/jmonkeyengine/source/browse/trunk/src/com/jme/scene/Node.java), and have 1000s of nodes in the scene with a good framerate, I’m quite sure ArrayList isn’t the problem :slight_smile:

Ok, ok, got the message guys. Since every one of you say the problem can’t be the ArrayList I believe you and try to dig deeper. It’s probably the rendering that does something dodgy.

Regardless, I’m still losing hair.

Second this, certain operations are very slow on array lists and it could be that your just setting it to null in an array (and so avoiding the reordering that an arraylist performs).

Alternatively, a long shot would be over-use of enhanced-for-loop usage creating too much iterator churn and stressing the gc. Although I think you’d have to be doing silly amounts of iteration for that to be a problem these days.

Post code!

Just try turning various things off (especially rendering) and then see what makes it run faster. Simple and easy, no? Or, just profile your code a bit with nanoTime. Either way, actually find out what’s causing the problem, it’s not too difficult of a task, it’s just time consuming.