Page MenuHomeFeedback Tracker
Feed Advanced Search

May 10 2016

ckfinite added a comment to T73276: Arrays are rejected by operators (e.g. ==, in, -; comparison by reference request).

Identical is not equivalent to reference equality. Let's assume that isEqualTo implements reference equality, and consider a trivial IL transformation of the earlier code, as would happen in a standard compiler.

[1,2,3] isEqualTo [1,2,3]
becomes
a=[1,2,3]
b=[1,2,3]
rval = isEqualTo a, b
Now, let's assume that the compiler is smart enough to know that a and b are not aliased in isEqualTo, and stores their arrays in program memory. Then, this would become a notional in-memory form (with made-up indices) of
0x60000: 1
0x60004: 2
0x60008: 3
0x6000C: 1
0x60010: 2
0x60014: 3
Then, a = 0x60000 and b = 0x6000C. Then, while every element in the arrays is the same, a != b (assume that a = b, then 0x60000 = 0x6000C iff 0 = 12, which is patently false). Therefore, a isEqualTo b, if isEqualTo implements reference equality, should be false.

However, isEqualTo clearly does not implement reference equality. In the above example, it doesn't compare 0x60000 and 0x6000C, rather, it compares 1=1, 2=2, and 3=3. This means that it's arbitrarily slower (in big omega of n) than the reference equality implementation - to see why, make two identical arrays of a billion elements, and run isEqualTo on them. The notional reference equality implementation is instant - it doesn't depend on the size of the arrays, but isEqualTo could take a very long time indeed.

Note that the actual implementation of the arrays will cause code that looks more like
a=Array.create(1,2,3)
b=Array.create(1,2,3)
to be generated. This means that the pointers are actually looking at heap elements, but the same idea holds.

May 10 2016, 6:33 AM · Arma 3
ckfinite added a comment to T73276: Arrays are rejected by operators (e.g. ==, in, -; comparison by reference request).

I'm not actually that familiar with Arma scripting, though the use cases for this are pretty clear generally.

There are two big cases that reference equality is helpful: when comparisons are expensive, and when arrays are large. I don't think you can define your own equality and objects, so the first case doesn't apply to Arma. However, the second case can apply.

Suppose you have an array with a hundred million elements, and you want to see if an array that was just given to you is the same array. If you use isEqualTo, it will have to compare every single one of those hundred million elements, which, if each comparison can happen in a millionth of a second, will take 100 seconds. In contrast, a reference equality implementation would take a millionth of a second.

May 10 2016, 6:33 AM · Arma 3
ckfinite added a comment to T73276: Arrays are rejected by operators (e.g. ==, in, -; comparison by reference request).

He's saying that [1,2,3] isEqualTo [1,2,3]. The two arrays have different memory pointers (unless you have a *very* aggressive optimizing compiler), yet isEqualTo returns true.

This indicates that isEqualTo is linear time, as well.

May 10 2016, 6:33 AM · Arma 3
ckfinite added a comment to T73276: Arrays are rejected by operators (e.g. ==, in, -; comparison by reference request).

Array subtraction is a bit sketchy, especially if applied to ordered lists (e.g. arrays). If there is a special set datatype, then that should support subtraction, but not arrays. The same goes for the in operator, especially as over arrays it's necessarily a O(n) operation, whereas a hashtable or tree backed set implementation may provide O(1) or O(log n) performance.

Is isEqualTo an O(n+m) or O(1) operation over the elements in the arrays? For many applications, constant-time reference equality is required, and it sounds as if isEqualTo is a linear time algorithm.

I also want to add that there must be a reference pointer somewhere - even if they're linked lists or something more exotic, they're still in memory somewhere, and that can be used to do O(1) comparisons. Basically every other language (even most functional ones) have this, because it's very convenient.

May 10 2016, 6:33 AM · Arma 3
ckfinite added a comment to T73276: Arrays are rejected by operators (e.g. ==, in, -; comparison by reference request).

This is equivalent JS code:
var a = [1,2,3];
var b = a;
if (a == b) {alert ('match')} else {alert ('no match')};

var c = [a]
if (a == c[0]) {alert ('match')} else {alert ('no match')};

This produces two dialogs with the text "match". This functionality is shared by Java, C, C++, C#, Python, x86 assembly, and many, many others. It is called reference equality, and is an artifact of the underlying implementation of the language. Computers implement arrays by having contiguous blocks of memory containing the array values. What is stored in the variable is actually the integer offset into memory.

This is best exemplified by how C handles array accesses. The syntax x[n] in C is equivalent to (x + n)*, or adding n to the pointer x and dereferencing the memory location. The array that the programmer interacts with is x, but x is just the integer memory location where the array is stored.

This ticket is asking for the ability to compare arrays by their reference pointer. In the above javascript example, a, b, and c[1] all have the same integer value, and logically should be equivalent.

May 10 2016, 6:33 AM · Arma 3