Forum

> > CS2D > Scripts > hook function optimization
Forums overviewCS2D overview Scripts overviewLog in to reply

English hook function optimization

3 replies
To the start Previous 1 Next To the start

old hook function optimization

Quattro
GAME BANNED Off Offline

Quote
Just for the sake of curiosity, what's the most efficient way of handling a hook function? I heard return 1 can produce lags if used often and sometimes converting global variable to local can be faster. Which damage version below is faster and how to improve it?


1
2
3
4
5
function hit(victim_id, attacker_id, weapon, hpdmg)
	if player(attacker_id, 'team') ~= player(victim_id, 'team') then
		match_damage_temp[attacker_id] = match_damage_temp[attacker_id] + hpdmg
	return
end

1
2
3
4
5
6
function hitControl(victim_id, attacker_id, weapon, hpdmg)
	if player(attacker_id, 'team') == player(victim_id, 'team') then
		return 1
	end
	match_damage_temp[attacker_id] = match_damage_temp[attacker_id] + hpdmg	
end

old Re: hook function optimization

Hajt
User Off Offline

Quote
Both pieces of code will run with the same performance I would say, just code in second version looks better imo.

I used to code like this btw
1
2
3
4
5
6
7
8
9
10
11
12
addhook("hit","damage.hit")
function damage.hit(id,source,weapon,hpdmg)
	if source == 0 then
		return
	end

	if player(source,"team") == player(id,"team") then
		return
	end

	dmg[source] = dmg[source] + hpdmg
end

old Re: hook function optimization

Gaios
Reviewer Off Offline

Quote
> https://www.geeksforgeeks.org/writing-clean-else-statements/

Bad >

Good >


user Quattro has written
I heard return 1 can produce lags if used often

Using
return 1
will prevent server to calculate dmg and player's hp and doesn't need to synchronize with all clients. For me it should be even faster.

old Re: hook function optimization

DC
Admin Off Offline

Quote
user Quattro has written
I heard return 1 can produce lags if used often

Wrong. Actually the opposite is the case (see below).

user Gaios has written
user Quattro has written
I heard return 1 can produce lags if used often

Using
return 1
will prevent server to calculate dmg and player's hp and doesn't need to synchronize with all clients. For me it should be even faster.

Correct. If hooks allow you to skip the standard CS2D action with return 1 then you should definitely return 1 if you want to skip that action. Skipping something is faster than not skipping something

For most hooks using return 1 will not only save a tiny bit of CPU (performance impact is absolutely negligible) but also - which is more important - the network sync traffic.

user Quattro has written
and sometimes converting global variable to local can be faster.

Not sure what you mean in this particular case. I can't see anything related to that in your sample.
It's true though that local variables are faster than global variables in general. This is a micro optimization though which is not relevant for CS2D Lua scripting unless you have insane loops which iterate very often. It's still good programming style to save globals in local variables when they are accessed often in the same code block. This can also improve readability if the global var has a long name.
edited 1×, last 31.08.19 11:29:59 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview