Temp Timer and Temp Trigger Question

Post Reply
Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Temp Timer and Temp Trigger Question

Post by Jor'Mox »

So, I know that these functions can be given either strings to compile and execute, or a premade function (or a function created dynamically). In terms of performance and memory usage, is one preferable to another?

Specifically, if I want to call a function and pass it information from the calling function after the timer or trigger has fired, what would be the best method to use?

An example would be appreciated. :D

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Temp Timer and Temp Trigger Question

Post by Vadi »

I would go with the closures. They are much cleaner and simpler to use, and going with strings you'll be losing out time on string concatenation and globals lookup after.

That said, I haven't benchmarked it, but I believe the differences are not that big to go with the inconvenient way.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Temp Timer and Temp Trigger Question

Post by Jor'Mox »

Thanks Vadi. Obviously when using functions that don't need arguments, using predefined functions seems ideal. Is there any way to pass arguments to functions when using these?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Temp Timer and Temp Trigger Question

Post by Vadi »

Not by giving the function to tempTimer directly, but you can with the usual string method -
Code: [show] | [select all] lua
mytimer = tempTimer(1, [[my_function("]]..value1.."[[, "]]..value2..[[")]])

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Temp Timer and Temp Trigger Question

Post by Jor'Mox »

Right, well I knew about that one. Thanks Vadi.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: Temp Timer and Temp Trigger Question

Post by demonnic »

or perhaps wrap it in an anonymous function
Code: [show] | [select all] lua
myTimer = tempTimer(1, function() my_function(value1, value2) end)

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Temp Timer and Temp Trigger Question

Post by Jor'Mox »

That would certainly work better for passing table variables. Thanks.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Temp Timer and Temp Trigger Question

Post by Heiko »

Jor'Mox wrote:So, I know that these functions can be given either strings to compile and execute, or a premade function (or a function created dynamically). In terms of performance and memory usage, is one preferable to another?
Mudlet does *NOT* support function parameters neither in tempTimers nor tempTriggers. A long time ago I added code to test support for function parameters for tempTimers but no code to clean up the memory when the timer gets killed. Consequently, using a function parameter causes a memory leak. The reason why I opted against adding a function parameters feature for this type of functions is because it imho slows down the client unnecessarily because we have to call into Lua again and clean up the timer info before the timer object gets destroyed. As I've added other "slow-but-useful" stuff since then I'm open for a feature discussion.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Temp Timer and Temp Trigger Question

Post by Jor'Mox »

Honestly, I was hoping to be able to pass arguments directly, rather than using some of the other methods suggested, so as to avoid the current memory leak associated with using tempTriggers and tempTimers, in that the functions that they generate are never removed from memory, remaining stored in the global variables associated with them, even once the timer or trigger is destroyed. I.E. if I make a tempTimer, and it has ID# 5, then Timer5 becomes a new variable in the global name space, and stores the resulting function, and it isn't cleared after the timer fires and is removed.

Note: More thorough testing reveals this only happens when timers and/or triggers are passed code to execute as a string. Passing a function, even an anonymous function, does not result in persistent storage problems. Though, even with nothing referencing them, the ID number remains "taken" and will not be reused, which seems less than ideal, but not hugely problematic.

As an aside, it would also be nice if other function invoking functions, like setLabelClickCallback allowed for similar syntax.

Post Reply