
(Warning! this will be technical mumbojumbo)
In real time computer graphics one thing has given headaches to artist for years: Z-sorting (iow. Z-buffering). Everything goes great until you need to sort objects on screen. It simply eats your CPU. In AS3 this is a huge bottle neck. Yeah the drawing is rather slow but sorting is even more slow. If the challenge is demanding you just need to be equally smart.
As you may have noticed in my works the z-sorting doesn’t seems to be very big issue. For example the GameOfLife3D rolls 13824 particles and does dozen other stuff as well. In worst case just the sort can take up to 40ms in every frame.
When creating an engine of 3D the handling and z-sorting of objects should not thought as separated blocks. To gain performance every piece need to work together. The perfect symbiosis has to be found to serve all step of the system.
I build a test that sort list of objects that have X,Y,Z -coordinates and parameter i. Task is to z-sort objects and set i to z. In every test coordinates are set to same to have fair results. I added all z-sort methods I could find.
Here are average results with 100000 objects (click on them to stop the process):
Vector native sort-method + for-loop: 327ms
Vector shellShort-method + for-loop: 207ms
Array native sort-method + for-loop: 328ms
Array native sortOn-method + for-loop: 54ms
Jedi+Voodoo sorting and handle method: 4ms
Cut the bullshit and tell us how!
Michael Baczynski introduced this code back in 2007 and I think it’s extremely weird that it hasn’t been noticed more widely. I was convinced about the power of linked lists after reading his post and been using them ever since. I build the evo3d and evoCunningParticleEngine based on doubly-LinkedLists. He also shows a very elegant method for sorting called InsertionSort and boy it’s fast when used right.
The beauty of it is that it doesn’t do anything unnecessary. Here’s the lines I use on test:
var n:Particle3D, j:Particle3D, p:Particle3D, c:Particle3D;
c = f.next;
while(c) {
n = c.next;
p = c.prev;
if (c.z > p.z)
{
j = p;
while(j.prev)
{
if (c.z > j.prev.z)
{
j = j.prev;
}else{
break;
}
}
n ? (p.next = n, (n.prev = p)) : p.next = null;
(j == f) ? (c.prev = null, (c.next = j), (j.prev = c), (f = c)) : (c.prev = j.prev, (j.prev.next = c), (c.next = j), (j.prev = c));
}
c = n;
}
As you can see if the objects are in order there’s only one comparison and everything happens inside linkedlist while loop that is the fastest way to loop in as3. Also the benefit of this is that it provides the first object in linkedlist and it can be used to loop again in linkedlist while loop.
{
first = first.next;
}
There is a but on this method. If objects are completely mixed this insertion sorting is by far the slowest. That’s why list has to semi ordered in the first run. This is not a problem in 3d when you are moving using camera and objects doesn’t swap too much. All the models should be preordered by other methods for the first time. After that insertion sort will take the rest and give maximum performance.
Yeah I know the test isn’t very good since all objects are in order all ready :) Anyway the linkedlist + insertion sorting is the fastest there is since it only swap those that need to be swapped and does it inside the fastest loop possible.
You can check out the sources here and try for your self.
If you find a faster way to do this I would really appreciate if you let me know how.
— UPDATE 06:32pm 8.12.2009 —
Rob Bateman responded on this topic by explaining his method on Z-sorting.
Radix Sort is something I didn’t even knew exist until now :) A must read post.

December 6th, 2009
3:00 pm
Social comments and analytics for this post…
This post was mentioned on Twitter by simppafi: The fastest way to Z-sort and handle objects in AS3 http://www.simppa.fi/blog/?p=534...
December 6th, 2009
3:42 pm
[...] This post was Twitted by pjinteractive [...]
December 6th, 2009
4:16 pm
this is really interesting. It’s wierd that linked lists are still very much unheard of in the flash world considering what a useful data sctruct they are.
Amazing work as usual!
December 7th, 2009
6:29 am
This could be really useful for those times where you really don’t have many changes in the sorting order, like you say. As you also say, it’d be disasterous if you use it when there are many changes. This includes a lot of 3D scenes, particularly those with fast motion. But if you have a low-poly scene that you know doesn’t change much, this could be a really nice performance boost. Thanks for the algorithm and the writeup!
December 7th, 2009
10:14 am
It’s fastest on low poly sorting. In most cases this sorting is valid for 3d in high poly counts also.
It’s very special case when the disasterous lack of speed pops in. The problem comes only when everything is totally mixed and there are big amount of objects to sort. If you are turning in 3d faster then your framerate then it is an issue. Like instant 180 degree turn, but this case is easy to catch and change the sorting method for one frame.
December 8th, 2009
7:11 am
[...] The fastest way to Z-sort and handle objects in AS3 [...]
December 8th, 2009
5:53 pm
[...] latest post is about sorting in AS3, and specifically, z-sorting objects that are being drawn in a 3D scene. [...]
December 8th, 2009
5:57 pm
Hey Simo
very interesting post – i’ve been running my own tests and have compared them to your sorting method. It’s hard to say for sure which is faster in real-world sorting as you only test the best case in your demo (using a pre-sorted list). Using Radix Sorting, i get a pretty respectable 16 ms for 100000 particles, regardless of how sorted or unsorted the list is. Check out my post on the subject to see how it is done: http://www.infiniteturtles.co.uk/blog/fast-sorting-in-as3
December 9th, 2009
3:28 pm
Hi Simppa :)
I saw your update on not knowing the radix sorting method.
If you have a look at this ( http://people.cs.ubc.ca/~harrison/Java/sorting-demo.html ) link…. it lists all sorting algorithms with working examples and stuff.
Maybe its some use to you.
bb
Rackdoll
December 9th, 2009
3:35 pm
the “Fast-Quick” sort seems also a good option for fast preformance.
December 15th, 2009
8:43 pm
[...] simppa.fi/blog » The fastest way to Z-sort and handle objects in AS3 [...]
January 4th, 2010
12:03 pm
[...] of digital data on the real world) is an exciting new tech trend. … 3 Likes simppa.fi/blog » The fastest way to Z-sort and handle objects in AS3 3 Likes Andrew Shorten » Help shape future versions of Flash Builder [...]