Skip to content

Commit 1116dff

Browse files
Performance Optimizations (nuclear-unicorn#137)
* perf: Prefer static time source This skips creating two new `Date` instances each tick. Minor performance bump. * style: Clean up code around tick handler * refactor: Move policy effect calculation into passive path `calculateTradeBonusFromPolicies` seems generally like the ideal location for the calculation. The existing approach causes heavy computation after every trade, due incurred to meta cache refreshes.
1 parent 88e66cf commit 1116dff

File tree

3 files changed

+56
-60
lines changed

3 files changed

+56
-60
lines changed

game.js

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,63 +4594,59 @@ dojo.declare("com.nuclearunicorn.game.ui.GamePage", null, {
45944594

45954595

45964596
tick: function(){
4597-
/**
4598-
* Even if the game is paused, scheduler should still be able to obtain a focus to handle cases like save/load/reset
4599-
*/
4597+
// Even if the game is paused, scheduler should still be able to obtain a focus
4598+
// to handle cases like save/load/reset
46004599
this.timer.updateScheduledEvents();
4601-
var fpsElement;
46024600

4603-
if (this.undoChange){
4604-
//"Undo button" countdown should still tick while pawsed
4601+
if (this.undoChange) {
4602+
// "Undo button" countdown should still tick while pawsed
46054603
this.undoChange.ttl--;
4606-
if (this.undoChange.ttl <= 0){
4604+
if (this.undoChange.ttl <= 0) {
46074605
this.undoChange = null;
46084606
this._publish("server/undoStateChanged");
46094607
}
46104608
}
4611-
if (this.isPaused){
4612-
this.ui.update(); //Still update UI if the player gathers catnip while pawsed or something
4609+
if (this.isPaused) {
4610+
// Still update UI if the player gathers catnip while pawsed or something
4611+
this.ui.update();
46134612
return;
46144613
}
46154614

4616-
4617-
4618-
var timestampStart = new Date().getTime();
4615+
var timestampStart = Date.now;
46194616

46204617
this.update();
46214618
this.calendar.tick();
46224619
this.ticks++;
46234620

4624-
var timestampEnd = new Date().getTime();
4625-
//if (this.isLocalhost) { //always collect fps metrics
4626-
this.totalUpdateTimeTicks++;
4627-
4628-
var tsDiff = timestampEnd - timestampStart;
4629-
this.totalUpdateTime[this.totalUpdateTimeCurrent] += tsDiff;
4630-
this.totalUpdateTimeCurrent = this.totalUpdateTimeCurrent == 4 ? 0 : this.totalUpdateTimeCurrent + 1;
4631-
4632-
var avg = (this.totalUpdateTime[0] + this.totalUpdateTime[1] + this.totalUpdateTime[2] + this.totalUpdateTime[3] + this.totalUpdateTime[4]) / this.totalUpdateTimeTicks;
4633-
var avg0 = this.totalUpdateTime[0] / Math.floor((this.totalUpdateTimeTicks - 1) / 5);
4634-
var avg1 = this.totalUpdateTime[1] / Math.floor((this.totalUpdateTimeTicks - 2) / 5);
4635-
var avg2 = this.totalUpdateTime[2] / Math.floor((this.totalUpdateTimeTicks - 3) / 5);
4636-
var avg3 = this.totalUpdateTime[3] / Math.floor((this.totalUpdateTimeTicks - 4) / 5);
4637-
var avg4 = this.totalUpdateTime[4] / Math.floor((this.totalUpdateTimeTicks - 5) / 5);
4638-
4639-
if (tsDiff < 10) {
4640-
tsDiff = 10;
4641-
}
4642-
this.fps = {
4643-
ms: tsDiff,
4644-
avg: avg,
4645-
avg0: avg0,
4646-
avg1: avg1,
4647-
avg2: avg2,
4648-
avg3: avg3,
4649-
avg4: avg4
4650-
};
4651-
//}
4621+
var timestampEnd = Date.now;
4622+
4623+
this.totalUpdateTimeTicks++;
4624+
4625+
var tsDiff = timestampEnd - timestampStart;
4626+
this.totalUpdateTime[this.totalUpdateTimeCurrent] += tsDiff;
4627+
this.totalUpdateTimeCurrent = this.totalUpdateTimeCurrent == 4 ? 0 : this.totalUpdateTimeCurrent + 1;
4628+
4629+
var avg = (this.totalUpdateTime[0] + this.totalUpdateTime[1] + this.totalUpdateTime[2] + this.totalUpdateTime[3] + this.totalUpdateTime[4]) / this.totalUpdateTimeTicks;
4630+
var avg0 = this.totalUpdateTime[0] / Math.floor((this.totalUpdateTimeTicks - 1) / 5);
4631+
var avg1 = this.totalUpdateTime[1] / Math.floor((this.totalUpdateTimeTicks - 2) / 5);
4632+
var avg2 = this.totalUpdateTime[2] / Math.floor((this.totalUpdateTimeTicks - 3) / 5);
4633+
var avg3 = this.totalUpdateTime[3] / Math.floor((this.totalUpdateTimeTicks - 4) / 5);
4634+
var avg4 = this.totalUpdateTime[4] / Math.floor((this.totalUpdateTimeTicks - 5) / 5);
4635+
4636+
if (tsDiff < 10) {
4637+
tsDiff = 10;
4638+
}
4639+
this.fps = {
4640+
ms: tsDiff,
4641+
avg: avg,
4642+
avg0: avg0,
4643+
avg1: avg1,
4644+
avg2: avg2,
4645+
avg3: avg3,
4646+
avg4: avg4
4647+
};
46524648

4653-
//collect fps info every minute or so
4649+
// Collect fps info every minute or so
46544650
if (this.ticks % (this.ticksPerSecond * 60) == 0 && this.telemetry) {
46554651
var memory = null;
46564652
if (window.performance && window.performance.memory) {

js/diplomacy.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ dojo.declare("classes.managers.DiplomacyManager", null, {
543543
//Update Trade Stats
544544
this.game.stats.getStat("totalTrades").val = Math.min(this.game.stats.getStat("totalTrades").val + successfullTradeAmount, Number.MAX_VALUE);
545545
this.game.stats.getStatCurrent("totalTrades").val += successfullTradeAmount;
546-
this.game.upgrade({policies : ["sharkRelationsMerchants"]});
547546

548547
return boughtResources;
549548
},
@@ -769,7 +768,12 @@ dojo.declare("classes.managers.DiplomacyManager", null, {
769768
calculateTradeBonusFromPolicies: function(raceName, game){
770769
var tradepostsTradeRatio = game.bld.getBuildingExt("tradepost").meta.effects["tradeRatio"];
771770
var phantomTradeposts = game.diplomacy.calculatePhantomTradeposts(raceName, game);
772-
return phantomTradeposts * tradepostsTradeRatio;
771+
772+
// Apply effects from sharkRelationsMerchants policy.
773+
var trades = game.stats.getStatCurrent("totalTrades").val;
774+
var sharkRelationsMerchantsRatio = Math.min(Math.round((Math.log10(Math.max(trades, 100)) - 1) * 3) / 100, 0.3);
775+
776+
return phantomTradeposts * tradepostsTradeRatio * sharkRelationsMerchantsRatio;
773777
}
774778
});
775779

js/science.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,26 +1451,22 @@ dojo.declare("classes.managers.ScienceManager", com.nuclearunicorn.core.TabManag
14511451
}
14521452
}, {
14531453
name: "sharkRelationsMerchants",
1454-
label: $I("policy.sharkRelationsMerchants.label"),
1455-
description: $I("policy.sharkRelationsMerchants.desc"),
1456-
prices: [
1457-
{name : "culture", val: 2200}
1458-
],
1459-
effects:{
1460-
"tradeRatio" : 0
1461-
},
1462-
unlocked: false,
1463-
blocked: false,
1464-
isRelation: true,
1465-
blocks:["sharkRelationsScribes", "sharkRelationsBotanists"],
1466-
calculateEffects: function(self, game) {
1467-
var trades = game.stats.getStatCurrent("totalTrades").val;
1468-
self.effects["tradeRatio"] = Math.min(Math.round((Math.log10(Math.max(trades, 100)) - 1) * 3) / 100, 0.3);
1454+
label: $I("policy.sharkRelationsMerchants.label"),
1455+
description: $I("policy.sharkRelationsMerchants.desc"),
1456+
prices: [
1457+
{name : "culture", val: 2200}
1458+
],
1459+
effects: {
1460+
"tradeRatio" : 0
14691461
},
1470-
evaluateLocks: function(game){
1462+
unlocked: false,
1463+
blocked: false,
1464+
isRelation: true,
1465+
blocks: ["sharkRelationsScribes", "sharkRelationsBotanists"],
1466+
evaluateLocks: function(game) {
14711467
return game.science.checkRelation("sharks", 20);
14721468
}
1473-
},{
1469+
}, {
14741470
name: "sharkRelationsBotanists",
14751471
label: $I("policy.sharkRelationsBotanists.label"),
14761472
description: $I("policy.sharkRelationsBotanists.desc"),

0 commit comments

Comments
 (0)