AHK v2: converting/optimizing scripts Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 00:04

vvhitevvizard wrote:
13 Dec 2018, 00:01
No reasons really, I just picked the first large hex number that came to my mind ;)
BTW, it's sitting at exactly 50 lines!
Spoiler
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 00:32

btw I wonder why AHK doesn't allow us to just write
static m:=-1
instead of
static m:=2**31-1
:D
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 00:41

vvhitevvizard wrote:
13 Dec 2018, 00:01
but ur code would fail[/color] on \t or EOL symbols. any "beautified" json input would crash. and if u add more comparisons it would become slower than regex

actually our own beautified output would crash json.get. Try that sample as input data (AHK v2 only syntax):
Those are some detailed analysis!
Back to regexmatch it is then! I think I'm all AHKed out again for the day
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 00:49

oif2003 wrote:
12 Dec 2018, 23:45
I changed it back because I figure after the static checks the overhead should be constant unless you change this.Space every other call :D
I see. But just this loop (just 4 iterations of it inside the function) takes enormous part (1/4) of the whole method's run-time. 125ms out of 450. Something inside AHK logic is definitely goes completely wrong here.

Code: Select all

indent(n) => n ? indent(--n) " " : ""

	t:=A_TickCount
		loop(n)
			indent(4)
		;	b:=json.get(j)
	a1:=A_TickCount-t
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 00:54

oif2003 wrote:
13 Dec 2018, 00:41
Those are some detailed analysis!
Back to regexmatch it is then! I think I'm all AHKed out again for the day
thank u! I think ur regex replacement snippet could be useful for simpler regex expressions thou. :D I just imagine we write it in simple c and compile (just x64 version, no x32 support) and insert into the script and call via mcode. it would be a killer feature still. and just a few tens of bytes.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:05

Code: Select all

indent(n) => --n ? indent(n) " " : ""
seems to yield immediate improvements!

Edit: disregard. Just my PC being inconsistent.
Last edited by oif2003 on 13 Dec 2018, 01:10, edited 1 time in total.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:06

I recall I fiddled with mcode a few years ago before and re-wrote it (just the mcode function) for my needs. To be as small as possible. AHK v2 version with x64/x86 compiled code support and test code snippet:

Code: Select all

;Example:
f:=MCode("2,x86:aipYww==,x64:uCoAAADD")
MsgBox(DllCall(f, "cdecl"))

;<encoding>[,x86:<x86_mcode>][,x64:<x64_mcode>]
;1=HEX, 2=Base64

MCode(_m){
	static e:=[4,1], c:=(A_PtrSize=8) ? "x64":"x86"
	if(!regexmatch(_m, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
		return
	if(!DllCall("crypt32\CryptStringToBinary", "str",m[3], "uint",0, "uint",e[m[1]]
		, "ptr",0, "uint*",s, "ptr",0, "ptr",0))
		return
	p:=DllCall("GlobalAlloc", "uint",0, "ptr",s, "ptr")
	if(c=="x64")
		DllCall("VirtualProtect", "ptr",p, "ptr",s, "uint",0x40, "uint*",op)
	if(DllCall("crypt32\CryptStringToBinary", "str",m[3], "uint",0, "uint",e[m[1]]
		, "ptr",p, "uint*",s, "ptr",0, "ptr",0))
		return p
	DllCall("GlobalFree", "ptr",p)
}



And here is a plain C example (just returning 42)

Code: Select all

#include <windows.h>
int f(){
	return 42;
}
Last edited by vvhitevvizard on 13 Dec 2018, 01:23, edited 8 times in total.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:09

oif2003 wrote:
13 Dec 2018, 01:05

Code: Select all

indent(n) => --n ? indent(n) " " : ""
seems to yield immediate improvements!
hell yeah! this one works as expected cpu-time -wise. :D
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:13

Still not good enough :(

Code: Select all

n := 200000
indent1(n) => --n ? indent1(n) " " : ""
indent2(n) => n ? indent2(--n) " " : ""
indent3(n) {
	loop n
		s .= " "
	return s
}

	t:=A_TickCount
		loop(n)
			indent1(4)
	a1:=A_TickCount-t

	t:=A_TickCount
		loop(n)
			indent2(4)
	a2:=A_TickCount-t
	
	t:=A_TickCount
		loop(n)
			indent3(4)
	a3:=A_TickCount-t

msgbox(a1 " | " a2 " | " a3)
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:18

What if we just make this.Space a literal string... like
" "
means 4 spaces lol
Last edited by oif2003 on 13 Dec 2018, 01:28, edited 1 time in total.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:27

oif2003 wrote:
13 Dec 2018, 01:18
What if we just make this.Space a literal string... like " " means 4 spaces lol
:D
there is a reason I'd prefer to be it a number rather than a string of spaces. I used to use .ini with public/global settings for my scripts. and I have a code that auto-initiates properties/keys for classes from respective .ini keys. the issue is I cant have Space=____ (replace _ with spaces) line in .ini for the moment. not to mention some "Tabify indent" and "Replace white spaces with tabs" optimizations of smart text editors would ruin it.

Code: Select all

[json]
;0=enable indentation and line feed, 1=the most compact representation:
Compact=0
;spaces per level, similar to JavaScript's JSON.stringify() 'space' parameter
;	JSON array elements and object members will be pretty-printed with the indent level:
Space=4
;0=suppress error on built-in object types, just output a class name, e.g.: "File Object":
ErrObjType=0
;0=supress error if the root is not an object:
ErrObj=0
Still not good enough
yeah. :( 875 | 969 | 140
there r moments when we give in to ugly loops :)
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 01:50

btw what I meant by "there is room for optimization"
this new version takes 844 vs 950. +11% And thats with regex line! vs original 1170 its +28% overall.
I removed a:=(b:=s.1).__Arr superfluous thingie from the main cycle and put it where it belongs to ("}]" and "{[" branches and init line).
EDIT: ok I fixed an intermediate bug.
Now its one hell of a function but can be further optimized still. xD

Code: Select all

;json.get by coco and heavily modified by vvhitevvizard and oif2003
get(ByRef _s){ ;obj:=json.get(str)
		static q:=Chr(34), p:={"__Arr":1}, x:=q "{[0123456789-" ;json allowed chars outside strings
		n:=0, k:="", kf:=0, s:=[b:=root:={}], z:="{"
		while(c:=SubStr(_s, ++n, 1)) !== ""
			InStr(" `t`n`r", c) || (
				InStr(z, c) || E(c, n), u:=0
				, InStr("}]", c) ? ((s.1==root) && E(c, n)
					, s.RemoveAt(1), z:=(a:=(b:=s.1).__Arr) ? ",]" : ",}")
				: InStr(",:", c) ? (z:=(kf:=!a && c==",") ? q : x)
				: InStr("{[", c) ? ((c=="{") ? (kf:=1, v:={}, z:=q "}")
					: (v:=new p, z:=x "]")
						, s.InsertAt(1,v), a ? k:=b.Push(v):b[k]:=v, a:=(b:=s.1).__Arr)
				: ((c==q) ? (v:=SubStr(_s, n+1, InStr(_s,q,, n+1)-n-1)
						, n+=StrLen(v)+1, (kf) && (k:=v, z:=":", u:=1)) ;string literals
					: (v:=SubStr(_s, n, (SubStr(_s, n) ~= "[\]\},\s]|$")-1)
						, n+=StrLen(v)-1, (v is "Number") ? v+=0:E(c, n)) ;number
				 ,(u) || (a ? k:=b.Push(v):b[k]:=v, z:=a ? ",]" : ",}"))
			)
		return(s.1==root || E(c, n), s[1,""]) ;s.Count()!=1 ;unpaired {}, []
		E(_c,_n)=>this.fthrow(Exception("Unexpected char <" _c "> in pos " _n, -2))
	}

Image
Last edited by vvhitevvizard on 13 Dec 2018, 04:01, edited 2 times in total.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 03:08

oif2003 wrote:
13 Dec 2018, 01:18
I found a trivial bug in ur inline optimizations :) Compact=1 mode leaves some spaces. It should be whitespace free.
Image
1.
ok. the bug was in (this.Compact) ? n:=d:="" : n:="`n", d:=_d t -> (this.Compact) ? n:=d:="" : (n:="`n", d:=_d t). U missed parentheses so d:=_d t executed unconditionally.
2.
I changed some names in the bargain: ind to t, space to p (sounds like padding) and shortened "private" fthrow to just _T
and wrapped up a few excessively long lines in json.Set.
3.
redundant code (_d || p==this.Space) || t:=indent(p:=this.Space) -> (p=this.Space) || t:=indent(p:=this.Space)
4.
And I abandoned throw Exception. "What" property (it was used here to retrieve caller's function name for error message) makes little sense anyways. Just increases code size - throw("error message") is enuf. The caller should catch it and add whatever he likes to the error text.
And ur right, its a command (directive) and cant be inlined :(

corrected ver 1.1:

Code: Select all

class json{ ;json.get by coco and heavily modified by vvhitevvizard and oif2003
;json.set by vvhitevvizard and heavily modified by oif2003
;ver 1.1

;1) 0=enable indentation and line feed, 1=the most compact representation:
;2) spaces per level, similar to JavaScript's JSON.stringify() 'space' parameter
;	JSON array elements and object members will be pretty-printed with the indent level
;3) 0=suppress error on built-in object types, just output a class name, e.g.: "File Object"
;4) 0=supress error if the root is not an object:
	static Compact:=0, Space:=4, ErrObjType:=0, ErrObj:=0

;"stringify": format [associative] array (object) as JSON string: str:=json.set(obj)
	Set(_o, _d:="") { ;_d:current indent
		static q:=Chr(34), t, p
		if !IsObject(_o) ;"string" | number
			return this.ErrObj && this._T("Not an object.")
		(p=this.Space) || t:=indent(p:=this.Space)
			, (this.Compact) ? n:=d:="" : (n:="`n", d:=_d t), VarSetCapacity(s,1024*8)
		try a:=_o.__Arr ;trying to get a unique key
		catch ;"Unknown property" exception means its a built-in inenumerable object
			return(s:=Type(_o) " Object"
				, (this.ErrObjType) && this._T(s " type is not supported."), "{" q s q "}")
		for i in _o ;due to the use of a for-loop, arrays such as '[1,,3]' are detected as objects({})
			if !(a:=i=A_Index) ;a=0:associative or sparse array ([1,,3])
				break
;JSON keys are always "strings", expected values r: number|"string"|{object}|[array]
		for k,v in _o ;recursive
			s.=d (a ? "" : q k q ":") (isObject(v) ? this.Set(v, d) 
				: (Type(v)=="String") ? q v q : (Type(v)=="Float") ? Format("{:g}", v) : v) "," n
		return(a ? "[" : "{") (s ? (n RTrim(s, "," n) n _d) : "") (a ? "]" : "}") ;wrap to brackets
		indent(n)=>--n ? indent(n) " ":""
	}

;parse json string. obj:=json.get(str)
	Get(ByRef _s){ 
		static q:=Chr(34), p:={"__Arr":1}, x:=q "{[0123456789-" ;json: allowed chars outside strings
		n:=0, k:="", kf:=0, s:=[b:=root:={}], z:="{"
		while(c:=SubStr(_s, ++n, 1))!==""
			InStr(" `t`n`r", c) || (
				InStr(z, c) || E(c, n), u:=0
				, InStr("}]", c) ? ((s.1=root) && E(c, n)
					, s.RemoveAt(1), z:=(a:=(b:=s.1).__Arr) ? ",]" : ",}")
				: InStr(",:", c) ? (z:=(kf:=!a && c==",") ? q : x)
				: InStr("{[", c) ? ((c=="{") ? (kf:=1, v:={}, z:=q "}")
					: (v:=new p, z:=x "]")
						, s.InsertAt(1,v), a ? k:=b.Push(v):b[k]:=v, a:=(b:=s.1).__Arr)
				: ((c==q) ? (v:=SubStr(_s, n+1, InStr(_s,q,, n+1)-n-1)
						, n+=StrLen(v)+1, (kf) && (k:=v, z:=":", u:=1)) ;string literals
					: (v:=SubStr(_s, n, (SubStr(_s, n) ~= "[\]\},\s]|$")-1)
						, n+=StrLen(v)-1, (v is "Number") ? v+=0:E(c, n)) ;number
				, (u) || (a ? k:=b.Push(v):b[k]:=v, z:=a ? ",]" : ",}"))
			)
		return(s.1=root || E(c, n), s[1,""]) ;s.Count()!=1 ;unpaired {}, []
		E(_c,_n)=>this._T("Unexpected char <" _c "> in pos " _n)
	}
	_T(_s){
		throw(_s)
	}
}


; Test runs
;j:=FileRead("1678982546")
;j:='{"btc_usd":[{"type":"bid","price":3665,"amount":0.00014128,"tid":202998772,"timestamp":1544493122}]}'
j:='{"r1":{"r2":{"type1":11,"type2":11},"misc":12}, "type":"ask","price":104.2,"amount":101, "a":[111,"aa"]}'

n:=20000
t:=A_TickCount
loop(n)
	b:=json.Get(j)
a1:=A_TickCount-t

t:=A_TickCount
loop(n)
	s:=json.Set(b)
a2:=A_TickCount-t

msgbox(clipboard:=a1 "|" a2 "`n`n" s)
Last edited by vvhitevvizard on 13 Dec 2018, 14:54, edited 1 time in total.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 06:20

oif2003 wrote:
12 Dec 2018, 21:56
Two things I want to try: 1)look for replacement of regexmatch (~=), 2) try "loop parse _s" instead of using a while loop
Edit: item 2 won't work (or is too much work) since index n is changed multiple times inside the while loop
Well, 2 the most time-critical parts r obtaining quoted key's name and obtaining value's body with regex.

Code: Select all

				: ((c==q) ? (v:=SubStr(_s, n+1, InStr(_s,q,, n+1)-n-1)
						, n+=StrLen(v)+1, (kf) && (k:=v, z:=":", u:=1)) ;string literals
					: (v:=SubStr(_s, n, (SubStr(_s, n) ~= "[\]\},\s]|$")-1)
						, n+=StrLen(v)-1, (v is "Number") ? v+=0:E(c, n)) ;number
I bet m-coding these simple and small parts could do wonders.

2.
Just an idea (needs to be tested). The current algorithm marks every level of nestedness with ___Arr keys denoting object type: array [] vs object {}. We could use just one 64-bit uint and save the level type info within bits. And use (a>>=1)&1 and (a<<=1))&1 for bitshifting+masking instead of making lots of __Arr keys. Thou it effectively limits maximum nesting level to 64.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 12:01

vvhitevvizard wrote:
13 Dec 2018, 03:08
Good catch!

Code: Select all

		(_d) || space == this.Space 				;if !_d then if space != this.Space
			 || ind := indent(space := this.Space)	;then ...
If you remove space==this.Space, indent() will be called on every non-recursive call of json.set

adjusted this in json.set(): combined conditionals on (a)

Code: Select all

return (m := s ? (n RTrim(s, "," n) n _d) : "", (a ? ("[" m "]") : ("{" m "}")))
and this in json.get(): combined conditionals on (a) and removed StrLen(v)

Code: Select all

				: ((c==q) ? (_n:=InStr(_s,q,, n+1),v:=SubStr(_s, n+1, _n-n-1)
						, n:=_n, (kf) && (k:=v, z:=":", u:=1)) ;string literals
					: (_n:=SubStr(_s, n) ~= "[\]\},\s]|$",v:=SubStr(_s, n, _n-1)
						, n+=_n-2, (v is "Number") ? v+=0:E(c, n)) ;number
				, (u) || (a ? (k:=b.Push(v), z:=",]"):(b[k]:=v, z:=",}")))
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 15:11

oif2003 wrote:
13 Dec 2018, 12:01
If you remove space==this.Space, indent() will be called on every non-recursive call of json.set
if I remove _d in (_d || p=this.Space) || t:=indent(p:=this.Space), condition p=this.Space will be tested every recursive call of json.Set. With _d not removed, (_d) will be tested every recursive run respectively. So its virtually the same for run-time speed (benchmark shows no tangible difference, tho checking arg must be a tad faster than this.key). Overall I just removed 1 instruction from the algorithm. :D
btw I removed 2nd = in p==this.Space on purpose b/c this.Space is not a String: faster case-sensitive comparison doesn't take place thus rending == redundant here.
Last edited by vvhitevvizard on 13 Dec 2018, 15:44, edited 8 times in total.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 15:21

vvhitevvizard wrote:
13 Dec 2018, 15:11
Stupid me, I missread what you wrote :facepalm:
I finally broke down and adjusted it to use loop parse. I also reindented the code so hopefully I didn't break anything doing that

Code: Select all

class json{ ;json.get by coco and heavily modified by vvhitevvizard and oif2003
;json.set by vvhitevvizard and heavily modified by oif2003
;ver 1.1

;1) 0=enable indentation and line feed, 1=the most compact representation:
;2) spaces per level, similar to JavaScript's JSON.stringify() 'space' parameter
;	JSON array elements and object members will be pretty-printed with the indent level
;3) 0=suppress error on built-in object types, just output a class name, e.g.: "File Object"
;4) 0=supress error if the root is not an object:
	static Compact:=0, Space:=4, ErrObjType:=0, ErrObj:=0

;"stringify": format [associative] array (object) as JSON string: str:=json.set(obj)
	Set(_o, _d:="") { ;_d:current indent
		static q:=Chr(34), t, p
		
		if !IsObject(_o) ;"string" | number
			return this.ErrObj && this._T("Not an object.")
		
		(p=this.Space) || t:=indent(p:=this.Space)
			
		, (this.Compact) ? n:=d:="" : (n:="`n", d:=_d t)
		
		, VarSetCapacity(s,1024*8)
		
		try ;to get a unique key
			a:=_o.__Arr 
		catch ;"Unknown property" exception: its a built-in inenumerable object
			return (
				s:=Type(_o) " Object"
				, (this.ErrObjType) && this._T(s " type is not supported.")
				, "{" q s q "}"
			)
		for i in _o ;due to the use of a for-loop, arrays such as '[1,,3]' are detected as objects({})
			if !(a:=i=A_Index) ;a=0:associative or sparse array ([1,,3])
				break
;JSON keys are always "strings", expected values r: number|"string"|{object}|[array]
		for k,v in _o ;recursive
			s .= d (a ? "" : q k q ":") 
				. (
					  (isObject(v)) 		? this.Set(v, d) 		;if then
					: (Type(v)=="String") 	? q v q 				;elseif then
					: (Type(v)=="Float") 	? Format("{:g}", v) 	;elseif then
					: v												;else
				) 
				. "," n
		
		return (	
			m := s ? (n RTrim(s, "," n) n _d) : ""
			,(a ? ("[" m "]") : ("{" m "}"))						;return	
		)
		
		indent(n) {
			loop n
				_ .= " "
			return _
		}
	}

;parse json string. obj:=json.get(str)
	Get(ByRef _s){ 
		static q:=Chr(34), p:={"__Arr":1}, x:=q "{[0123456789-" ;json: allowed chars outside strings
		n:=0, k:="", kf:=0, s:=[b:=root:={}], z:="{"

		loop parse _s
			(A_Index < n) || InStr(" `t`n`r", A_LoopField) || (	;if A_Index >= n && !InStr(...)
				n := A_Index
				
				, InStr(z, A_LoopField) || E(A_LoopField, n)		;if !InStr(...) then E(...)
				, u:=0
				
				, InStr("}]", A_LoopField) ?(						;if
					(s.1=root) && E(A_LoopField, n)
					, s.RemoveAt(1)
					, z := (a:=(b:=s.1).__Arr) ? ",]" : ",}"
				): InStr(",:", A_LoopField) ?(						;else if
					z:=(kf:=!a && A_LoopField = ",") ? q : x
				): InStr("{[", A_LoopField) ?(						;else if
					(A_LoopField = "{") ?(								;if
						kf:=1
						, v:={}
						, z:=q "}"
					):(													;else
						v:=new p
						, z:=x "]"
					)
					, s.InsertAt(1,v)
					, a ? k:=b.Push(v) : b[k]:=v
					, a:=(b:=s.1).__Arr
				):(													;else
					(A_LoopField = q) ?( 								;if string literals
						_n:=InStr(_s,q,, n+1)
						,v:=SubStr(_s, n+1, _n-n-1)
						,n:=_n
						,(kf) && (k:=v, z:=":", u:=1)
					):( 												;else number
						_n:=SubStr(_s, n) ~= "[\]\},\s]|$"
						,v:=SubStr(_s, n, _n-1)
						,n+=_n-2
						, (v is "Number") ? v+=0 :E(A_LoopField, n)
					) 
					, (u) || (											;if !u
						(a) ?(												;if
							k:=b.Push(v), z:=",]"
						):(													;else
							b[k] := v,z := ",}"
						)
					)
				)
				, n++
			)
			
		return (
			(s.1=root) || E(_, n)									;if s.1!=root then E(...)
			, s[1,""]												;return unpaired {}, []
		)
		
		E(_c,_n)=>this._T("Unexpected char <" _c "> in pos " _n)
	}
	
	_T(_s){
		throw(_s)
	}
}


#SingleInstance force
; Test runs
	j:="
(
{
    "a":[
        111,
        "aa"
    ],
    "amount":101,
    "price":104.2,
    "r1":{
        "misc":12,
        "r2":{
            "type1":11,
            "type2":11
        }
    },
    "type":"ask"
}

)"

t:=A_TickCount
loop(20000)
	b:=json.get(j)
a1:=A_TickCount-t

t:=A_TickCount
loop(20000)
	s:=json.set(b)
a2:=A_TickCount-t
	
msgbox(a1 "|" a2 "`n`n" s)
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 16:35

return(m:=s ? (n RTrim(s, "," n) n _d):"", a ? "[" m "]":"{" m "}") ;wrap to brackets
good idea on the paper. BUT it adds 1 additional assignment m:= (with redundant chars copying) so actually its either the same cputime-wise for short substrings or makes code even much slower for long substrings. I told u before that return() line for json.Set is tricky for optimizing. :D We have to use as few assignments (for modified strings) as possible. Thats why I came up to the idea of cutting strings' length (by forcing zero-termination with NumPut()) instead of using plain s:=Trim(s,"`n"). I feel sorry that AHK lacks a built-in function for changing its cached string's length.
Last edited by vvhitevvizard on 13 Dec 2018, 16:49, edited 1 time in total.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Problems with objects and legacy syntax

13 Dec 2018, 16:46

vvhitevvizard wrote:
13 Dec 2018, 16:35
return(m:=s ? (n RTrim(s, "," n) n _d):"", a ? "[" m "]":"{" m "}") ;wrap to brackets
good idea on the paper. BUT it adds 1 additional assignment m:= (with redundant chars copying) so actually its either the same cputime-wise for short substrings or makes code even much slower for long substrings. I told u before that return() line for json.Set is tricky for optimizing. :D We have to use as few assignments (for modified strings) as possible. Thats why I came up to the idea of cutting strings' length (by forcing zero-termination with NumPut()) instead of using plain s:=Trim(s,"`n")
true that, this ought to fix it

Code: Select all

		return (			
			(a) ? ("[" (s ? (n RTrim(s, "," n) n _d) : "") "]") 
				: ("{" (s ? (n RTrim(s, "," n) n _d) : "") "}")
		)
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Problems with objects and legacy syntax

13 Dec 2018, 17:04

Code: Select all

		return((a) ? ("[" (s ? (n RTrim(s, "," n) n _d) : "") "]")
			: ("{" (s ? (n RTrim(s, "," n) n _d) : "") "}")) ;wrap in brackets
this one works w/o redundant strings copying. but we just effectively duplicate the same line twice thus increasing code size and its readability. :D benchmark says there is no difference.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: gdqb521, niCode, Rohwedder, songdg and 36 guests