Reference to Objects Very Slow

My actual example is conceptually like this:
A Family has an Array of Children.
Families send their Children to Schools, so each School will have an Array of Children.
I do not want to copy the Children, so the School has an Array of Ref{Child}. Seems to work.
But accessing the Children via these Ref values seems glacial. I must be doing something wrong!!?? Here is a code example

type Foo
  i::Int64
end

function as_array(foos::Array{Foo,1})
  for f in foos
    f.i += 1
  end
end

function with_refs(foos::Array{Ref{Foo},1})
  for f in foos
    f.x.i += 1                   # is this the right way to dereference a Ref? Seems to work...
  end
end

foos = Array{Foo,1}()            # couple of empty arrays
ref_foos = Array{Ref{Foo},1}()

for i in 1:100000                # fill the arrays
  push!(foos, Foo(0))
  push!(ref_foos, Ref(foos[end]))
end

as_array(foos); @time as_array(foos)

with_refs(ref_foos); @time with_refs(ref_foos)

for i in 1:5
  @printf(STDERR, "i = %d foo.i %d %d\n", i, foos[i].i, ref_foos[i].x.i)     # seems to be working
end

When I run this, I get

0.000405 seconds (130 allocations: 7.719 KB)
0.013922 seconds (4 allocations: 160 bytes)

of course my actual task is much longer running, but doing things via a reference is > 30x slower.

Was not able to find help anything like this. In the C++ world, having pointers to objects is quite natural, but how should one go about this in Julia?

Any advice most welcome…

thanks

Ian

What are you trying to do? Foo is already a mutable type so there’s no need to put it in a Ref.

Ref is an abstract type so you’ll get terrible performance when using it as the type of a slot.

No. There’s no way to dereference a Ref in a generic way. For Base.RefValue, use ref[].

1 Like

I get it, thank you Yuyichao

I can create an Array of Foos, then put those Foo’s in any number of other Arrays, and it is the same Foo item. I had assumed that adding the Foo objects to another Array would make a copy of each item - like what would happen in C++.

Julia is not C++ - too many languages to keep straight!

sorry…

Thank you for helping me, timing looks much better now - code is much simpler too!

Ian