2.1 and SetHandler
We've been profiling some UI code since 2.1 went live and we've noticed some spikes with SetHandler on controls. Especially when the code is frequently making a new anonymous closure and passing that to SetHandler. In a lot of cases this can be avoided during play time by setting the handler on the control in the XML or on a template (virtual = true) in the XML so that cost is paid during the load. The handler closure should also only be made once instead of every time SetHandler is called, by storing the closure in a local or on a table.
|
probably a stupid question but i suppose someone has to ask it...
what is a closure? an example would be nice too. |
Hey,
Wandamey, quick google search and here it is: http://www.lua.org/pil/6.1.html ;) |
...
Is there a difference between setting the Handler in a xml or setting on load with a control creation in lua? now that i see what a closure is (in the great lines) i'd still like an example... i mean related to what Chip said. |
I'll try to provide a concrete example from some add-on, I'll probably use Fyrakin's MiniMap because it seems to have been hit hard by this.
|
Quote:
:eek: I hope you can make it 3 lines with pictures :D the big picture i see here is : store your functions first. but how do i pass a variable to it without using an Anonymous func again to call it? -- maybe the table? |
Ok found something that might illustrate the issue and possible solution. From MiniMap.lua function AddLocation:
Lua Code:
The anonymous function uses pinTooltipInfo from outer scope, thus each time this runs, a new closure holding that local variable must be created. Possible solution: define the handler function outside AddLocation, and change it to obtain pinTooltipInfo in a different way -- either make it a member of an argument it gets, like this: Lua Code:
or in this case it can easily be obtained from ZO_MapPin in the handler itself: Lua Code:
|
what if i have something iterated like this
for k,v in pairs(table) do control[k]:SetHandler("OnWhatever", function(self) myfunc(self, k, v.data) end) end like most of user tooltips could do for example. |
Thx for asking the question about closures (didn't know the wording neither) Wandamey, and the explanation merlight!
Reading chips comment: Quote:
Is there a difference between setting the handler via XML, or creating the controls in the lua script and assigning the handlers there? In what cases should we use XML definitions then. And how are we supposed to define it in an XML file? Maybe we can get an example of an anonymous closure that is creating spikes, and the same thing fixed in lua script, and in addition fixed in XML instead? This would be great. |
Quote:
|
Thanks for heads up, now I have something to dig through :).
|
Quote:
whaow... how to scare the noobs. sry but i still need a translation. are you saying "stop using anonymous functions for your handlers" or there is more to it? |
Quote:
a) don't use anymore b) use this instead would be a great help :banana: |
Quote:
Lua Code:
Lua Code:
|
1. Best:
Lua Code:
2. Also Good: Lua Code:
3. Bad: Lua Code:
Instead of 3, which keeps setting the same handler code over and over again use 1 or 2 to only do it once. |
Question:
What if like this: Bad? Lua Code:
|
aahh that's less scary.
if for example i set up my control with the anchors and all this like this, it's ok even if created in the course of the game? : (will create only once) Code:
if not (control) then and votan, thanks for the workaround... i learned something like that not long ago, still need to work on it :D Edit: btw, how to "remove" a Handler? Setting it on nil will delete the previous "closure" too? i suppose not if rewriting it just creates more ( i may not get exactly what a closure is but i hope you get the idea of the question ) |
Thanks for the examples, they really helped.
I hope using the local function for the handler, like votan asked, is ok too. And I also hope the ~= nil check in Wandamey's example is alright, as the handler should only be assigned once then. Using 2 different texture controls (and showing texture 1 as you are hiding texture 2), instead of only 1 texture control where you just change the dds file, only makes sense if the handler changes too. But even then I'd think removing the old handler and setting a new one is better then creating a second texture control in total? |
lol that cross edit... i let you do the speaking now :D
|
Quote:
|
All times are GMT -6. The time now is 03:57 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI