LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

"Is Array Empty" Compiler Optimization?

Solved!
Go to solution

Let's say I have an array of objects which themselves contain a number of children which contain a number of children and so on.  While the top level object array might only contain a handful of elements, because of this hierarchical nesting, each of those elements could contain a fairly large amount of data.  Is there any way to perform an operation such as checking if the array is empty or getting the number of elements without having to create a copy of the entire array or is there any optimization done by the compiler in such a case?



I saw my father do some work on a car once as a kid and I asked him "How did you know how to do that?" He responded "I didn't, I had to figure it out."
0 Kudos
Message 1 of 6
(3,852 Views)

If you are just reading the values in a VI, you can use Index Array and a data copy will not be made.  If you are doing updates, then look to the In Place Element Structure to make sure the compiler does not make the copies.  Though, the compiler has gotten really good at handling read-modify-write setups for arrays without the IPES.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 6
(3,844 Views)

This wouldn't be read-modify-write.  For sure, to make changes to an element of the array, the in place structure is then obvious candidate.

 

But something like this:

inplace is empty.png

would still (I'm assuming) have to create a copy of the entire array yes?



I saw my father do some work on a car once as a kid and I asked him "How did you know how to do that?" He responded "I didn't, I had to figure it out."
0 Kudos
Message 3 of 6
(3,835 Views)
Solution
Accepted by topic author blackburnite

@blackburnite wrote:

But something like this:

 

would still (I'm assuming) have to create a copy of the entire array yes?


No, it does not.  You don't even need the In Place Element Structure there.  Just because there is a branch does not necessarily mean you have created a data copy.  The compiler is smart enough to avoid those data copies.  When you start modifying the data in multiple branches is when data copies will occur.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 6
(3,821 Views)

It definitely shouldn't cause a data copy of the array, unless someone writing the LV made a horrible mistake.

LabVIEW stores the array with size information prepended in memory, so the "is array empty" should just check the value of the data size byte(s), no need to even look at the contents of the array.

Source

Also checked the buffer allocation tool in LabVIEW for this case, it shows no new array buffers. You wouldn't need the IPE structure either.

After all, branching a wire doesn't necessarily mean a copy, it depends on what the wire is used for.

Ian
LabVIEW since 2012
Message 5 of 6
(3,819 Views)

OK, excellent, that's what I wanted to know.  I assumed compiler optimization would handle that but I haven't dug into it too in depth yet.  thanks.



I saw my father do some work on a car once as a kid and I asked him "How did you know how to do that?" He responded "I didn't, I had to figure it out."
0 Kudos
Message 6 of 6
(3,818 Views)