Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort optimisation #946

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions starling/src/starling/display/DisplayObject.as
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ package starling.display
private var _orientationChanged:Boolean;
private var _is3D:Boolean;
private var _maskee:DisplayObject;
private var _sort:uint;

// internal members (for fast access on rendering)

Expand Down Expand Up @@ -1149,6 +1150,10 @@ package starling.display
}
}


public function get sort():uint { return _sort; }
public function set sort(value:uint):void { _sort = value;}

/** The display object container that contains this display object. */
public function get parent():DisplayObjectContainer { return _parent; }

Expand Down
39 changes: 38 additions & 1 deletion starling/src/starling/display/DisplayObjectContainer.as
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ package starling.display
public function sortChildren(compareFunction:Function):void
{
sSortBuffer.length = _children.length;
mergeSort(_children, compareFunction, 0, _children.length, sSortBuffer);
if (compareFunction) {
mergeSort(_children, compareFunction, 0, _children.length, sSortBuffer);
} else {
mergeSortNoFunction(_children, 0, _children.length, sSortBuffer);
}
sSortBuffer.length = 0;
setRequiresRedraw();
}
Expand Down Expand Up @@ -493,6 +497,39 @@ package starling.display
}
}

private static function mergeSortNoFunction(input:Vector.<DisplayObject>,
startIndex:int, length:int,
buffer:Vector.<DisplayObject>):void {

if (length > 1) {
var i:int;
var endIndex:int = startIndex + length;
var halfLength:int = length / 2;
var l:int = startIndex;
var r:int = startIndex + halfLength;

mergeSortNoFunction(input, startIndex, halfLength, buffer);
mergeSortNoFunction(input, startIndex + halfLength, length - halfLength, buffer);

for (i = 0; i < length; i++) {
if (l < startIndex + halfLength &&
(r == endIndex || input[l].sort <= input[r].sort))
{
buffer[i] = input[l];
l++;
}
else
{
buffer[i] = input[r];
r++;
}
}

for(i = startIndex; i < endIndex; i++)
input[i] = buffer[int(i - startIndex)];
}
}

/** @private */
internal function getChildEventListeners(object:DisplayObject, eventType:String,
listeners:Vector.<DisplayObject>):void
Expand Down