## Obj.Count() using Numget??

Get help with using AutoHotkey and its commands and hotkeys
swagfag
Posts: 4092
Joined: 11 Jan 2017, 17:59

### Obj.Count() using Numget??

can someone explain the internals of why this works: https://www.autohotkey.com/boards/viewtopic.php?p=73396#p73396
specifically

Code: Select all

``NumGet(&this + 4*A_PtrSize)``
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

I recently figured out myself how to do more advanced stuff for AHK v2 (its inner object's format is different from AHK v1)
U might be interested: https://www.autohotkey.com/boards/viewtopic.php?p=258935#p258935
In the proof of concept implementation I do direct string's length manipulations w/o copying it and w/o recalculating new length.
It would increase performance for large strings cuz we avoid copying string data and just change its length along with field capacity (required) and add terminating zero at new place.
Ofc all that is desperate stuff due to lack of functionality in AHK.

concerning AHK v1 inner format:
https://www.autohotkey.com/boards/viewtopic.php?t=6646

for AHK v1 x64 field offsets r as follows (8 here is A_PointerSize):

Code: Select all

``````0		vTbl pointer
2*8 8		mRefCount
mBase: address of base object, 0 if none
16		mFields: Array of pointers to the object's keys
4*8 32	mFieldCount: Current number of key-value pairs = Obj.Count()
5*8 40	mFieldCountMax: Current capacity of the object = Obj.GetCapacity()
6*8 48	mKeyOffsetObject: Index of the first possible object key in mFields
This is equal to the current number of integer keys
The current number of object keys is: mKeyOffsetString-mKeyOffsetObject
The current number of string keys is: mFieldCount - mKeyOffsetString``````
Last edited by vvhitevvizard on 23 Jan 2019, 11:05, edited 2 times in total.
swagfag
Posts: 4092
Joined: 11 Jan 2017, 17:59

### Re: Obj.Count() using Numget??

great, now i wanna know how whoever came up with this came up with this
"lemme check the source. oh look, ill just access this pointer here"
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

swagfag wrote:
23 Jan 2019, 10:06
great, now i wanna know how whoever came up with this came up with this
"lemme check the source. oh look, ill just access this pointer here"
how? well, for a few years AHK missed a bullet-proof (reliable, convenient, performance-wise decent) way to count pairs key-value in the associative arrays and objects.

As for me, I had a task of increasing performance for large text strings. I needed direct string's length manipulations. Mainly to cut them. That was the reason I started to fiddle with it myself.
Btw in the topic u will find SSE 4.2 boosted StrLen mcode I wrote - its up to 15 times as fast compared to StrPut/VarSetCapacity(buf,-1) length calculations.
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Obj.Count() using Numget??

Simply put, It works because the instance of the (c++) class is basically a stuct, where members (non-static at least) appear in the order they are defined. According to msdn, the location of the vtable is guaranteed. I'm not sure if the layout of the object is otherwise guaranteed by the c++ standard, this might be common, guaranteed or VS specific. In any case, you cannot rely on the implementation not changing, thus breaking your script.

With the above in mind, and since Object is derived from ...IUnknown, we see that we can do,

Code: Select all

``````o := {}
msgbox dllcall(numget(numget(&o)+a_ptrsize), 'ptr', &o, 'uint') 		; addref
msgbox dllcall(numget(numget(&o)+a_ptrsize*2), 'ptr', &o, 'uint') 		; release
``````
@ vvhitevvizard, more (useless) explorations here, see the spoiler.

Cheers
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Helgef wrote:
23 Jan 2019, 10:54
@ vvhitevvizard, more (useless) explorations here, see the spoiler.
thank u, Helgef! very interesting
Helgef wrote:
23 Jan 2019, 10:54
In any case, you cannot rely on the implementation not changing, thus breaking your script.
No doubt. All that r bad practices. But a script can be "statically-linked" ("compiled" into .exe) with particular AHK version. And sometimes performance > compatibility.

And recall my query about VarSetCapacity: how to retrieve its internally stored size? It seems the only way for now is to read its internal data. Tho I don't yet figure out how to get to regular (not object fields) variable internals yet... Its just an academic question anyways - one could just store settings he used with VarSetCapacity.
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Helgef wrote:
23 Jan 2019, 10:54
@ vvhitevvizard, more (useless) explorations
One more thought. They r definitely NOT useless. For example, if one writes in C++ language and studies how C++ compiler turns his lines into resulting machine assembly code, he can find ways/styles of writing more effective C++ algorithms with increased performance and reduced resources consumption, w/o even programming in assembly directly.

Using undocumented AHK features is NOT recommended ofc, but knowing how high-level objects and variables r stored in the process memory and ways they r processed by the interpreter is a MUST for advanced AHK users.
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Interesting thing is: key name can be a floating point, but it is stored as a string actually
{0.5:"0.6"} -> {"0.5":"0.6"}
{0.5:0.6} -> {"0.5":0.5999999999999998}

and 2nd:
I didn't know that key's _name_ can be an object: ["obj_key"]:["obj_val"]. I thought such a construction is illegal.

Someone might conclude a few tips for optimization: using integer-type names for some keys in an associative array/object would increase performance when accessing them b/c processing integers is faster than processing strings (internally). One might even think of making the whole "object name's set" as integers.
Last edited by vvhitevvizard on 24 Jan 2019, 03:32, edited 1 time in total.
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Obj.Count() using Numget??

doc.
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Obj.Count() using Numget??

Integer keys would be faster to find mainly due to numeric comparison being faster than case insensitive string comparison, not due to their internal placement. These are assumptions.

Cheers.
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Helgef wrote:
24 Jan 2019, 03:30
doc.
What I meant is "obj_key":["obj_val"] is correct for JSON. and ["obj_key"]:["obj_val"] is illegal.
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Helgef wrote:
24 Jan 2019, 03:33
Integer keys would be faster to find mainly due to numeric comparison being faster than case insensitive string comparison, not due to their internal placement. These are assumptions.
Yes. I was in the middle of correcting my post above. Internally they r divided into 3 grps and searched separately. But integer keys r like 2-3 times as fast according to my synthetical tests
Last edited by vvhitevvizard on 24 Jan 2019, 03:48, edited 1 time in total.
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

### Re: Obj.Count() using Numget??

What does AHK have to do with JSON other than using a similar syntax?
Recommends AHK Studio
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

nnnik wrote:
24 Jan 2019, 03:39
What does AHK have to do with JSON other than using a similar syntax?
Common sense. U know why Python is so popular? B/c Almost everything in it has a unified syntax. I don't consider a choice of esoteric, multiple ways of syntax as a virtue.
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

### Re: Obj.Count() using Numget??

How does that relate to my comment?
Recommends AHK Studio
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

Helgef wrote:
24 Jan 2019, 03:30
doc.
Also:
The key's value is preserved, but its type identity is not. That is, integers may be stored as strings or vice versa, so long as the value remains the same (including the formatting of numeric strings).
Doesn't it look like making a key _name_ as an object makes no sense. Just a quirk of syntax. key name _IS_ stored internally as an object type tho. Im lost
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

### Re: Obj.Count() using Numget??

It is both useful and guaranteed. Javas HashMaps have similar features - I don't understand why you shouldn't use this.
Generally in AHK type identity between strings and integers/floats is not guaranteed.
Recommends AHK Studio
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

### Re: Obj.Count() using Numget??

Doesn't it look like making a key _name_ as an object makes no sense.
I do not know what you mean by key _name_.
The key's value is preserved,
So the key doesn't have a _name_, it has a _value_, (and is associated with another value)

Code: Select all

``````for k, v in { func('msgbox') : 'hello world' }
%k%(v)
``````
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

### Re: Obj.Count() using Numget??

How does that relate to my comment?
Do u believe quirks and irregularities compared to recognized standards would make AHK better? AHK can be whatever its current developers want it to be. But if AHK is to compete with top 50 languages at least, it has to be structured and clear as much as possible. It had the long haul coming from a script with set of commands (AutoIt v1) to an interpreting language (AHK v2), why should it stop in midway? Lexikos is genius, the community is great here. The issue lies in trying to stick to esoteric heterogeneous features by the cost of language clarity. My humble opinion.
I do not know what you mean by key _name_.
"string name" or integer index it can be referenced to. e.g. JSON format has only 1 type for a key's name: string (array elements has no names).
Last edited by vvhitevvizard on 24 Jan 2019, 04:19, edited 1 time in total.
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

### Re: Obj.Count() using Numget??

Maybe you just have very limited experience in programming. At the very least that would explain your reaction.
Using an object as key is nothing amazing and is done in many languages in different ways.
AHK just happens to be one of them.
Recommends AHK Studio