Page MenuHomeWildfire Games

Fix style, Cc and slightly refactor ProductionQueue
ClosedPublic

Authored by Silier on Dec 14 2019, 10:36 PM.

Details

Summary

Fix redeclarations of variables
Fix variable shadowing
Remove duplicated cmpPlayer
Move querying of the same components out of loop
Change if (foo.length > 0) to if (foo.length)
Change if (foo == 0) to if(!foo)
Fix remaining var -> let
Remove white space from empty object { } to {}
Restyle more complicated objects.

Test Plan

Jenkins does not report anything about variables redeclaration and shadowing.
Check nothing is broken.

Diff Detail

Repository
rP 0 A.D. Public Repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/765/display/redirect

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1281/display/redirect

elexis added a subscriber: elexis.Dec 15 2019, 1:41 AM

Fix redeclarations of variables, remove duplicated cmpPlayer and fix remaining var->let

Should mention that ProductionQueue is the subject of the patch, so that multiple patches changing var to let can be distinguished by the title.

binaries/data/mods/public/simulation/components/ProductionQueue.js
2 ↗(On Diff #10602)

Perhaps move those two into the prototype

202 ↗(On Diff #10602)

coding convention is to use ++i except where the return value matters

366 ↗(On Diff #10602)

(spaces between operators)

377 ↗(On Diff #10602)

(Wondering if the two if (type == cases should go to helper functions, but scope creep I guess)

491 ↗(On Diff #10602)
return this.queue.map(item => ({
...
}))
522 ↗(On Diff #10602)

(&& ... .length) if you will

747 ↗(On Diff #10602)

{ } -> {} if it needs a value, same L730

765 ↗(On Diff #10602)

If objects are written this way:

let notification = {
	 "players": [cmpPlayer.GetPlayerID()],
	"message": markForTranslation("Can't find free space to spawn trained units"),
	"translateMessage": true
};

it has the advantage that the reader can count and locate properties without reading all property values.

Also the notification variable can be inlined.

Silier updated this revision to Diff 10608.Dec 15 2019, 4:36 PM
Silier retitled this revision from Fix redeclarations of variables, remove duplicated cmpPlayer and fix remaining var->let to Fix style, Cc and slightly refactor ProductionQueue.
Silier edited the summary of this revision. (Show Details)

Build failure - The Moirai have given mortals hearts that can endure.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/768/display/redirect

Build failure - The Moirai have given mortals hearts that can endure.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1284/display/redirect

Silier updated this revision to Diff 10610.Dec 15 2019, 5:02 PM

i am not sure changes around ApplyValueModificationsToEntity functions are correctly done

Build failure - The Moirai have given mortals hearts that can endure.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/769/display/redirect

Build failure - The Moirai have given mortals hearts that can endure.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1285/display/redirect

Silier updated this revision to Diff 10611.Dec 15 2019, 5:32 PM
Silier edited the summary of this revision. (Show Details)

fix tests

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/770/display/redirect

nani added a subscriber: nani.Dec 15 2019, 5:34 PM
nani added inline comments.
binaries/data/mods/public/simulation/components/ProductionQueue.js
726–728 ↗(On Diff #10610)

time can become negative time -= item.timeRemaining so time >0 is needed

Silier added inline comments.Dec 15 2019, 5:37 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
2 ↗(On Diff #10602)

does that not mean x times more variables where x is number of entities with production queue in game?

Successful build - Chance fights ever on the side of the prudent.

Linter detected issues:
Executing section Source...
Executing section JS...
|    | [NORMAL] ESLintBear (indent):
|    | Expected indentation of 3 tabs but found 4.
|----|    | /zpool0/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
|    |++++| /zpool0/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
| 283| 283| 				    +template.Promotion.RequiredXp,
| 284| 284| 				    cmpPlayer.GetPlayerID(),
| 285| 285| 				    template)
| 286|    |-				)
|    | 286|+			)
| 287| 287| 			{
| 288| 288| 				this.AddBatch(template.Promotion.Entity, type, count, metadata);
| 289| 289| 				return;
|    | [NORMAL] ESLintBear (no-multi-spaces):
|    | Multiple spaces found before 'Engine'.
|----|    | /zpool0/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
|    |++++| /zpool0/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
| 721| 721| 	// with items that take fractions of a second)
| 722| 722| 	let time = g_ProgressInterval;
| 723| 723| 	let cmpPlayer = QueryOwnerInterface(this.entity);
| 724|    |-	let cmpTemplateManager =  Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
|    | 724|+	let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
| 725| 725| 
| 726| 726| 	while (time > 0 && this.queue.length)
| 727| 727| 	{
Executing section cli...

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1286/display/redirect

In D2470#103529, @Angen wrote:

i am not sure changes around ApplyValueModificationsToEntity functions are correctly done

I didn't check for semantic changes, but the style changes look okay.

binaries/data/mods/public/simulation/components/ProductionQueue.js
182 ↗(On Diff #10611)

for...of if you want

286 ↗(On Diff #10611)

-\n

453 ↗(On Diff #10611)

perhaps for...of loop (I heard it was said it's slower, but what's the data for that actually, i.e. does that also include the cost for this.queue[i] lookup, and is it significant enough? I guess this function is only called per user click so its negligible here anyway but the iteration difference may still be relevant for code that is run often)

468 ↗(On Diff #10611)

inline unitCategory

740 ↗(On Diff #10611)

+1 tab should be enough

815 ↗(On Diff #10611)

(I've always used +1 tab instead of tabs for object properties, but its not set in stone I guess)

2 ↗(On Diff #10602)

Nope, its stored in the prototype of the object, just like the functions.

The component objects have owned properties and the prototype property which an object reference.
When accessing a JS object property it checks first if the object has that property, after that it checks if the prototype has that property, then it checks if the prototype of the prototype has that property etc.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

elexis added inline comments.Dec 16 2019, 3:11 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
2 ↗(On Diff #10602)

(Trivia: One could run some turns of a match with -ooslog and see the text representation of the savegame to see if these variables would end up in the savegame. The savegame only writes owned properties (see the JS_Enumerate call in CBinarySerializerScriptImpl::HandleScriptVal which https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_Enumerate If they were present in that file for each ProductionQueue entity component then it'd be stored per class instance. If they're not in the file, it wouldn't rule out that there is still one copy of the number per class instance in memory, so for that we have to either trust the specs or read the spidermonkey code)

elexis added inline comments.Dec 16 2019, 3:16 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
2 ↗(On Diff #10602)

(Other than that usually the first association of hardcoded literals in simulation components I have is to move them to the template files, since the purpose of the template files is to store all the values of simulation components to begin with
(I suppose a constant should be moved to a template if it makes sense to have at least 2 different templates with different values and in the prototype if we know for sure that the property or restriction is one of the code itself and thus applying equally to all instances. I guess it's still better to move them to the prototype than not moving them at all, considering the somewhat ugly letter cases, whatever)

Silier planned changes to this revision.Dec 19 2019, 10:08 PM

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/894/display/redirect

Silier added inline comments.Dec 27 2019, 1:17 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
453 ↗(On Diff #10611)

no, we need i for L498

815 ↗(On Diff #10611)

that are space not tabs :)
(For any alignment within a line of code (as opposed to indentation at the start), use spaces, not tabs.)
Or did I get that wrong ?

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1410/display/redirect

elexis added a comment.EditedDec 27 2019, 5:05 PM

(Still not a fan of the period additions, I guess I don't have to be. What we ought to strive for is to gain the most quality change per line of code changed, and if we don't add anything useful to a hunk we get more quality/line of code changed if we remove the diff. But if you guys want that period I wont stop you in your diffs)

binaries/data/mods/public/simulation/components/ProductionQueue.js
180 ↗(On Diff #10797)

space after { and before }
supercede -> supersede

-a
-by

259 ↗(On Diff #10797)

(ideally there should be no speculation in code)

499 ↗(On Diff #10797)

(I suppose its slightly more performant to pass undefined than creating a garbage-collection tracked empty object, I suppose semantic changes are out of scope of this diff)

601 ↗(On Diff #10797)

cmpPosition is only needed in one of the cases, so (unless the JIT optimizer is smart), it would become less performant for no benefit or did I miss something?

609 ↗(On Diff #10797)

// so

716 ↗(On Diff #10797)

1000msecs shouldnt be hardcoded in the comment

747 ↗(On Diff #10797)

(Comment redundant with function name?)

796 ↗(On Diff #10797)

(This is not a sentence, so the period would be grammatically wrong?)

880 ↗(On Diff #10797)

// If
-.=

815 ↗(On Diff #10611)

Its one convention people recommend, I personally didn't like nor use it (as 4 spaces require 4 more keypresses to insert and navigate and provide more surface area for too many or too few indentation). I don't care much, only problem is that if we dont use the same CC then we'll have inconsistent code, one patch adding it this way the next the other way, perhaps even some patches changing the same lines back and forth, so in theory it should become conventional or perhaps the consequences of different styles aren't so impactful, whatever)

2 ↗(On Diff #10602)

^ This, not?

Silier added inline comments.Dec 31 2019, 12:45 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
601 ↗(On Diff #10797)

One of the cases is in for loop and it is when unit is not going to be auto-garrisoned what happens in most cases when spawning units

Stan added a subscriber: Stan.Dec 31 2019, 1:33 PM

I wonder if all the ApplyValueModification calls affecting other components shouldn't be functions in said components.They would be cached when possible and updated on ValueModification.

Silier updated this revision to Diff 10833.Dec 31 2019, 4:14 PM

update some comments and move globals to prototype

In D2470#105465, @Stan wrote:

I wonder if all the ApplyValueModification calls affecting other components shouldn't be functions in said components.They would be cached when possible and updated on ValueModification.

in GetBatchTime could get cached, but i am not sure about GetTechCostMultiplier (because number of different resources) and others are modification to templates and that are no go, as that would probably result in a lot of used memory if done.

Stan added a comment.EditedDec 31 2019, 4:22 PM

You're right. I just like component to handle their stuff.

binaries/data/mods/public/simulation/components/ProductionQueue.js
342 ↗(On Diff #10833)

Variable? Dunno if it is smart enough to not compute the multiplication twice.

373 ↗(On Diff #10833)

Like here.

587 ↗(On Diff #10833)

delete this.timer?

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1435/display/redirect

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/917/display/redirect

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/macos-differential/12/display/redirect

You are right about cmpPosition being more performant if its moved out of the loop (in the average case at least).

From a brief look, the patch looks okay.

binaries/data/mods/public/simulation/components/ProductionQueue.js
526 ↗(On Diff #10833)

It seems like a big waste to recreate the array and every item of it when it seems only one property of every object changes, or did I miss something?

I suppose it's cloning the thing to avoid modification. It could avoid doing so by letting the consumer of the return value make that one computation.

It also opens up the door for the consumer to unintentionally modify the object, but that probably should have lower precedence than copying it. From a quick glance, it doesn't seem to be used that rarely (AIProxy, GetEntityState).

(Its not a regression if its not addressed in this revision.)

Silier added inline comments.Jan 2 2020, 1:41 PM
binaries/data/mods/public/simulation/components/ProductionQueue.js
587 ↗(On Diff #10833)

timer is cancelled and what is in variable this.timer does not matter because this entity is going to be destroyed

Silier updated this revision to Diff 10841.Jan 2 2020, 1:43 PM

move * 1000 to variable as variable is not used elsewhere

Vulcan added a comment.Jan 2 2020, 1:46 PM

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/924/display/redirect

Vulcan added a comment.Jan 2 2020, 1:47 PM

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/macos-differential/19/display/redirect

Vulcan added a comment.Jan 2 2020, 1:51 PM

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/docker-differential/1442/display/redirect

Stan added a comment.EditedJan 2 2020, 5:47 PM

The patch looks good now.

binaries/data/mods/public/simulation/components/ProductionQueue.js
298 ↗(On Diff #10841)

Move below? Inline?

This revision was not accepted when it landed; it landed in state Needs Review.Jan 2 2020, 9:29 PM
This revision was automatically updated to reflect the committed changes.