GML Huge FPS drops when searching ds_lists?

S

shadow7692

Guest
I have the code below in the Draw GUI event, where Target is an instance id, and has a instance variable of Inventory which is a ds_list. When this is executed I get HUGE fps drops, and I'm fairly certain it's because iterating through the whole ds_list every draw step is very memory consuming.

How would I go about changing this so that it doesn't? I would still need to draw everything based on an array of some sort.

Code:
if (!ds_list_empty(Target.Inventory)) {
    for (var i = 0; i < ds_list_size(Target.Inventory); i++) {
        var Item = Target.Inventory[| i]
        if (Item[2] == false) { // not equipped
            draw_set_halign(fa_right);
            draw_text_transformed(x-410+i*88, y+65, Item[1], 1.7, 1.6, 0); // show amount of the item
        };
    };
};
 

chamaeleon

Member
I have the code below in the Draw GUI event, where Target is an instance id, and has a instance variable of Inventory which is a ds_list. When this is executed I get HUGE fps drops, and I'm fairly certain it's because iterating through the whole ds_list every draw step is very memory consuming.

How would I go about changing this so that it doesn't? I would still need to draw everything based on an array of some sort.

Code:
if (!ds_list_empty(Target.Inventory)) {
    for (var i = 0; i < ds_list_size(Target.Inventory); i++) {
        var Item = Target.Inventory[| i]
        if (Item[2] == false) { // not equipped
            draw_set_halign(fa_right);
            draw_text_transformed(x-410+i*88, y+65, Item[1], 1.7, 1.6, 0); // show amount of the item
        };
    };
};
Are you saying that the fps would not drop if you were to, say, comment out the two draw calls? Under the assumption that GMS doesn't optimize away the loop because no changes are made at that point..
 

Ricardo

Member
If the list isn't long, it shouldn't impact performance so badly, I think. I'd run the debugger and use the profiler to check what's going on.
Btw, you could remove draw_set_halign(fa_right) out of the for loop. No reason to call it on every iteration if you can call it only once and get the same result.
 
If the list isn't long, it shouldn't impact performance so badly, I think. I'd run the debugger and use the profiler to check what's going on.
Agreed. Simply iterating through a list shouldn't cause major slowdowns, so the issue may be rooted elsewhere.

If iterating through the list per frame is really causing such a huge performance issue, I'd say draw everything once to a surface and only update it if something in the inventory changes. This way you're only doing the iteration on creation and updates.
 
S

shadow7692

Guest
Agreed. Simply iterating through a list shouldn't cause major slowdowns, so the issue may be rooted elsewhere.

If iterating through the list per frame is really causing such a huge performance issue, I'd say draw everything once to a surface and only update it if something in the inventory changes. This way you're only doing the iteration on creation and updates.
Ah, the issue was rooted elsewhere. Thanks for all the help.
 
Top