Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/session/unit_actions.js
Show First 20 Lines • Show All 1,280 Lines • ▼ Show 20 Lines | "unload-all": { | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let entState of entStates) | for (let entState of entStates) | ||||
{ | { | ||||
if (!entState.garrisonHolder) | if (!entState.garrisonHolder) | ||||
continue; | continue; | ||||
if (allowedPlayersCheck([entState], ["Player"])) | if (allowedPlayersCheck([entState], ["Player"])) | ||||
count += entState.garrisonHolder.entities.length; | count += entState.garrisonHolder.entities.length; | ||||
else | else | ||||
for (let entity of entState.garrisonHolder.entities) | for (let entity of entState.garrisonHolder.entities) | ||||
if (allowedPlayersCheck([GetEntityState(entity)], ["Player"])) | if (allowedPlayersCheck([GetEntityState(entity)], ["Player"])) | ||||
Silier: check for state.canGarrison existance or if not needed do GetEntityState(entity).canGarrison. | |||||
++count; | ++count; | ||||
} | } | ||||
Done Inline ActionsIn my tests this is faster. I wonder how much perf we can squeeze out of GetEntity state by using for loops. for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } Test result Poor Man For 0.002200744999572635 ms. Poor man for filter 0.004132418988738209 ms. Reduce 0.006193987999577075 ms. Reduce Poor man for 0.005311861000955105 ms. Reduce Poor man for 0.0031248840107582507 ms. Poor man for Poor man for 0.001599128011148423 ms. Dubious code nodejs let GetEntityState = function(ent) { return { "canGarrison": { "unloadable": ent % 2 == 0 }, } }; const iterations = 100000; class EntityState { constructor() { this.garrisonHolder = undefined; } SetGarrisonHolder(obj) { this.garrisonHolder = obj; } } var entState1 = new EntityState(); let entities = [1, 2, 3, 4, 5, 6, 7, 8] entState1.SetGarrisonHolder({"entities": entities, "buffHeal": 2, "allowedClasses": "Infantry+Ranged", "capacity": entities.length + 1, "garrisonedEntitiesCount": entities.length }) var entStates = []; for (i = 0; i < 50; ++i) { if(i % 3 == 0) entStates.push(new EntityState()); entStates.push(entState1); } console.log("Poor Man For"); let start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) for (let entity of entState.garrisonHolder.entities) { let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } } let duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Poor man for filter"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) count += entState.garrisonHolder.entities.filter(ent =>{ let state = GetEntityState(ent) return state.canGarrison && state.canGarrison.unloadable }).length } duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((r, entState) => r + (entState.garrisonHolder ? (entState.garrisonHolder.entities.filter(ent =>{ let state = GetEntityState(ent) return state.canGarrison && state.canGarrison.unloadable }).length) : 0), 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((total, entState) => { let count = 0; if (!entState.garrisonHolder) return total + 0; for (let entity of entState.garrisonHolder.entities) { let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } return total + count; }, 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((total, entState) => { let count = 0; if (!entState.garrisonHolder) return total + 0; for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } return total + count; }, 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Poor man for Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } } duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); } Stan: In my tests this is faster. I wonder how much perf we can squeeze out of GetEntity state by… | |||||
Done Inline ActionsThat and above also seems unrelated. wraitii: That and above also seems unrelated. | |||||
Done Inline ActionsThe above is to allowe healers as turrets. Freagarach: The above is to allowe healers as turrets.
This is so that one can't think that a turret can be… | |||||
Done Inline ActionsAh, yeah. The above sounds hacky then, this is fine. wraitii: Ah, yeah. The above sounds hacky then, this is fine. | |||||
if (!count) | if (!count) | ||||
return false; | return false; | ||||
return { | return { | ||||
"tooltip": colorizeHotkey("%(hotkey)s" + " ", "session.unload") + | "tooltip": colorizeHotkey("%(hotkey)s" + " ", "session.unload") + | ||||
translate("Unload All."), | translate("Unload All."), | ||||
"icon": "garrison-out.png", | "icon": "garrison-out.png", | ||||
Show All 13 Lines | "unload-all-turrets": { | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let entState of entStates) | for (let entState of entStates) | ||||
{ | { | ||||
if (!entState.turretHolder) | if (!entState.turretHolder) | ||||
continue; | continue; | ||||
if (allowedPlayersCheck([entState], ["Player"])) | if (allowedPlayersCheck([entState], ["Player"])) | ||||
count += entState.turretHolder.turretPoints.filter(turretPoint => turretPoint.entity).length; | count += entState.turretHolder.turretPoints.filter(turretPoint => turretPoint.entity && turretPoint.ejectable).length; | ||||
else | else | ||||
for (let turretPoint of entState.turretHolder.turretPoints) | for (let turretPoint of entState.turretHolder.turretPoints) | ||||
if (turretPoint.entity && allowedPlayersCheck([GetEntityState(turretPoint.entity)], ["Player"])) | if (turretPoint.entity && allowedPlayersCheck([GetEntityState(turretPoint.entity)], ["Player"])) | ||||
++count; | ++count; | ||||
} | } | ||||
if (!count) | if (!count) | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | "garrison": { | ||||
}, | }, | ||||
"allowedPlayers": ["Player"] | "allowedPlayers": ["Player"] | ||||
}, | }, | ||||
"leave-turret": { | "leave-turret": { | ||||
"getInfo": function(entStates) | "getInfo": function(entStates) | ||||
{ | { | ||||
if (entStates.every(entState => !entState.turretable || | if (entStates.every(entState => !entState.turretable || | ||||
entState.turretable.holder == INVALID_ENTITY)) | entState.turretable.holder == INVALID_ENTITY || | ||||
!entState.turretable.ejectable)) | |||||
return false; | return false; | ||||
return { | return { | ||||
"tooltip": translate("Unload"), | "tooltip": translate("Unload"), | ||||
"icon": "garrison-out.png", | "icon": "garrison-out.png", | ||||
"enabled": true | "enabled": true | ||||
}; | }; | ||||
}, | }, | ||||
"execute": function(entStates) | "execute": function(entStates) | ||||
{ | { | ||||
if (!entStates.length) | if (!entStates.length) | ||||
return; | return; | ||||
Engine.PostNetworkCommand({ | Engine.PostNetworkCommand({ | ||||
"type": "leave-turret", | "type": "leave-turret", | ||||
"entities": entStates.filter(entState => entState.turretable && | "entities": entStates.filter(entState => entState.turretable && | ||||
entState.turretable.holder != INVALID_ENTITY).map(entState => entState.id) | entState.turretable.holder != INVALID_ENTITY || | ||||
!entState.turretable.ejectable).map(entState => entState.id) | |||||
}); | }); | ||||
}, | }, | ||||
"allowedPlayers": ["Player"] | "allowedPlayers": ["Player"] | ||||
}, | }, | ||||
"repair": { | "repair": { | ||||
"getInfo": function(entStates) | "getInfo": function(entStates) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 345 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
check for state.canGarrison existance or if not needed do GetEntityState(entity).canGarrison.unloadable