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

Reduce index calls on EGP:ValidEGP #2962

Merged
merged 4 commits into from
Jan 4, 2024

Conversation

brandonsturgeon
Copy link
Contributor

Simple optimization to reduce entity __index calls for this commonly-used method.
With only a few EGPs out, I recorded 77,762 __index calls from this method over a 10 second sample.

Also, from a high-level skim, it seems like this could even be cached on the EGP entity itself to make it even faster.

@Vurv78
Copy link
Contributor

Vurv78 commented Dec 29, 2023

If indexing is a problem, couldn't you take the functions from the metatable directly and just use those?

Comment on lines 233 to 234
local class = Ent:GetClass()
return class == "gmod_wire_egp" or class == "gmod_wire_egp_hud" or class == "gmod_wire_egp_emitter"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of GetClass, we can set a variable to each of these entities, ENT.IsEGP = true and check Ent.IsEGP It will invoke __index still unless you localize GetTable or GetVar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this, thanks for the suggestion

@brandonsturgeon
Copy link
Contributor Author

I'd like to fix the linting issues in this file. I could do it in this PR while I'm here, or I could do it in a followup PR - up to you 👍

@Denneisk
Copy link
Member

GetVar uses GetTable internally and has overhead for a default value so I would prefer just using GetTable.

function EGP:ValidEGP( Ent )
return IsValid( Ent ) and (Ent:GetClass() == "gmod_wire_egp" or Ent:GetClass() == "gmod_wire_egp_hud" or Ent:GetClass() == "gmod_wire_egp_emitter")
do
local GetTable = FindMetaTable( "Entity" ).GetTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add local IsValid = FindMetaTable( "Entity" ).IsValid? I'm also curious if this function returns false if nil is passed into it or if it errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it errors, then replace IsValid( ent ) with ent and IsValid( ent )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It handles nil:

lua_run print( FindMetaTable( "Entity" ).IsValid( nil ) )
> print( FindMetaTable( "Entity" ).IsValid( nil ) )...
false

Applied 👍

@thegrb93 thegrb93 merged commit 857bb81 into wiremod:master Jan 4, 2024
1 check failed
@brandonsturgeon brandonsturgeon deleted the feature/rewrite-ValidEGP branch January 5, 2024 03:12
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.

4 participants