A_IPAddress Performance

Report problems with documented functionality
egocarib
Posts: 97
Joined: 21 May 2015, 18:21

A_IPAddress Performance

31 May 2018, 12:45

Code: [Select all] [Download] GeSHi © Codebox Plus

startTick := A_TickCount
Loop 100
a := A_IPAddress1
OutputDebug("Execution time = " (A_TickCount - startTick) "ms")
;example output:
; Execution time = 3890ms


After several hours of debugging, I isolated a major performance bottleneck in my code to a line that was accessing A_IPAddress1.

Use case: I was analyzing logged TCP traffic, checking IP values in each packet against A_IPAddress1 to determine the source/destination. It was taking several minutes to process just a few thousands lines of logged traffic.

I'm guessing AHK is looking up this IP address every time it's accessed. Does AHK cache any of it's built-in variables internally after they are first retrieved? If so, would it make sense to cache A_IPAddress1-4?
swagfag
Posts: 1099
Joined: 11 Jan 2017, 17:59

Re: A_IPAddress Performance

31 May 2018, 13:12

cache it yourself before you start comparing. i dont see the argument for having ahk cache its built in vars for you
egocarib
Posts: 97
Joined: 21 May 2015, 18:21

Re: A_IPAddress Performance

31 May 2018, 19:04

Yes, I did cache it in my script to fix the issue.

When I started this thread, I assumed that AHK probably caches some of the built-in variables, so the purpose here was to discuss whether A_IPAddress should also be cached. If no system-related built-in variables are cached, then I agree that this one shouldn't be either. In that case, perhaps caching these variables in your script could be suggested in the docs, instead.
Last edited by egocarib on 31 May 2018, 19:12, edited 2 times in total.
User avatar
jeeswg
Posts: 4991
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_IPAddress Performance

31 May 2018, 19:09

This is interesting, thanks for sharing. I'll do some benchmark tests on all of the 'A_' variables. And afterwards I'll mention any slow variables here:
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596
egocarib
Posts: 97
Joined: 21 May 2015, 18:21

Re: A_IPAddress Performance

31 May 2018, 19:12

I think part of the reason this surprised me and caused debugging pain is that when I use a variable, I typically expect the value to be immediately available to the script. If there are expensive operations being performed behind the scenes that take a long time (40ms per occurrence is quite significant), I would expect that to be a function rather than a variable. In this case, we're probably just dealing with one of AHK's peculiar language traits that set it apart from other languages...

AHKv2 is trending more toward the behavior I would expect. So, for example, we have new functions such as MonitorGet, which is a similar kind of system information being retrieved, in function form. It is more obvious that this will take time and I should cache the value rather than calling the function over and over. That's a commonplace rule of programming, right? - you cache function return values instead of re-calling the function. That rule doesn't usually apply to "variables," though.

Anyhow, I digress. This is likely not a "bug" and my initial report can be ignored.
User avatar
jeeswg
Posts: 4991
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_IPAddress Performance

31 May 2018, 19:20

- Yes, I would say that the A_ variables are all essentially functions. Some update each time like A_Now, perhaps A_ScriptHwnd is retrieved once and once only. For some it's not obvious which is which.
- I've said before that it would be good to have a summary for each function saying a bit about how they're implemented. I've been working towards this myself by translating the source code for various functions:
AutoHotkey via DllCall: AutoHotkey functions as custom functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=37871
- I will do a full read of the AHK source code perhaps within the next 12 months.
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: A_IPAddress Performance

01 Jun 2018, 20:16

The computer's IP addresses can change while the script is running. If you know that it will not, can account for that in your script, or just don't care about that possibility, then by all means cache the address. If it was cached for you, you would have no control over when the address is actually retrieved.

A_IPAddress calls WSAStartup, gethostname, gethostbyname and WSACleanup. It actually retrieves all of the addresses each time, so perhaps a function which returns an array would be suitable - maybe SysGetIPAddresses. There were no arrays, only pseudo-arrays, when the variables were first added.

MonitorGet is not new; it was previously a sub-command of SysGet.
User avatar
Brazolek123
Posts: 163
Joined: 06 Jun 2016, 16:02

Re: A_IPAddress Performance

02 Jun 2018, 04:07

jeeswg wrote:This is interesting, thanks for sharing. I'll do some benchmark tests on all of the 'A_' variables. And afterwards I'll mention any slow variables here:
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596


I wonder if have you done it? I'm curious of the results, but couldn't find the link.
User avatar
jeeswg
Posts: 4991
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_IPAddress Performance

02 Jun 2018, 16:25

- @Brazolek123: I didn't say when I'd do the tests. Even if I'd said 'soon', based on Internet time, that might have meant 3 months.
- Anyway, I've done some tests now. Cheers.
jeeswg's benchmark tests - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=74&t=37876&p=221802#p221802

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 1 guest