Page 1 of 1

A_IPAddress Performance

Posted: 31 May 2018, 12:45
by egocarib

Code: Select all

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?

Re: A_IPAddress Performance

Posted: 31 May 2018, 13:12
by swagfag
cache it yourself before you start comparing. i dont see the argument for having ahk cache its built in vars for you

Re: A_IPAddress Performance

Posted: 31 May 2018, 19:04
by egocarib
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.

Re: A_IPAddress Performance

Posted: 31 May 2018, 19:09
by jeeswg
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

Re: A_IPAddress Performance

Posted: 31 May 2018, 19:12
by egocarib
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.

Re: A_IPAddress Performance

Posted: 31 May 2018, 19:20
by jeeswg
- 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.

Re: A_IPAddress Performance

Posted: 01 Jun 2018, 20:16
by lexikos
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.

Re: A_IPAddress Performance

Posted: 02 Jun 2018, 04:07
by Brazolek123
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.

Re: A_IPAddress Performance

Posted: 02 Jun 2018, 16:25
by jeeswg
- @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 ... 02#p221802