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-optimization for drawing base entities #2158

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

noaccessl
Copy link
Contributor

@noaccessl noaccessl commented Nov 23, 2024

Replaced self:DrawModel( flags ) with Entity.DrawModel( self, flags ).

Makes it ~26% faster.

image


Benchmark

local Entity = FindMetaTable( "Entity" )

local SysTime = SysTime

local TOTAL1 = 0
local TOTAL2 = 0

local NUM_CALLS = 0

function ENT:Draw( flags )

	do

		local START = SysTime()

			self:DrawModel( flags )

		local END = SysTime()

		TOTAL1 = TOTAL1 + ( END - START ) * 1000

	end

	do

		local START = SysTime()

			Entity.DrawModel( self, flags )

		local END = SysTime()

		TOTAL2 = TOTAL2 + ( END - START ) * 1000

	end

	NUM_CALLS = NUM_CALLS + 1

end

timer.Create( "base_anim:Draw", 0, 0, function()

	local AVG1 = TOTAL1 / NUM_CALLS
	local AVG2 = TOTAL2 / NUM_CALLS

	local PERCENT_DIFF = 100 - ( ( AVG2 * 100 ) / AVG1 )

	print( Format( "base_anim:Draw called %i times:\n\tEntity.DrawModel is %.2f%% faster than self:DrawModel\n\t(%.4f ms compared to %.4f ms)", NUM_CALLS, PERCENT_DIFF, AVG1, AVG2 ) )

	TOTAL1 = 0
	TOTAL2 = 0

	NUM_CALLS = 0

end )

P.S. Changed to using the model drawing function directly.

function ENT:Draw( flags )

self:DrawModel( flags )
Entity.DrawModel( self, flags )

Choose a reason for hiding this comment

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

Isn't it easier to do ENT.Draw = Entity.DrawModel?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're right. Thanks.

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.

2 participants