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

Improve performance of tocolor #2937

Open
1 task done
CrosRoad95 opened this issue Mar 29, 2023 · 4 comments · May be fixed by #3044
Open
1 task done

Improve performance of tocolor #2937

CrosRoad95 opened this issue Mar 29, 2023 · 4 comments · May be fixed by #3044
Labels
enhancement New feature or request

Comments

@CrosRoad95
Copy link
Contributor

Is your feature request related to a problem? Please describe.

This function is very, very often used, on almost all servers in render loops,

int CLuaUtilDefs::tocolor(lua_State* luaVM)
current implementation doing a lot of unnecessary things

Describe the solution you'd like

move implementation to lua to decrease context switch, example implementation:

function tocolor(r,g,b,a)
  return b + g * 256 + r * 256 * 256 + (a or 255) * 256 * 256 * 256;
end

Describe alternatives you've considered

/

Additional context

:cat

Security Policy

  • I have read and understood the Security Policy and this issue is not about a cheat or security vulnerability.
@CrosRoad95 CrosRoad95 added the enhancement New feature or request label Mar 29, 2023
@Xenius97
Copy link
Contributor

Hi,
I've fixed your code, cause it could break sometimes (eg. if you use interpolate animations). Now works well.

function tocolor(r,g,b,a)
	return math.floor(b) + math.floor(g) * 256 + math.floor(r) * 256 * 256 + math.floor(a or 255) * 256 * 256 * 256;
end

@tederis
Copy link
Collaborator

tederis commented Mar 30, 2023

It's a bad practice to invoke tocolor every frame [assuming that a color is immutable]. The result of this function should be cached somewhere. Or even better to use a hexadecimal number like 0xFF00FFFF.

@Einheit-101
Copy link

Einheit-101 commented Apr 12, 2023

Hi, I've fixed your code, cause it could break sometimes (eg. if you use interpolate animations). Now works well.

function tocolor(r,g,b,a)
	return math.floor(b) + math.floor(g) * 256 + math.floor(r) * 256 * 256 + math.floor(a or 255) * 256 * 256 * 256;
end

Sorry, your "floor" ruins the whole optimization. Without floor its twice as fast as tocolor, but with floor its twice as slow. I highly suggest just checking for yourself that you dont put wrong arguments into this wrapper function instead of doing this check for every tocolor() call.

@thisdp
Copy link
Contributor

thisdp commented Apr 19, 2023

It won't optimise speed, instead, it will decrease performance

@TracerDS TracerDS linked a pull request May 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants