AHK v2: converting/optimizing scripts Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: converting/optimizing scripts

24 Dec 2018, 02:09

for json.Get I also tried (c is "Space") instead of InStr(" `t`n`r",c,1), No luck. its much slower in complex functions
Value is "Space":
True if Value is a string and is empty or contains only whitespace consisting of the following characters: space (A_Space or `s), tab (A_Tab or `t), linefeed (`n), return (`r), vertical tab (`v), and formfeed (`f).
w:=Chr(33)
...
(c<w)

is NOT faster, too. LOL
It looks like InStr(a,b,1) doesnt do any additional conversions and simply compares raw widechars in here.
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: converting/optimizing scripts

24 Dec 2018, 04:37

Testing "binary cut" of strings (manually placing a terminating zero with size of short (for Unicode)):

Code: Select all

Str1(s){ 
	s1:=s "." ;+1 symbol
	NumPut(0,s1,(StrLen(s1)-2)*2,"short") ;-2 symbols
	VarSetCapacity(s1,-1) ;force AHK to recalculate length
	return s1
}

Str2(s){ 
	s1:=s "."
	s2:=SubStr(s1,1,-2) ;regular substringing to cut 2 symbols from the end
	return s2
}

j:=FileRead("1678982546")
;j:="1234567890"

n:=10*1000
t:=A_TickCount
loop(n)
	s1:=Str1(j)
a1:=A_TickCount-t

t:=A_TickCount
loop(n)
	s2:=Str2(j)
a2:=A_TickCount-t

msgbox(clipboard:=a1 "|" a2 "`n`n" s1)
Sometimes its a tad slower, but for the most part its up to 30% faster for long strings.
I use the same 14KB json test file here:
Image
For a string 10 symbols long, the regular SubStr is x1.5 .. x2 as fast.

So for truncating a loaded text file I would use "a binary cut" for sure =) I wish AHK would have a function StrTrunc or something just to quickly reduce a string's length

Actually thats the exact time where we could use super fast mcode of SSE4.2-boosted Strlen. But I dont know where to put that number so that AHK would accept it as a string length.
Last edited by vvhitevvizard on 24 Dec 2018, 23:39, edited 1 time in total.
oif2003
Posts: 213
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: AHK v2: converting/optimizing scripts

24 Dec 2018, 11:46

That's quite interesting... I poked around a bit and didn't see any obvious way to locate where strlen is stored based on the string address. The code may become too fragile anyway if we start messing with AHK internals. Speaking of which, maybe one day someone will compile an AHK with functions for users to hotpatch :D
https://www.codeproject.com/Articles/10 ... eep-Inside
https://nullprogram.com/blog/2016/03/31/

Hey, I am unable to reproduce the result with the test file you sent me earlier as string.
Spoiler
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: converting/optimizing scripts

24 Dec 2018, 23:36

oif2003 wrote:
24 Dec 2018, 11:46
Hey, I am unable to reproduce the result with the test file you sent me earlier as string.
Change the loop n from 100000 down to 10000. Str1 reduces the length by 1 with each iteration and the file's length is only 14000.
With every run I have 172|203 this way.

btw, the previous page's last post: have u notified any performance improvement with the updated version of StrLen mcode?
AHK with functions for users to hotpatch
Interesting reading. I had time for the second link only. Well, we have a different situation here. We r not going to patch a script's code, and for patching AHK's inner logic there is a better way - just to patch its source. But I dont feel like installing VCC and I guess AHK source won't compile with GCC. Anyways I have neither time nor experience and qualification for this.
Last edited by vvhitevvizard on 25 Dec 2018, 02:15, edited 3 times in total.
oif2003
Posts: 213
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: AHK v2: converting/optimizing scripts

25 Dec 2018, 01:31

I see what you are going for now. I made the adjustments but it appears that StrLen is still faster on average after running both inside an untimed loop first. Without the untimed loop, I was getting inconsistent results.
Spoiler
Regarding the previous MCode, I saw the improvements you reported. Nice work! :thumbup:

Merry Christmas :xmas:
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: converting/optimizing scripts

25 Dec 2018, 02:22

the untimed loop
Yeah warming up helps a lot with getting consistent results!
Try increase j length by x10 before the tests:
j:=j j j j j j j j j j
I get consistent 328|750 result with ur version. The point is a binary truncation is faster for long text data, twice as fast with 100KB file.

Merry Christmas! :D Thou Christmas here in Russia is normally celebrated on January 7th
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: converting/optimizing scripts

20 Jan 2019, 05:54

well, I figured out how to change (cut) string len internally w/o any re-calculations of its size. for _v2_. and only for object's string field for now.
(AHK L v2 x64 Unicode):

Code: Select all

obj := { 77: "The quick brown fox jumps over the lazy dog"}
mFields:=NumGet(&obj+3*A_PtrSize)
i:=0, o:=mFields+A_PtrSize*3*i ;i=N of key
s.="`n" StrGet(NumGet(o)+A_PtrSize*2, "UTF-16") ; "The quick ..."
s.="`n" NumGet(NumGet(o)) ;capacity in widechars
s.="`n" NumGet(NumGet(o)+A_PtrSize) ;len of string in widechars
s.="`n" NumGet(o+A_PtrSize, "UInt") ;Key name

;to change str len:
n:=8 ;new len in widechars
NumPut(0, NumGet(o)+A_PtrSize*2+n*2, "uint") ;put wchar zero-terminator at pos 8
NumPut(n, NumGet(o)+A_PtrSize) ;8=new len of string in widechars
obj.SetCapacity(77, n*2) ;capacity of string in chars, cannot be less than Len

s.="`n`n" StrGet(NumGet(o)+A_PtrSize*2, "UTF-16") ; "The quick ..."
s.="`n" NumGet(NumGet(o)) ;capacity in widechars
s.="`n" NumGet(NumGet(o)+A_PtrSize) ;len of string in widechars
msgbox(s)

Return to “Ask For Help”

Who is online

Users browsing this forum: AHK_user, blad4, Google [Bot], grimboto and 258 guests