Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro optimizations to player.GetBy* functions #2124

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions garrysmod/lua/includes/extensions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,35 +261,48 @@ function meta:HasGodMode()

end


-- These are totally in the wrong place.
local AccountID={}
local UniqueID={}
local SteamID64={}
local SteamID={}
local 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=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=UniqueID[ID]
if ( ret and ret:IsValid() ) then
return ret
end

return false
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=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=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?
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(bots,1)--remove a null entity from the list
end
end

Expand All @@ -298,11 +311,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=cSteamID64[ID]
joeyjumper94 marked this conversation as resolved.
Show resolved Hide resolved
if ( ret and ret:IsValid() ) then
return ret
end

return false
Expand All @@ -319,11 +330,30 @@ function player.Iterator()

end

local function InvalidatePlayerCache( ent )

if ( ent:IsPlayer() ) then PlayerCache = nil end

hook.Add( "OnEntityCreated", "PlayerCache", function( ent )
if ( ent:IsPlayer() ) then
PlayerCache = nil
timer.Create("PlayerCache",0,1,function()
joeyjumper94 marked this conversation as resolved.
Show resolved Hide resolved
AccountID={}
UniqueID={}
SteamID64={}
SteamID={}
bots={}
for i = 1, #PlayerCache do
joeyjumper94 marked this conversation as resolved.
Show resolved Hide resolved
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)
end
end
end)
end
end
hook.Add( "EntityRemoved", "PlayerCache", function( ent )
if ( ent:IsPlayer() ) then
PlayerCache = nil
end
end
joeyjumper94 marked this conversation as resolved.
Show resolved Hide resolved

hook.Add( "OnEntityCreated", "player.Iterator", InvalidatePlayerCache )
hook.Add( "EntityRemoved", "player.Iterator", InvalidatePlayerCache )