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

[Improvement] Creep.getActiveBodyparts CPU cost correction #152

Open
iamgqr opened this issue Feb 8, 2020 · 0 comments
Open

[Improvement] Creep.getActiveBodyparts CPU cost correction #152

iamgqr opened this issue Feb 8, 2020 · 0 comments

Comments

@iamgqr
Copy link

iamgqr commented Feb 8, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant