-
Notifications
You must be signed in to change notification settings - Fork 65
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
Optimization suggestion for _lookAreaMixedRegister() #41
Comments
|
I don't believe it is:
This seems to be the actual elements of And if you look at the actual way the key is used:
Clearly |
I think here Still, iterating over each room object is going to be a bit less overhead than 1000 as in the perf test (and the other part is more than a simple addition). Here's a test which is somewhat more comparable to our workload (though still definitely not exact): https://jsperf.com/screeps-engine-benchmark-test. In Chrome 59.0.3071 / Linux, I get |
Ah, right, my mistake. Well, in order to be sure this is not a micro-optimization, it would be nice to see some concrete benchmarks on real use cases using Node.js 6.x and Node.js 8.x (as we have plans to switch soon). |
Ok, I've written a script using the NPM benchmark package I also had the microtime module installed (instructions in the link above). These were the results on my machine: CPU: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
The script can be found here I should note, when I in-lined the function with the test, I do not have Nodejs version 8, so if someone can run this script on 8 to ensure performance gains on newer versions are similar. |
I've ran the test again on the new version of node:
Node8 appears significantly faster in both cases. And in this case using a normal for loop is 17% faster. These tests are with 50 objects on the map, I've noticed lowering the object count actually leads to a greater performance difference between the two. For example same test on node8 with just 10 objects on the map:
In the case with less objects on the map, the for loop is 23.6% faster then the forEach. |
Thank you for efforts on benchmarking this! So, if I understand this correctly, 1 |
I use the look functions a lot. There are some ticks where I will use it potentially hundreds of times in a single room (this is vital for automated room planning). |
For reference I use lookFor at least 3 times per active creep, and up to around ~13 times for an active military creep. It's more like 90-120 times per owned room for me. But since this is lookForArea / lookForAtArea, I use that much less often. Maybe 4-5 times per military attack creep, and in total around 3 per owned room on average each tick sounds accurate. |
This is used by lookForAtArea and lookAtArea. I'd image that fairly often it's used per-active creep as this is this is used by virtually every creep to look at their surroundings. I should note, that |
As an example how this might affect performance, imagine a builder creep who has the subroutine:
This code will call the function being purposed to be optimized 10 times a tick per builder creep, and is completely reasonable to see players especially newer players make this type of code. Many of creeps use the lookForAtArea to look for things like resources it can sweep up while it walks, or military creeps looking for enemies in attack range. |
In _lookAreaMixedRegister at src/game/rooms.js#L681 the engine is using a .forEach call on the keys of an object.
You should be able to optimize this code quite a bit by converting it to a for loop. It is known that Array.forEach, especially when using closure, has far worst performance (between 50% - 90%) then a standard for loop.
https://jsperf.com/fast-array-foreach
The text was updated successfully, but these errors were encountered: