factoriolib / flib

A set of high-quality, commonly-used utilities for creating Factorio mods.
https://mods.factorio.com/mod/flib
MIT License
61 stars 15 forks source link

gui-beta optimization #27

Closed JanSharp closed 3 years ago

JanSharp commented 3 years ago

Some improvements to gui-beta gui building. The major one is the first commit Prepare gui structure before adding to parent. The other 3 commits are optional, you can cherry pick whatever you want out of those. I think Improve gui refs building is generally a good one and the other ones depend on preference. They all don't make a huge difference.

As mentioned on discord, the reason this is so much faster (10-30% depending on the tests i did) is because the game doesn't have to convert the entire children tree into a property tree when adding all the elements. Additionally the tags are prepared before the add call instead of afterwards, sometimes even with 2 more reads of the tags and 2 more writes, which is a lot of table building and parsing for no reason other than reusing functions that already exist, but in the first commit you can see that it's a very tiny amount of "code duplication", if you can even call it that.

raiguard commented 3 years ago

I'm ok with the changes, the only concern I have is that now the code is modifying the tables that are passed in. This could be problematic if someone intends to re-use the table for something else. But that is an edge-case that I'm willing to set aside for the much better performance.

Thanks!

JanSharp commented 3 years ago

Oh i see what you mean, didn't notice that. (i saw elem.tags = {} and thought it was resetting them anyway, but that was wrong). Restoring it afterwards is really cheap so might as well.

raiguard commented 3 years ago

Thank you!