(SOLVED) Lag-free depth system

Hi everyone.
I've been using a depth system that works quite nice.
The way it works, is it stores all the objects in the current room in a grid,
using each individual object's x and y value.
The grid is sorted each frame according to these values. When the player moves,
each frame, the entire grid gets sorted.
In the depth object, the draw event of the object's are being controlled (using event_perform(ev_draw,0))
and they only draw after being sorted.

This works great, however, once I get a lot of objects in the room, i experience massive lag.
The reason I think is two-fold:
1) The many objects cause the depthgrid to be very large. Completely sorting this amount of rows in the grid each frame causes lag.
2) And also considering the large amount of objects, each one having its draw event being controlled externally by a depth controller object, could also be laggy.

The depth object I use is a variant of Friendlycosmonaut on YouTube. The code goes like this;

//Grid Logic
var instancenumber = instance_number(OBJ_DepthSystem_PAR);
//(OBJ_DepthSystem_PAR is the parent object of every object that needs depth)

var dsgrid = DepthGrid;
if (ds_grid_height(DepthGrid) != instancenumber)
{ds_grid_resize(dsgrid, 2, instancenumber);}
var yy = 0;
dsgrid[# 0, yy] = id;
dsgrid[# 1, yy] = ((y-id.depthspecific)*room_width) + x;
yy += 1;

//The next line works, but causes massive lag:
ds_grid_sort(dsgrid, 1, true);

//Override draw events of each object and draw them in the correct order:
var inst;
var zz = 0;
//Pull out id
inst = dsgrid[# 0, zz];
//Draw yourself
zz +=1;

A quick note: The "depthspecific" variable is just something I put in every object that needs specific depth. Don't pay too much attention to that, it just means that some sprites might have to "start their depth" at 2 pixels from the bottom, or 3, or 1,...

So my questions are:
How to solve the massive lag when the depthgrid gets really large?
Also, depth = -y does not do what I want to do.
Are there other depth systems that are much more friendly towards processing power?


Are there other depth systems that are much more friendly towards processing power?
You could see if MirthCastle's depth sorting system is suitable for your purpose.


My poor memory seems to tell me it relies essentially on a sufficient number of y coordinate layers. That is, there's a lookup table of layers for y, and when y changes for an instance you set the layer for the instance to the layer corresponding to the new y coordinate. Given that the system relies on the layer system there is no sorting taking place in your own code.