Page MenuHomeWildfire Games

WIP Refactor playerPlacement functions (another approach)
Needs ReviewPublic

Authored by marder on Feb 18 2024, 8:48 PM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

alternative to D5198

just some WIP experiments

Test Plan

.

Diff Detail

Lint
Lint Skipped
Unit
Unit Tests Skipped

Event Timeline

marder requested review of this revision.Feb 18 2024, 8:48 PM
marder created this revision.
sera added a subscriber: sera.Feb 18 2024, 9:29 PM
sera added inline comments.
binaries/data/mods/public/maps/random/rmgen-common/player.js
611

PlayerPlacement and why not class syntax?

marder retitled this revision from WIP Refactor playerPlacement functions (another apporach) to WIP Refactor playerPlacement functions (another approach).Feb 18 2024, 9:53 PM
marder edited the summary of this revision. (Show Details)
marder added inline comments.Feb 18 2024, 10:00 PM
binaries/data/mods/public/maps/random/rmgen-common/player.js
611

PlayerPlacement good point


why not class syntax

no really good reason, mostly because we generally use this syntax a lot probably because of historic reasons and, as mdn says: "very fast"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain#with_constructor_functions

I can also change it tho, I don't have a strong opinion here.

sera added inline comments.Feb 18 2024, 10:10 PM
binaries/data/mods/public/maps/random/rmgen-common/player.js
611

"very fast"

Depends also on version and JIT, don't think the answer is that simple, but mostly you won't care unless you call it thousands of time in a loop.

So seeing as more and more code migrates to class syntax it would also be my preference here.

Stan added subscribers: bb, Stan.Feb 19 2024, 1:23 AM
Stan added inline comments.
binaries/data/mods/public/maps/random/rmgen-common/player.js
611

I suppose it should be profiled but back a few SM version @bb noticed it was significantly slower to use the class syntax, hence why we refrain from using it in RMGEN and Simulation.

That discussion should be somewhere either on phab, or in the irc logs.

The only thing I could find on phab was this:
https://code.wildfiregames.com/D4021#173216

Generally PlayerPlacement should be called at most twice per-map creation, so the overhead should be ok, but I can do some tests as well.

But also, just from the point of keeping an (somewhat) consistent codebase... Should we just stick to xy.prototype.method ?

at least from a simple test (just object creation and setting a value) the performance looks equal. Not sure what happens in complex scenarios tho.

sera added a comment.Feb 19 2024, 1:04 PM

at least from a simple test (just object creation and setting a value) the performance looks equal. Not sure what happens in complex scenarios tho.

That's the most likely outcome.

https://medium.com/javascript-scene/why-composition-is-harder-with-classes-c3e627dcd0aa suggest classes can even be used for performance reason and lower memory consumption. Therefore blindly using bb's benchmark on an old SM as the general rule for all the future uses doesn't strike me as a good idea.

marder updated this revision to Diff 22789.Feb 19 2024, 1:27 PM

unrelated to the class / prototype discussion, just a quick update to make it somewhat functional.
At least mainland generates now.

sera added inline comments.Feb 19 2024, 1:34 PM
binaries/data/mods/public/maps/random/rmgen-common/player.js
804

Line -> line

Stan added a comment.Feb 19 2024, 2:11 PM

A better solution indeed would be to profile :)

I don't like this interface:
If one wants a PlayerPlacement one would have to call two functions.
That's a two step initialization. (Which is bad IMO)

It's uncommon to store the result in this instead of returning it. The dataflow is hidden.

I don't like this interface:
If one wants a PlayerPlacement one would have to call two functions.
That's a two step initialization. (Which is bad IMO)

Its indeed two step. But it enables

const placement = new PlayerPlacement();
placement[g_MapSettings.TeamPlacement](args);

which would make playerPlacementByPattern obsolete. I mainly wanted to try this since you mentioned here: https://code.wildfiregames.com/D5198#221356
That we could put the function into an object to enable this syntax (but I didn't liked the idea to create another global object for this).

It's uncommon to store the result in this instead of returning it. The dataflow is hidden.

I'll take your word for it. I have seen stuff like this, but I can't say if it is common or uncommon.
It definitely isn't what you would do in a functional approach, but in oop (as far as my understanding goes) it would fit into the "Command-query separation" principle.

As option C we can just stick to individual functions that return an object of class PlayerPlacement, instead of methods.

Idk, I have a hard time to decide what is the more maintainable / clear. So I'm open for opinions.

(but I didn't liked the idea to create another global object for this).

Well now there is also a global object but it's hidden in PlayerPlacement.prototype.

If you really want to go without the global object you'd have to return it from PlayerPlacement(). But that's even worse IMO.