From 9b788b042736553ce31615a904d3cce17cf0e918 Mon Sep 17 00:00:00 2001 From: joeyjumper94 Date: Sat, 28 Sep 2024 17:12:22 -0700 Subject: [PATCH 1/4] Update player.lua speed up player.getby____ID(ID) functions by relying primarily on indexing a table --- garrysmod/lua/includes/extensions/player.lua | 72 ++++++++++++++------ 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/garrysmod/lua/includes/extensions/player.lua b/garrysmod/lua/includes/extensions/player.lua index cd046db3fa..c0d3d42fd6 100644 --- a/garrysmod/lua/includes/extensions/player.lua +++ b/garrysmod/lua/includes/extensions/player.lua @@ -261,24 +261,27 @@ function meta:HasGodMode() end + -- These are totally in the wrong place. +local cache={} +cache.AccountID={} +cache.UniqueID={} +cache.SteamID64={} +cache.SteamID={} +cache.bots={} function player.GetByAccountID( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:AccountID() == ID ) then - return players[i] - end + local ret=cache.AccountID[ID] + if ( ret and ret:IsValid() ) then + return ret end return false end function player.GetByUniqueID( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:UniqueID() == ID ) then - return players[i] - end + local ret=cache.UniqueID[ID] + if ( ret and ret:IsValid() ) then + return ret end return false @@ -286,10 +289,21 @@ end function player.GetBySteamID( ID ) ID = string.upper( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:SteamID() == ID ) then - return players[i] + local ret=cache.SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed + if ( ret and ret:IsValid() ) then + return ret + end + if ( ret and ID=="BOT" ) then--if we are using BOT and the original + while ( ret ) do + ret=cache.bots[1]--get the first bot on the list + if ( not ret ) then--cache.bots is now empty, we failed + return false + end + if ( ret:IsValid() ) then--is it a valid entity? + cache.SteamID[ID]=ret--entry for BOT so that hopefully we don't have to do this again + return ret--return the bot + end + table.remove(cache.bots,1)--remove a null entity from the list end end @@ -298,11 +312,9 @@ end function player.GetBySteamID64( ID ) ID = tostring( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:SteamID64() == ID ) then - return players[i] - end + local ret=cache.SteamID64[ID] + if ( ret and ret:IsValid() ) then + return ret end return false @@ -321,7 +333,27 @@ end local function InvalidatePlayerCache( ent ) - if ( ent:IsPlayer() ) then PlayerCache = nil end + if ( ent:IsPlayer() ) then + PlayerCache = nil + timer.Create("player.Iterator",0,1,function() + PlayerCache = player.GetAll() + cache.AccountID={} + cache.UniqueID={} + cache.SteamID64={} + cache.SteamID={} + cache.bots={} + for i = 1, #PlayerCache do + local p=PlayerCache[i] + cache.AccountID[p:AccountID()]=p + cache.UniqueID[p:UniqueID()]=p + cache.SteamID64[p:SteamID64()]=p + cache.SteamID[p:SteamID()]=p + if ( p:SteamID() == "BOT" ) then + table.insert(cache.bots,p) + end + end + end) + end end From 277bf0e981ae55f50d1ba28390e2c7ba1240c71f Mon Sep 17 00:00:00 2001 From: joeyjumper94 Date: Sat, 28 Sep 2024 17:22:28 -0700 Subject: [PATCH 2/4] Update player.lua some fine tuning --- garrysmod/lua/includes/extensions/player.lua | 62 ++++++++++---------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/garrysmod/lua/includes/extensions/player.lua b/garrysmod/lua/includes/extensions/player.lua index c0d3d42fd6..c5f6fb0249 100644 --- a/garrysmod/lua/includes/extensions/player.lua +++ b/garrysmod/lua/includes/extensions/player.lua @@ -263,14 +263,13 @@ end -- These are totally in the wrong place. -local cache={} -cache.AccountID={} -cache.UniqueID={} -cache.SteamID64={} -cache.SteamID={} -cache.bots={} +local AccountID={} +local UniqueID={} +local SteamID64={} +local SteamID={} +local bots={} function player.GetByAccountID( ID ) - local ret=cache.AccountID[ID] + local ret=AccountID[ID] if ( ret and ret:IsValid() ) then return ret end @@ -279,7 +278,7 @@ function player.GetByAccountID( ID ) end function player.GetByUniqueID( ID ) - local ret=cache.UniqueID[ID] + local ret=UniqueID[ID] if ( ret and ret:IsValid() ) then return ret end @@ -289,21 +288,21 @@ end function player.GetBySteamID( ID ) ID = string.upper( ID ) - local ret=cache.SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed + local ret=SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed if ( ret and ret:IsValid() ) then return ret end if ( ret and ID=="BOT" ) then--if we are using BOT and the original while ( ret ) do - ret=cache.bots[1]--get the first bot on the list - if ( not ret ) then--cache.bots is now empty, we failed + ret=bots[1]--get the first bot on the list + if ( not ret ) then--bots is now empty, we failed return false end if ( ret:IsValid() ) then--is it a valid entity? - cache.SteamID[ID]=ret--entry for BOT so that hopefully we don't have to do this again + SteamID[ID]=ret--entry for BOT so that hopefully we don't have to do this again return ret--return the bot end - table.remove(cache.bots,1)--remove a null entity from the list + table.remove(bots,1)--remove a null entity from the list end end @@ -312,7 +311,7 @@ end function player.GetBySteamID64( ID ) ID = tostring( ID ) - local ret=cache.SteamID64[ID] + local ret=cSteamID64[ID] if ( ret and ret:IsValid() ) then return ret end @@ -331,31 +330,30 @@ function player.Iterator() end -local function InvalidatePlayerCache( ent ) - +hook.Add( "OnEntityCreated", "PlayerCache", function( ent ) if ( ent:IsPlayer() ) then PlayerCache = nil - timer.Create("player.Iterator",0,1,function() - PlayerCache = player.GetAll() - cache.AccountID={} - cache.UniqueID={} - cache.SteamID64={} - cache.SteamID={} - cache.bots={} + timer.Create("PlayerCache",0,1,function() + AccountID={} + UniqueID={} + SteamID64={} + SteamID={} + bots={} for i = 1, #PlayerCache do local p=PlayerCache[i] - cache.AccountID[p:AccountID()]=p - cache.UniqueID[p:UniqueID()]=p - cache.SteamID64[p:SteamID64()]=p - cache.SteamID[p:SteamID()]=p + AccountID[p:AccountID()]=p + UniqueID[p:UniqueID()]=p + SteamID64[p:SteamID64()]=p + SteamID[p:SteamID()]=p if ( p:SteamID() == "BOT" ) then - table.insert(cache.bots,p) + table.insert(bots,p) end end end) end - end - -hook.Add( "OnEntityCreated", "player.Iterator", InvalidatePlayerCache ) -hook.Add( "EntityRemoved", "player.Iterator", InvalidatePlayerCache ) +hook.Add( "EntityRemoved", "PlayerCache", function( ent ) + if ( ent:IsPlayer() ) then + PlayerCache = nil + end +end From ad1cbcf2c16efec810a80e5b767cedde172084be Mon Sep 17 00:00:00 2001 From: joeyjumper94 Date: Wed, 2 Oct 2024 19:28:45 -0700 Subject: [PATCH 3/4] finalize player.GetBy___(ID) optimizations --- garrysmod/lua/includes/extensions/player.lua | 61 ++++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) mode change 100644 => 100755 garrysmod/lua/includes/extensions/player.lua diff --git a/garrysmod/lua/includes/extensions/player.lua b/garrysmod/lua/includes/extensions/player.lua old mode 100644 new mode 100755 index c5f6fb0249..539cb0876e --- a/garrysmod/lua/includes/extensions/player.lua +++ b/garrysmod/lua/includes/extensions/player.lua @@ -261,7 +261,6 @@ function meta:HasGodMode() end - -- These are totally in the wrong place. local AccountID={} local UniqueID={} @@ -278,28 +277,33 @@ function player.GetByAccountID( ID ) end function player.GetByUniqueID( ID ) - local ret=UniqueID[ID] + local ret = UniqueID[ID] if ( ret and ret:IsValid() ) then return ret end - + PrintTable{ + ID=ID, + ret=ret, + UniqueID=UniqueID, + } return false end function player.GetBySteamID( ID ) ID = string.upper( ID ) - local ret=SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed + local ret = SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed if ( ret and ret:IsValid() ) then return ret end - if ( ret and ID=="BOT" ) then--if we are using BOT and the original + if ( ret and ID=="BOT" ) then--the original function would return the first bot it could find, while ( ret ) do - ret=bots[1]--get the first bot on the list + ret = bots[1]--get the first bot on the list if ( not ret ) then--bots is now empty, we failed + SteamID.BOT = nil return false end if ( ret:IsValid() ) then--is it a valid entity? - SteamID[ID]=ret--entry for BOT so that hopefully we don't have to do this again + SteamID.BOT = ret--update entry for BOT so that hopefully we don't have to do this again return ret--return the bot end table.remove(bots,1)--remove a null entity from the list @@ -311,7 +315,7 @@ end function player.GetBySteamID64( ID ) ID = tostring( ID ) - local ret=cSteamID64[ID] + local ret = SteamID64[ID] if ( ret and ret:IsValid() ) then return ret end @@ -332,28 +336,35 @@ end hook.Add( "OnEntityCreated", "PlayerCache", function( ent ) if ( ent:IsPlayer() ) then - PlayerCache = nil - timer.Create("PlayerCache",0,1,function() - AccountID={} - UniqueID={} - SteamID64={} - SteamID={} - bots={} + PlayerCache = player.GetAll() + AccountID[ent:AccountID()] = ent + if ( CLIENT ) then--workaround to https://github.com/Facepunch/garrysmod-issues/issues/6012 + UniqueID[ ent:UniqueID() ] = ent + else + timer.Simple(0, function() + if ent:IsValid()then + UniqueID[ ent:UniqueID() ] = ent + end + end) + end + SteamID64[ent:SteamID64()] = ent + local ID=ent:SteamID() + if ( ID == "BOT" ) then--if a bot joined, we got a bit more work to do + bots = {}--lets rebuild the bot cache for i = 1, #PlayerCache do - local p=PlayerCache[i] - AccountID[p:AccountID()]=p - UniqueID[p:UniqueID()]=p - SteamID64[p:SteamID64()]=p - SteamID[p:SteamID()]=p - if ( p:SteamID() == "BOT" ) then - table.insert(bots,p) + local p = PlayerCache[i] + if ( p:SteamID() == "BOT" ) then--this is more reliable than Player:IsBot() which checks entity flags that may be added or removed + table.insert(bots, p) + SteamID.BOT = SteamID.BOT or p end end - end) + else + SteamID[ID] = ent + end end -end +end ) hook.Add( "EntityRemoved", "PlayerCache", function( ent ) if ( ent:IsPlayer() ) then PlayerCache = nil end -end +end ) From 359dbfef24a46063591e07fe1b1a7d72919b26e8 Mon Sep 17 00:00:00 2001 From: joeyjumper94 Date: Wed, 2 Oct 2024 19:52:18 -0700 Subject: [PATCH 4/4] fix the formating so that it matches contributing.md fix the formating so that it matches contributing.md --- garrysmod/lua/includes/extensions/player.lua | 37 +++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/garrysmod/lua/includes/extensions/player.lua b/garrysmod/lua/includes/extensions/player.lua index 539cb0876e..429063217a 100755 --- a/garrysmod/lua/includes/extensions/player.lua +++ b/garrysmod/lua/includes/extensions/player.lua @@ -262,13 +262,13 @@ function meta:HasGodMode() end -- These are totally in the wrong place. -local AccountID={} -local UniqueID={} -local SteamID64={} -local SteamID={} +local accountID={} +local uniqueID={} +local steamID64={} +local steamID={} local bots={} function player.GetByAccountID( ID ) - local ret=AccountID[ID] + local ret = accountID[ID] if ( ret and ret:IsValid() ) then return ret end @@ -277,21 +277,16 @@ function player.GetByAccountID( ID ) end function player.GetByUniqueID( ID ) - local ret = UniqueID[ID] + local ret = uniqueID[ID] if ( ret and ret:IsValid() ) then return ret end - PrintTable{ - ID=ID, - ret=ret, - UniqueID=UniqueID, - } return false end function player.GetBySteamID( ID ) ID = string.upper( ID ) - local ret = SteamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed + local ret = steamID[ID]--ret could be nil if there was never an entry, or NULL if the player DCed if ( ret and ret:IsValid() ) then return ret end @@ -299,11 +294,11 @@ function player.GetBySteamID( ID ) while ( ret ) do ret = bots[1]--get the first bot on the list if ( not ret ) then--bots is now empty, we failed - SteamID.BOT = nil + steamID.BOT = nil return false end if ( ret:IsValid() ) then--is it a valid entity? - SteamID.BOT = ret--update entry for BOT so that hopefully we don't have to do this again + steamID.BOT = ret--update entry for BOT so that hopefully we don't have to do this again return ret--return the bot end table.remove(bots,1)--remove a null entity from the list @@ -315,7 +310,7 @@ end function player.GetBySteamID64( ID ) ID = tostring( ID ) - local ret = SteamID64[ID] + local ret = steamID64[ID] if ( ret and ret:IsValid() ) then return ret end @@ -337,17 +332,17 @@ end hook.Add( "OnEntityCreated", "PlayerCache", function( ent ) if ( ent:IsPlayer() ) then PlayerCache = player.GetAll() - AccountID[ent:AccountID()] = ent + accountID[ent:AccountID()] = ent if ( CLIENT ) then--workaround to https://github.com/Facepunch/garrysmod-issues/issues/6012 - UniqueID[ ent:UniqueID() ] = ent + uniqueID[ ent:UniqueID() ] = ent else timer.Simple(0, function() if ent:IsValid()then - UniqueID[ ent:UniqueID() ] = ent + uniqueID[ ent:UniqueID() ] = ent end end) end - SteamID64[ent:SteamID64()] = ent + steamID64[ent:SteamID64()] = ent local ID=ent:SteamID() if ( ID == "BOT" ) then--if a bot joined, we got a bit more work to do bots = {}--lets rebuild the bot cache @@ -355,11 +350,11 @@ hook.Add( "OnEntityCreated", "PlayerCache", function( ent ) local p = PlayerCache[i] if ( p:SteamID() == "BOT" ) then--this is more reliable than Player:IsBot() which checks entity flags that may be added or removed table.insert(bots, p) - SteamID.BOT = SteamID.BOT or p + steamID.BOT = steamID.BOT or p end end else - SteamID[ID] = ent + steamID[ID] = ent end end end )