screeps/docs

[Improvement] Creep.getActiveBodyparts CPU cost correction

Opened this issue · 0 comments

The CPU cost of Creep.getActiveBodyparts should be "low" instead of "insignificant". Here's my reason.
Went for the engine and found that the actual codes of Creep.getActiveBodyparts is down below,

function _getActiveBodyparts(body, type) {
    var count = 0;
    for(var i = body.length-1; i>=0; i--) {
        if (body[i].hits <= 0)
            break;
        if (body[i].type === type)
            count++;
    }
    return count;
}

This loop can run up to 50 times in total, which may be a little costy and should not be "insignificant". And also, this result is not cached, which is worth noting in the api.
Spotted this problem after using a lot Creep.getActiveBodyparts and it ended up being the most CPU-costing in screeps-profiler.
I'm not a native English speaker, please excuse my grammar mistakes.