Reduce memory overhead of RoomPosition#87
Conversation
|
This is interesting. Looks like a memory-CPU tradeoff to me. It would be useful if someone can do benchmarking on their private servers with complex world setup and AI logic, to track how it affects both heap and CPU. |
|
I've applied this patch on S+, my small 2 room empire has gone from ~45MB heap size to ~ 42MB EDIT: Updated value, it jumped back to ~42MB and has been holding solid for the past few minutes. |
|
@ags131 what about other players on S+? |
|
@artch Nobody else who has responded so far collect stats on S+, any my current stats collection doesn't include heaps yet, looking back at the stats that were collected, (User CPU and Dirty time), the CPU graphs aren't showing a noticeable change in the user averages. |
There's no reason to bring floating points into this.
|
Updated this against latest master. |
How does rp.getRangeTo() respond to this change? I've just grep'd my code and I use isEqualTo 13 times but getRangeTo 341 times. |
|
|
|
@wtfrank most methods become nominally more expensive but my gut is that it won't be observable. Reduced heap pressure should be worth it. Phase 2 of this change would be to add |
|
@CaptEmulation |
@laverdet Here's what I get in Node 8: |
|
Your example isn't correct because you're using defineProperty directly on the object, not on that object's prototype chain. function thing() {
}
thing.prototype.property = 1;
let that = new thing;
for (let key in that) {
console.log(key, that[key]);
}
let copy = Object.assign({}, that);
for (let key in copy) {
console.log(key, copy[key]);
}Logs: |
List of known breaking changes
Object.create(RoomPosition.prototype, ...)-- please usenew RoomPositionObject.assign({}, rp)- properties on RoomPosition are no longer "own properties" so they won't be picked up by functions that filter on own propertiesrp.x = -1; rp.y = 50-- invalid values are no longer supported on RoomPositionrp.roomName = 'e1s1'-- lowercase room names will be converted to uppercaseI'm not sure if this will be a performance win or not. This modifies
RoomPositionto internally only store a single 32-bit value instead of two numbers and a string. Getters and setters are implemented so that the interface remains the same. The biggest observable difference is that nowx,y, androomNameare not "own properties" anymore so something likeObject.assign({}, rp)won't behave the same.JSON.stringify(rp)still works as expected. Invalid values for the 3 properties will now throw if you try to set them. So you can't dorp.x = -1orrp.roomName = 'foo'.rp.isEqualTo()becomes a lot faster.utils.getRoomNameFromXYis modified to always return an existing memoized string instead of constructing the string on the fly.utils.roomNameToXYis modified to use simple string ops and math instead of a regex to improve speed, it's 2-3 times faster now. The return value in case of an invalid input is changed from[undefined,undefined]to[NaN,NaN]but a quick audit of the code suggests this won't be a problem.My world shows a net heap decrease of just 30kb but I only have 2 creeps and a spawn.
Edit: And obviously now since these are accessors instead of properties that may affect runtime speed. It's unclear if that overhead will be overcome by the reduced memory footprint.