Skip to content

Global Lua optimization. Part 1#3773

Closed
KionX wants to merge 0 commit intoFAForever:deploy/fafdevelopfrom
KionX:deploy/fafdevelop
Closed

Global Lua optimization. Part 1#3773
KionX wants to merge 0 commit intoFAForever:deploy/fafdevelopfrom
KionX:deploy/fafdevelop

Conversation

@KionX
Copy link
Contributor

@KionX KionX commented Apr 8, 2022

Continued #3511
A total of ~1000 files are expected.

Number of optimizations 4585(globals, math, table, string, exclude ui).

@Garanas
Copy link
Member

Garanas commented Apr 8, 2022

I think we should start simple: only rescope common table and math operations. This is considered a good improvement according to the Lua book in the chapter 'Basic facts'. It is also easy to review and it won't cause any hidden issues.

I agree with your last statement in #3511 that doing this type of work on all the files just makes them a lot more difficult to read. Instead, I think our focus should be on better memory management and refactoring hot-code to improve efficiency. An example of the former is #3743 that has a 10% improvement in performance when you have 900 ASF because of recycling of tables. An example of the latter is #3702 or #3718. where we drastically reduce the amount of Lua byte code generated and prevent table operations as much as possible.

@KionX
Copy link
Contributor Author

KionX commented Apr 8, 2022

At the moment, the total number of microoptimizations is 25699.
Maybe it's worth disabling some optimizations?
The optimizer can easily perform routine optimizations.

I don't plan any further work on the existing FAF code and am considering whole remake FAF.

@Garanas
Copy link
Member

Garanas commented Apr 8, 2022

Yes - I'd say we limit it to:

  • globals (e.g., CreateEmitterAtBone)
  • math functions (as an example, math.abs)
  • table functions (as an example, table.insert)

And leave other engine and lua functions out of it, as they make the code a lot less readable.

@Garanas
Copy link
Member

Garanas commented Apr 8, 2022

And what does a remake of FAF entail? 😄

@KionX
Copy link
Contributor Author

KionX commented Apr 10, 2022

I suggest a couple more optimizations:
Extract imports to global, for k,v in t do -> for k,v in next, t, nil do
Also after optimization, you can rename everything for better readability.
Example of a fully optimized file: Unit.txt
I think we should integrate the optimizer into the development process.

The main idea of the FAF remake is that everything will be written in LuaJIT x64.
If necessary, other functions are added via Lua dll modules.
Graphics - DX9 (this is the simplest)
I care about the engine. Hope others will do the rest.

@Garanas
Copy link
Member

Garanas commented Apr 10, 2022

Extract imports to global, for k,v in t do -> for k,v in next, t, nil do
I was not aware of this one, I did a few benchmarks. I do not notice a significant difference, how much do you think this change is faster?

Also after optimization, you can rename everything for better readability.
That is not viable / maintainable in the long term, especially if you want to integrate the optimizer.

I'd be fine with this list:

  • globals (e.g., CreateEmitterAtBone)
  • math functions (as an example, math.abs)
  • table functions (as an example, table.insert)
  • k,v in t do -> for k,v in next, t, if it is faster

Anything else is not required for the average code and can lead to errors down the route. As an example, if you'd optimize the brain and use GetUnitsAroundPoint or GetListOfUnits directly then you'd skip the Lua versions of those functions. The same applies to Lua functions in general, as mods / maps can override those (without hooking the file, quite destructive) and we'd still have a reference to the old function.

I care about the engine. Hope others will do the rest.
I don't know what type of changes this would require but I'd be open to discuss it and help you with it.

@KionX
Copy link
Contributor Author

KionX commented Apr 11, 2022

Number of optimizations 4585(globals, math, table, string, exclude ui).
This PR is completed.

@Garanas
Copy link
Member

Garanas commented Apr 11, 2022

This is looking good. Surprised to see that the number of optimizations is still so high. Good idea to include the string functions too.

I'll look through it and experiment with it. Great work.

@KionX
Copy link
Contributor Author

KionX commented Apr 11, 2022

Think about using this https://github.com/FAForever/FAFLuaJit for the lua test.
Example: https://github.com/FAForever/LOCChecker

files = io.dir(dir)
for i,v in files do
s, e = loadfile(dir .. v)
if not s then
  io.stdout(e)
  os.exit(1)
end

@KionX
Copy link
Contributor Author

KionX commented May 29, 2022

What's with this PR?

@Garanas
Copy link
Member

Garanas commented May 29, 2022

I've been testing it on the benchmark map and did not notice a significant change. I'm not sure if it is worth it.

@KionX
Copy link
Contributor Author

KionX commented May 29, 2022

If it does not spoil the convenience, then it will not be worse.

@KionX KionX closed this Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants