Page 1 of 1

Dear devs, can you share your patches for Lua?

Posted: Sat May 09, 2020 1:11 am
by hhrhhr
from help:
Factorio API wrote:Libraries and functions
2. New functions
pairs()

In standard Lua, the order of iteration when using pairs() is arbitrary. Because Factorio has to be deterministic, this was changed in Factorio's version of Lua. Factorio's iteration order when using next(), which pairs() uses for iteration, depends on the insertion order: Keys inserted first are iterated first. However, Factorio also guarantees that the first 1024 numbered keys are iterated from 1-1024, regardless of insertion order. This means that in usual usage, pairs() does not have any drawbacks compared to ipairs().
Interesting things highlighted.
In Lua 5.2+, the iteration order is not deterministic, which leads to different results after each cycle. LuaJIT uses hashes to sort, but the order is always the same.

Example of pairs() output:

Code: Select all

+--------------+---------------+---------------+
!              ! Factorio      ! LuaJIT        !
! local t = {} +---------------+---------------+
! t.aaaa  = 1  ! [500]     = 5 ! [2000]    = 7 !
! t.aa    = 2  ! [1000]    = 3 ! [1000]    = 3 !
! t[1000] = 3  ! ["aaaa"]  = 1 ! ["aaaa"]  = 1 !
! t.a     = 4  ! ["aa"]    = 2 ! ["aa"]    = 2 !
! t[500]  = 5  ! ["a"]     = 4 ! [500]     = 5 !
! t.aaaaa = 6  ! ["aaaaa"] = 6 ! ["a"]     = 4 !
! t[2000] = 7  ! [2000]    = 7 ! ["aaaaa"] = 6 !
+--------------+---------------+---------------+
These changes are very interesting. Is there any way to see them in the form of code / patches / algorithms?

Re: Dear devs, can you share your patches for Lua?

Posted: Sat May 09, 2020 4:26 am
by posila
Code is here: https://github.com/Rseding91/Factorio-Lua
Unfortunatelly, you'll have to diff it against original Lua 5.2.1 yourself.

Re: Dear devs, can you share your patches for Lua?

Posted: Sat May 09, 2020 5:33 am
by hhrhhr
Thank you, I will study and understand.