1. Post #1
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    ATTENTION!
    I've recently updated and re-purposed this module. It used to implement LuaJIT 1.1.7 (or 2.0.0) into garrysmod years ago, but garrysmod has since implemented LuaJIT 2.0.0. The module now implements LuaJIT 2.0.3 into garrysmod, but it won't be long before it becomes obsolete again.

    http://dl.dropbox.com/u/20789739/gmod_luajit.rar

    The installation instructions haven't changed, so read below.

    Old post preserved below:
    For those who don't know, LuaJIT is a library that runs Lua code much faster than the standard interpreter. This module detours every single Lua API function that exists in lua_shared.dll with ones from LuaJIT 1.1.7 to give Garry's Mod a performance boost. I modified LuaJIT 1.1.7 with the same syntax changes that gmod Lua has, even the bitwise operators (I had to write assembly to make that work). I also included the coroutine fix I posted before in the "What are you working on?" thread.

    http://dl.dropbox.com/u/20789739/gmod_luajit.rar

    There is also a WIP LuaJIT 2.0.0 module in there with the name 'gmod_luajit2'

    Installation:
    For dedicated servers:
    Place the module somewhere in the addons folder and create a *.vdf for it. This sample *.vdf is placed directly in the addons folder and assumes the module is also in the addons folder:
    Code:
    "Plugin"
    {
    	"file"	"../garrysmod/addons/gmod_luajit"
    }
    For Garry's Mod clients:
    Place the module in the 'garrysmod/garrysmod/bin folder, and prefix the name with 'game_shader_generic_'. Example name:
    Code:
    game_shader_generic_gmod_luajit.dll
    Usage:
    Nothing special is required to use LuaJIT once it is installed, however, you may be interested in the extra library added by LuaJIT.

    Didn't someone already make a LuaJIT module?
    Yes, but mine doesn't require any special code to use.

    How compatible is this with other modules?
    If the other module detours/calls lua API functions from lua_shared.dll, then it will conflict. However, you could change the conflicting module yourself so that it detours/calls the functions from this module instead of lua_shared.dll, and it should work fine. The module exports the LuaJIT API functions to make this easier.

    If you have problems with the module, let me know. Just don't bug me if your scripts error because 'arg' was nil. The implicit use of 'arg' in vararg functions is deprecated and not supported in LuaJIT.
    Reply With Quote Edit / Delete Reply Windows XP United States Show Events Lua King x 24Winner x 5Optimistic x 2Useful x 2Friendly x 2 (list)

  2. Post #2
    Amazing work, man!
    Garry must see.
    Reply With Quote Edit / Delete Reply Windows 8 Russian Federation Show Events Optimistic Optimistic x 1 (list)

  3. Post #3
    garry's Avatar
    September 2001
    12,410 Posts
    Nice work - any performance comparisons?
    Reply With Quote Edit / Delete Reply Windows 7 Show Events Agree x 11Funny x 3Useful x 1Artistic x 1Optimistic x 1Informative x 1Friendly x 1 (list)

  4. Post #4
    Noi
    Purrr ~
    Noi's Avatar
    February 2010
    1,239 Posts
    Too sad that garry removed 'plugin_load'.
    Reply With Quote Edit / Delete Reply Windows 7 Russian Federation Show Events Agree Agree x 2Funny Funny x 1 (list)

  5. Post #5
    Gold Member
    BomBom's Avatar
    April 2005
    263 Posts
    I can't seem to get this plugin to load :/
    Always get this error when the server starts.

    Code:
    Unable to load plugin "../garrysmod/addons/gmod_luajit"

  6. Post #6
    gamerpaddy's Avatar
    March 2009
    366 Posts
    Code:
    Garry's Mod server.dll Build #130 - Linux
    [Dec  1 2011 11:14:41]
    Unable to load plugin "../garrysmod/addons/gmod_luajit"
    Linux... :(

  7. Post #7
    Gold Member
    Robber's Avatar
    January 2006
    6,138 Posts
    Didn't garry already add LuaJIT to GMod and removed it again after it started crashing GMod?
    Reply With Quote Edit / Delete Reply Windows 7 Austria Show Events Agree Agree x 4 (list)

  8. Post #8
    Gold Member
    BomBom's Avatar
    April 2005
    263 Posts
    Code:
    Garry's Mod server.dll Build #130 - Linux
    [Dec  1 2011 11:14:41]
    Unable to load plugin "../garrysmod/addons/gmod_luajit"
    Linux... :(
    A Linux version would be nice but it doesn't even work on my windows servers.

  9. Post #9
    "file" "../garrysmod/addons/gmod_luajit"
    Replace "garrysmod" with "garrysmodbeta".
    Or try just typing "file" "addons/gmod_luajit" instead.
    Reply With Quote Edit / Delete Reply Windows 8 Russian Federation Show Events Disagree Disagree x 2 (list)

  10. Post #10
    Title

    August 2009
    300 Posts
    I'd love to see some performance comparisons, I remember the old one but didn't use it enough to test.

  11. Post #11
    Gold Member
    BlackAwps's Avatar
    July 2011
    765 Posts
    Would love to see a linux build.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 2 (list)

  12. Post #12
    I made WAYWO a better place
    OldFusion's Avatar
    September 2011
    1,311 Posts
    Didn't garry already add LuaJIT to GMod and removed it again after it started crashing GMod?
    Yea something to do with try/catch statements in combination with LuaJIT would cause it to break on valve's DirectX 7.0 Implantation, and also a lack of var_arg support.

    Worked fine server side however.

    http://lua-users.org/lists/lua-l/2010-10/msg00514.html

  13. Post #13
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    I can't seem to get this plugin to load :/
    Always get this error when the server starts.

    Code:
    Unable to load plugin "../garrysmod/addons/gmod_luajit"
    You might be missing a dependency. Make sure you have the latest Visual C++ 2005 Redistributable installed.

    Edited:

    Would love to see a linux build.
    I can't even begin to think about a linux build unless I can find the linux server binaries, which seem to be missing from the dedicated server install. I vaguely remember the binaries being posted on the forums, but I can't seem to find it. I would also need a cross-platform detours library (or at least one that works on linux)

  14. Post #14
    Gold Member
    Megalan's Avatar
    October 2005
    561 Posts
    1.1.7? Why not 2.0? It's a lot faster. Gonna try and update it.
    Reply With Quote Edit / Delete Reply Windows 7 Russian Federation Show Events Dumb Dumb x 1 (list)

  15. Post #15
    Gold Member
    BlackAwps's Avatar
    July 2011
    765 Posts
    I can't even begin to think about a linux build unless I can find the linux server binaries, which seem to be missing from the dedicated server install. I vaguely remember the binaries being posted on the forums, but I can't seem to find it. I would also need a cross-platform detours library (or at least one that works on linux)
    The best I can do is upload the linux binaries for you.

    http://dl.dropbox.com/u/38214687/lin_mac_bins.rar
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 1 (list)

  16. Post #16
    Nexus435's Avatar
    July 2010
    1,461 Posts
    Yea something to do with try/catch statements in combination with LuaJIT would cause it to break on valve's DirectX 7.0 Implantation, and also a lack of var_arg support.

    Worked fine server side however.

    http://lua-users.org/lists/lua-l/2010-10/msg00514.html
    Who is still using DX7?
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 9Dumb Dumb x 1Funny Funny x 1 (list)

  17. Post #17
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    Nice work - any performance comparisons?
    I haven't run any, but the official site has a comparison chart between standard Lua, the stable version of LuaJIT, and the development version of LuaJIT. http://luajit.org/performance_x86.html

    1.1.7? Why not 2.0? It's a lot faster. Gonna try and update it.
    Development versions tend to be less stable, and gmod crashes enough already. I didn't want to add to that. I could stick the new version in if you really want it, though.

    The best I can do is upload the linux binaries for you.

    http://dl.dropbox.com/u/38214687/lin_mac_bins.rar
    Thanks for that. It appears that the linux binaries don't have symbols like I've heard they do, so it will probably be a while before I decide to start disassembling it to look for function signatures.

  18. Post #18
    Gold Member
    BlackAwps's Avatar
    July 2011
    765 Posts
    Thanks for that. It appears that the linux binaries don't have symbols like I've heard they do, so it will probably be a while before I decide to start disassembling it to look for function signatures.
    That's why I included the mac binaries. I'm pretty sure it will have the symbols you would need for linux.

  19. Post #19
    Gold Member
    Megalan's Avatar
    October 2005
    561 Posts
    Development versions tend to be less stable
    I have several applications developed using latest beta of luajit running 24/7 for at least several months. None of them crashed yet.
    Reply With Quote Edit / Delete Reply Windows 7 Russian Federation Show Events Agree Agree x 3Informative Informative x 1 (list)

  20. Post #20
    I made WAYWO a better place
    OldFusion's Avatar
    September 2011
    1,311 Posts
    The LuaJIT development version are rather stable.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 4 (list)

  21. Post #21
    CapsAdmin's Avatar
    August 2005
    3,974 Posts
    yeah

    i've never had any issues with the 2.x version of luajit in my projects either
    Reply With Quote Edit / Delete Reply Windows 7 Norway Show Events Agree Agree x 2 (list)

  22. Post #22
    pennerlord's Avatar
    February 2011
    503 Posts
    Made a shitty test with this script:

    local t = SysTime()
    
    for i=1, 2000 do
    	print(i)
    end
    
    print(SysTime() - t)
    

    I have run it 5 times (each with and without luajit). My results:

    Without luajit:
    Code:
    0.02301025390625
    0.0230712890625
    0.02398681640625
    0.0235595703125
    0.02166748046875
    With luajit:
    Code:
    0.021163940429688
    0.020851135253906
    0.022552490234375
    0.021713256835938
    0.021484375
    So I guess it's really faster, isn't it?
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Dumb Dumb x 8Optimistic Optimistic x 1Useful Useful x 1 (list)

  23. Post #23
    SB2DevTeam's Avatar
    September 2007
    384 Posts
    Made a shitty test with this script:

    local t = SysTime()
    
    for i=1, 2000 do
    	print(i)
    end
    
    print(SysTime() - t)
    

    I have run it 5 times (each with and without luajit). My results:

    Without luajit:
    Code:
    0.02301025390625
    0.0230712890625
    0.02398681640625
    0.0235595703125
    0.02166748046875
    With luajit:
    Code:
    0.021163940429688
    0.020851135253906
    0.022552490234375
    0.021713256835938
    0.021484375
    So I guess it's really faster, isn't it?
    Could you test it using stuff like entities? so engine functions get called. Should be more accurate then just a simple thing like that :p
    Reply With Quote Edit / Delete Reply Mac Belgium Show Events Agree Agree x 1 (list)

  24. Post #24
    pennerlord's Avatar
    February 2011
    503 Posts
    Could you test it using stuff like entities? so engine functions get called. Should be more accurate then just a simple thing like that :p
    Next test:

    local t = SysTime()
    local ply = player.GetByID(1)
    
    for i=1, 100 do
    	local ent = ents.Create("prop_physics")
    	ent:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
    	ent:SetPos(ply:GetPos() + Vector(0,0,200))
    	ent:Spawn()
    	
    	print(i)
    end
    
    print(SysTime() - t)
    


    Results:

    Without luajit:
    Code:
    0.030975341796875
    0.016754150390625
    0.016815185546875
    0.017242431640625
    0.019500732421875
    With luajit:
    Code:
    0.03314208984375
    0.0166015625
    0.016845703125
    0.0164794921875
    0.0166015625
    Luajit is still faster.
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Dumb Dumb x 6Funny Funny x 3Optimistic Optimistic x 1 (list)

  25. Post #25
    garry's Avatar
    September 2001
    12,410 Posts
    The thing you guys are missing is it's not going to make calling a function (like print) faster, because its just calling a function - hardly anything is happening Lua side.

    Try doing calculations in Lua side. Multiplying 50,000 numbers, merging huge tables etc etc.

    It's not going to make any function calls faster unless they dip back into Lua.
    Reply With Quote Edit / Delete Reply Linux Show Events Agree Agree x 16Informative Informative x 1 (list)

  26. Post #26
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    The thing you guys are missing is it's not going to make calling a function (like print) faster, because its just calling a function - hardly anything is happening Lua side.

    Try doing calculations in Lua side. Multiplying 50,000 numbers, merging huge tables etc etc.

    It's not going to make any function calls faster unless they dip back into Lua.
    Even if its not making the C-bindings faster, its still a good improvement.

    So if even a third-party dev with no actual sourcecode can implement that without making the game unstable, why not give it another chance?
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Agree Agree x 9Dumb Dumb x 5 (list)

  27. Post #27
    I made WAYWO a better place
    OldFusion's Avatar
    September 2011
    1,311 Posts
    Next test:

    local t = SysTime()
    local ply = player.GetByID(1)
    
    for i=1, 100 do
    	local ent = ents.Create("prop_physics")
    	ent:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
    	ent:SetPos(ply:GetPos() + Vector(0,0,200))
    	ent:Spawn()
    	
    	print(i)
    end
    
    print(SysTime() - t)
    


    Results:

    Without luajit:
    Code:
    0.030975341796875
    0.016754150390625
    0.016815185546875
    0.017242431640625
    0.019500732421875
    With luajit:
    Code:
    0.03314208984375
    0.0166015625
    0.016845703125
    0.0164794921875
    0.0166015625
    Luajit is still faster.
    require("jit.opt").start() ??

    Edited:

    The thing you guys are missing is it's not going to make calling a function (like print) faster, because its just calling a function - hardly anything is happening Lua side.

    Try doing calculations in Lua side. Multiplying 50,000 numbers, merging huge tables etc etc.

    It's not going to make any function calls faster unless they dip back into Lua.
    There is still plenty of lua code getting executed. And the performance difference with 2.0 is quite a lot



    In an avg gamemode you can find plenty of nested loops and hooks that get called a few times every frame.

  28. Post #28
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    require("jit.opt").start() ??
    That probably won't work. Garry's Mod is still using all of the same libraries and functions, and its require() and module loaders don't know about any embedded LuaJIT libraries. The only reason the 'jit' library is there at all is because I detoured luaL_loadlibs to load it.

    I have several applications developed using latest beta of luajit running 24/7 for at least several months. None of them crashed yet.
    Ok, I'm going to start working on sticking LuaJIT 2 in there.
    Reply With Quote Edit / Delete Reply Windows Vista United States Show Events Winner Winner x 2 (list)

  29. Post #29
    garry's Avatar
    September 2001
    12,410 Posts
    require("jit.opt").start() ??

    Edited:



    There is still plenty of lua code getting executed. And the performance difference with 2.0 is quite a lot



    In an avg gamemode you can find plenty of nested loops and hooks that get called a few times every frame.
    I'm aware of those. I'm talking about people document speed increases in THIS module. Maybe timedemo the difference between two demos to show how drastic the difference is to make me want to implement it into GMod for real.
    Reply With Quote Edit / Delete Reply Windows 7 Show Events Agree Agree x 2 (list)

  30. Post #30
    Gold Member
    Dennab
    May 2011
    436 Posts
    I have to admit this is brilliant, it pretty much does what I was working on lately, but you were faster. I will try this out once I got more time

  31. Post #31
    gamerpaddy's Avatar
    March 2009
    366 Posts
    Can anyone test my Script with LuaJit?
    I see no difference...

    Without LuaJit on my slow PC:

    Code:
    PRIME: 100k digits after 0.128662109375
    PRIME: 1 million digits after 2.92138671875
    PRIME: 78494 numbers found
    With LuaJit on my slow PC:

    Code:
    PRIME: 100k digits after 0.108662109375
    PRIME: 1 million digits after 2.72138671875
    PRIME: 78494 numbers found
    Luacode:

    Code:
    local t = SysTime()
    local Primes = 0
    local I = 0
    function prime(N)
    	if (N % 2 == 0 or N % 3 == 0 or N % 7 == 0 or N % 5 == 0) then
    		return false
    	end
    	R =math.sqrt(N)
    	F = 11
    	if (R % 1 == 0)then
    		return false
    	end
    	while (F <= R) do
    		if (N % F == 0)then
    		return false
    	end
    	F = F + 2
    	if (N % F == 0)then
    		return false
    	end
    	F = F + 4
    	end
    	return true
    end
    local I = 0
    while I < 100000 do 
    I = I + 1
    	if prime(I) then
    		Primes = Primes + 1 
    	end
    end
    print("PRIME: 100k digits after "..SysTime() - t)
    local t = SysTime()
    
    while I < 1000000 do 
    I = I + 1
    	if prime(I) then
    		Primes = Primes + 1 
    	end
    end
    print("PRIME: 1 million digits after "..SysTime() - t)
    print("PRIME: "..Primes.." numbers found") -- fail D:

  32. Post #32
    Gold Member
    Megalan's Avatar
    October 2005
    561 Posts

    used test:

    http://dl.dropbox.com/u/1285798/metatable.lua

    original test:

    http://lubyk.org/en/post313.html


    to gamerpaddy:

    Without JIT:


    PRIME: 100k digits after 0.177734375
    PRIME: 1 million digits after 4.020263671875
    PRIME: 78494 numbers found

    With it:

    PRIME: 100k digits after 0.04248046875
    PRIME: 1 million digits after 1.005615234375
    PRIME: 78494 numbers found

    ps: tested on Core2Quad Q9400 @ 2.66ghz, 8gb RAM
    Reply With Quote Edit / Delete Reply Windows 7 Russian Federation Show Events Winner Winner x 8Agree Agree x 1 (list)

  33. Post #33
    Gold Member
    AzuiSleet's Avatar
    September 2007
    758 Posts
    It looks like your bitshift operators are missing the fallback implementation in luaV_arith (eg coercing a string into an int), but that's only a one or two line fix.

    I'm still skeptical whether this actually has any benefit to per-frame times, or if it is stable enough and easy enough to maintain. The last test, with LuaJIT 2, there was a rather serious issue with pcall.

    It would also be interesting to evaluate the generational GC in 5.2, as it's perfectly suited for the type of allocations in gmod. I believe it's on the roadmap but not yet implemented in LuaJIT.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 2Informative Informative x 1 (list)

  34. Post #34
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    It turns out that adding new ops to LuaJIT 2.0.0 is much harder than I thought it would be. I added all of GLua's syntax to it except for the bitwise operators. I've uploaded a new *.rar that has my current work on LuaJIT 2.0.0 in 'gmod_luajit2.dll' as well as a fix for LuaJIT 1.1.7 for the issue that AzuiSleet pointed out (thanks). I'll have to go over the LuaJIT 2.0.0 code a lot more before I can change it further. In the mean time, gmod_luajit2.dll adds the 'bit' library, so you can (if you really want to) replace all instances of '|', '&', '<<', and '>>' with their equivalents from the 'bit' library.
    Reply With Quote Edit / Delete Reply Windows XP United States Show Events Lua King Lua King x 2 (list)

  35. Post #35
    Gold Member
    Jellyman's Avatar
    October 2011
    1,379 Posts
    Flapadar posted:
    Code:
    E:\lua>luajit.exe prime_2.lua
    Testing 1000000 numbers
    Testing method 1
    Method 1:       78494 primes; Time taken:       0.418 seconds
    
    E:\lua>lua5.1.exe prime_2.lua
    Testing 1000000 numbers
    Testing method 1
    Method 1:       78494 primes; Time taken:       3.444 seconds
    Used a similar method to the Sieve of Eratosthenes. Luajit is 8x faster.
    I was asked to post this.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Informative Informative x 1 (list)

  36. Post #36
    Buttes
    Jcw87's Avatar
    May 2005
    459 Posts
    I managed to load the module into the Garry's Mod client by loading it as a shader. You can get the updated module in the first post.

    I am not sure if VAC will care about it. My guess is no, but I'm not making any promises.

  37. Post #37
    JustSoFaded's Avatar
    December 2011
    432 Posts
    if it's a plugin, VAC will not care
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 12 (list)

  38. Post #38

    December 2011
    350 Posts
    if it's a plugin, VAC will not care
    Are you implying VAC won't detect plugins?

  39. Post #39
    DylanWilson's Avatar
    January 2010
    273 Posts
    doesn't VAC use a blacklist?
    I don't see why it would care
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 1Disagree Disagree x 1 (list)

  40. Post #40
    I made WAYWO a better place
    OldFusion's Avatar
    September 2011
    1,311 Posts
    Gmod Vac uses a blacklist as far as we know.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 9 (list)