arrays of structs - performance

I am evaluating swift for a certain performance sensitive app and I am hitting a performance wall ( pretty terryfing one at that ) which I am trying to understand ..


I have a struct defined as:

struct DataBlock
{
    var counter:UInt=0
    var values:[UInt64]=[]
}


Then I crate an array of these structs :


var dataArray:[DataBlock]
dataArray=Array(count: aSize, repeatedValue: DataBlock())


My understanding here is that this should behave more/less like C++ equivalent with a continous array of DataBlock structs - in terms of repeatedly accessing content of that array it should be cache friendly.

Unfortunately this ends up being extremely slow ( 10 times slower than an equivalent Java code and 40 times lower than C++ ) - all of that compiled/tested with -OFast.

When profiling I see basically release/retain taking most of execution time which, I think , means I don't rally understand structs in Swfit - looks like there is still some boxing/unboxing going on big time.


For instance , if I extract the array size into a variable and index on that , the code becomes twice as fast

            let valuesArraySize=dataArray[i].values.count          
            for var j = 0; j < valuesArraySize; ++j


This, on the other hand, is twice as slow:

            for var j = 0; j < dataArray[i].values.count; ++j
            {


So obviously, this points to data being retained/released on every access within the array which is really odd given that I am using structs ...


Any ideas what I am doing wrong ?

Can you post some sample code that reproduces what are you saying? Because without knowing exactly what you are doing is hard to guess where the problem lies.

arrays of structs - performance
 
 
Q