Thread Tools Display Modes
09-01-14, 06:25 PM   #1
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Hooking my own functions

Hey all

I'm trying to so find some performance leaks in my addons code. My first thought was to hook everything and to see how long it takes.

So I came up with this:

Lua Code:
  1. _, ProfilerTestAddon= ...
  2.  
  3. function ProfilerTestAddon:Test1()
  4.     print("Test1")
  5. end
  6. function ProfilerTestAddon:Test2()
  7.     print("Test2")
  8. end
  9.  
  10. local function Profiler(self, fName, func)
  11.     print("profiler:",fName, GetTime(), func)
  12. end
  13.  
  14. for i, v in pairs(ProfilerTestAddon) do
  15.     if type(v) == "function" then
  16.         hooksecurefunc(ProfilerTestAddon, i, function(self) Profiler(self, i, v) end)
  17.     end
  18. end

Works as expected.

But ... to see how much time function needs a post-hook via hooksecurefunc obv. isn't enough. So I tried this:

Lua Code:
  1. _, ProfilerTestAddon= ...
  2.  
  3. function ProfilerTestAddon:Test1()
  4.     print("Test1")
  5. end
  6. function ProfilerTestAddon:Test2()
  7.     print("Test2")
  8. end
  9.  
  10. local function Profiler(self, fName, func)
  11.     print("profiler IN:",fName, GetTime())
  12.     func()
  13.     print("profiler OUT:",fName, GetTime())
  14. end
  15.  
  16. for i, v in pairs(ProfilerTestAddon) do
  17.     if type(v) == "function" then
  18.         print("hooking", i)
  19.         local tmp = v
  20.         v = function(self)
  21.             Profiler(self, i, v)
  22.         end
  23.         print(tmp == v)
  24.     end
  25. end

To my surprise that didn't worked.

It iterates the functions. The line print(tmp == v) returns false. I can call ProfilerTestAddon:Test1() and it does print "Test2". But that's it. Profiler() is not called.

What the hell could be wrong with it?


[e]
Even
Lua Code:
  1. for i, v in pairs(ProfilerTestAddon) do
  2.     if type(v) == "function" then
  3.         print("hooking", i)
  4.         local tmp = v
  5.         v = nil
  6.         print(tmp == v)
  7.     end
  8. end
doesn't break something. ProfilerTestAddon:Test1() does still work.

Last edited by Duugu : 09-01-14 at 06:33 PM.
  Reply With Quote
09-01-14, 06:50 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Where do you suppose Profiler would be called? What I think you might be missing here is that reassigning the loop variables won't actually change the table, which I think is what you're looking to do. Reassigning v won't change the value of table[i] in the same way that this "doesn't work":
Code:
a = 1
b = a
b = 2
print(a) -- a is still 1
__________________
Grab your sword and fight the Horde!
  Reply With Quote
09-01-14, 10:03 PM   #3
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Doh. Stupid me. *fp*
Thanks Lombra.
  Reply With Quote
09-02-14, 08:04 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
While this is hopefully only for local testing ... never forget the underscore to be defined local

Lua Code:
  1. _, ProfilerTestAddon= ...
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
09-02-14, 11:17 AM   #5
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
While this is hopefully only for local testing
Sure it is.

Don't know if I'm the only one who never heard of it, but I just realized that there is something like GetFunctionCPUUsage().

Last edited by Duugu : 09-02-14 at 03:05 PM.
  Reply With Quote
09-02-14, 05:06 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Even for local testing you should be vigilant in avoiding global leaks, especially generic ones like _, as you can easily taint vast swaths of the UI and inadvertently let other addons (and even the Blizzard UI code) mess with your code. It's just not that hard to type the "local" keyword in front of your variable declaration. Stop being so lazy; even I'm not that lazy. ಠ_ಠ
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-02-14, 05:14 PM   #7
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by Phanx View Post
Even for local testing you should be vigilant in avoiding global leaks, especially generic ones like _, as you can easily taint vast swaths of the UI and inadvertently let other addons (and even the Blizzard UI code) mess with your code. It's just not that hard to type the "local" keyword in front of your variable declaration. Stop being so lazy; even I'm not that lazy. ಠ_ಠ
That wasn't about being lazy. I was trying to get this to work (not even as a working addon but as a proof of concept) and the addon table was intentionally global to query it's contents ingame via /script and /dump.

There was only this single addon loaded, and in this specific situation I didn't care about any Blizzard stuff that could be affected by it.

I would never do this outside this specific circumstances. You see, there's no need to get ಠ_ಠ. (whatever this means )

Last edited by Duugu : 09-02-14 at 05:16 PM.
  Reply With Quote
09-02-14, 05:57 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
How are you on on the internet and don't know what the look of disapproval is?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-02-14, 06:22 PM   #9
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by Phanx View Post
How are you on on the internet and don't know what the look of disapproval is?
Well, I'm not very good with this 'internet' everyone's talking about nowadays. All those tubes, cats, penis enlargements, and blinkblink are confusing me. Someone should print it.
That reminds me on a speech that was deliverd by my fellow Chancellor last year. At least I'm not alone.
  Reply With Quote
09-02-14, 08:05 PM   #10
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Duugu View Post
That reminds me on a speech that was deliverd by my fellow Chancellor last year.
Ahh, politicians and goverments. Here in the US, they recently mandated that health insurance become available to everyone (you've probably heard of "Obamacare" by now) and that the signup process go through a website... except it seems that nobody involved in implementing this website has ever used a website before in their life, so after you the initial "enter your information" process, they actually send you massive volumes of paper in the mail. At one point they sent me a 1000-page catalog listing all the doctors in my state, which is the most populous and 3rd largest in the US, and the thing must have cost them at least $5 to print and mail, even doing them millions at a time, which surely explains why the government never has any money. Plus it takes them 6-12 weeks to actually continue with each step of the process, since they have to sort through huge volumes of incoming paper mail and then send more back out. I signed up in March, and I still don't have health insurance. If only they'd just give the project money to someone who was born after the invention of electricity, the entire process could have been done in 15 minutes.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Hooking my own functions

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off