WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Hooking my own functions (https://www.wowinterface.com/forums/showthread.php?t=49824)

Duugu 09-01-14 06:25 PM

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. :eek:

Lombra 09-01-14 06:50 PM

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


Duugu 09-01-14 10:03 PM

Doh. Stupid me. *fp*
Thanks Lombra.

Rilgamon 09-02-14 08:04 AM

While this is hopefully only for local testing ... never forget the underscore to be defined local ;)

Lua Code:
  1. _, ProfilerTestAddon= ...

Duugu 09-02-14 11:17 AM

Quote:

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().

Phanx 09-02-14 05:06 PM

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. ಠ_ಠ

Duugu 09-02-14 05:14 PM

Quote:

Originally Posted by Phanx (Post 296170)
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 :))

Phanx 09-02-14 05:57 PM

How are you on on the internet and don't know what the look of disapproval is? :(

Duugu 09-02-14 06:22 PM

Quote:

Originally Posted by Phanx (Post 296174)
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. :D

Phanx 09-02-14 08:05 PM

Quote:

Originally Posted by Duugu (Post 296178)
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. :rolleyes:


All times are GMT -6. The time now is 07:45 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI