mtasa-blue icon indicating copy to clipboard operation
mtasa-blue copied to clipboard

Fix #2937: Improve performance of tocolor

Open TracerDS opened this issue 1 year ago • 12 comments

Fixes #2937: Moved tocolor into an embedded section. That way it should be faster...?

TracerDS avatar May 25 '23 08:05 TracerDS

Did you perform any benchmarks? The assert and range checks will make it significantly slower than the C++ implementation.

xLive avatar May 25 '23 12:05 xLive

Did you perform any benchmarks? The assert and range checks will make it significantly slower than the C++ implementation.

Unfortunatelly I did not. Didnt have the chance. In that case a simple if statement may be used instead

TracerDS avatar May 25 '23 12:05 TracerDS

crun t1 = getTickCount() for i=1, 1000000 do tocolor(1, 2, 3, 4) end t2 = getTickCount() outputChatBox(t2-t1) 254

I then added your lua version of tocolor into runcode and it took 444 ms so this proves that this does NOT increase performance and actually degrades performance? As well as the fact that if you have 500 resources, each lua VM has a copy of this tocolor, so it's just unnecessarily increasing memory usage of every resource.

ArranTuna avatar Jun 12 '23 07:06 ArranTuna

crun t1 = getTickCount() for i=1, 1000000 do tocolor(1, 2, 3, 4) end t2 = getTickCount() outputChatBox(t2-t1) 254

I then added your lua version of tocolor into runcode and it took 444 ms so this proves that this does NOT increase performance and actually degrades performance?

Hmm, it looks like type() function is significantly slowing everything. But without that an error might occur 🤔

As well as the fact that if you have 500 resources, each lua VM has a copy of this tocolor, so it's just unnecessarily increasing memory usage of every resource.

This is not a significant increase in memory, if any

TracerDS avatar Jun 12 '23 13:06 TracerDS

crun t1 = getTickCount() for i=1, 1000000 do tocolor(1, 2, 3, 4) end t2 = getTickCount() outputChatBox(t2-t1) 254 I then added your lua version of tocolor into runcode and it took 444 ms so this proves that this does NOT increase performance and actually degrades performance?

Hmm, it looks like type() function is significantly slowing everything. But without that an error might occur 🤔

As well as the fact that if you have 500 resources, each lua VM has a copy of this tocolor, so it's just unnecessarily increasing memory usage of every resource.

This is not a significant increase in memory, if any

It's nothing about type(). The main reason is function call in lua is extremely slow. That's why i said lua implemented tocolor will decrease performance.

And another problem, your code can not work if r g b a are float like 128.5

thisdp avatar Jun 17 '23 06:06 thisdp

maybe we should focus on fixing "core issue" - you use "tocolor" because "dxDraw" functions need to be called every frame, what if, we design some "widget system" where you declare it, example rectangle, set color once and mta automatically drawing it?

local rect = dxCreateRectangleWidget(100, 100, 100, 100, tocolor(255, 0, 0)) -- no "onClientRender" required

it will be equivalent of onClientRender + dxDrawRectangle every frame with functions such a: change position, size, color ect

CrosRoad95 avatar Jun 17 '23 06:06 CrosRoad95

maybe we should focus on fixing "core issue" - you use "tocolor" because "dxDraw" functions need to be called every frame, what if, we design some "widget system" where you declare it, example rectangle, set color once and mta automatically drawing it?

local rect = dxCreateRectangleWidget(100, 100, 100, 100, tocolor(255, 0, 0)) -- no "onClientRender" required

it will be equivalent of onClientRender + dxDrawRectangle every frame with functions such a: change position, size, color ect

btw, tocolor is faster enough, dx functions are far more slower

thisdp avatar Jun 17 '23 06:06 thisdp

maybe we should focus on fixing "core issue" - you use "tocolor" because "dxDraw" functions need to be called every frame, what if, we design some "widget system" where you declare it, example rectangle, set color once and mta automatically drawing it?

local rect = dxCreateRectangleWidget(100, 100, 100, 100, tocolor(255, 0, 0)) -- no "onClientRender" required

it will be equivalent of onClientRender + dxDrawRectangle every frame with functions such a: change position, size, color ect

maybe also need a dxSetRectangleWidgetConfig(widget,100, 100, 100, 100, tocolor(255, 0, 0))

widget is a number instead of element

thisdp avatar Jun 17 '23 06:06 thisdp

There's actually a really simple fix for this which I already use except in the few cases where the color frequently changes instead of:

dxDrawText(blah, tocolor(255, 255, 255, 255), blah)

You just add this at top of script:

local white = tocolor(255, 255, 255, 255)

Then:

dxDrawText(blah, white, blah)

ArranTuna avatar Jun 17 '23 08:06 ArranTuna

or, just learn to use hex instead of tocolor?

tocolor(255, 255, 255, 255) == 0xFFFFFFFF

The hex format is 0xAARRGGBB FF is 255, 7F would be 127, and so on.. You'll learn it the more you use it.

Pirulax avatar Jun 25 '23 12:06 Pirulax

or, just learn to use hex instead of tocolor?

tocolor(255, 255, 255, 255) == 0xFFFFFFFF

The hex format is 0xAARRGGBB FF is 255, 7F would be 127, and so on.. You'll learn it the more you use it.

Best way to increase performance of tocolor (Deprecate tocolor) :merow:

thisdp avatar Jun 25 '23 13:06 thisdp

The hex format is 0xAARRGGBB FF is 255, 7F would be 127, and so on.. You'll learn it the more you use it.

But things get noticeably worse when you need to change individual components dynamically. Native implementation is better for such cases.

dmi7ry avatar Jun 25 '23 15:06 dmi7ry