If you have an array of immutables you can replace the elements with new ones:

julia> immutable Immut
       x::Int64
       y::Float64
       end

julia> a = Array(Immut, 5)
5-element Array{Immut,1}:
 Immut(140562692057328,6.9447197253385e-310)
 Immut(493921239054,6.94471970408933e-310)
 Immut(140562692025040,0.0)
 Immut(493921239054,6.94471972113815e-310)
 Immut(140562692025104,0.0)


julia> a[4] = Immut(3, 4.5)
Immut(3,4.5)

julia> a
5-element Array{Immut,1}:
 Immut(140562692057328,6.9447197253385e-310)
 Immut(493921239054,6.94471970408933e-310)
 Immut(140562692025040,0.0)
 Immut(3,4.5)
 Immut(140562692025104,0.0)

You just can’t mutate the individual elements:

julia> a[4].x = 1
ERROR: type Immut is immutable

Does that do what you need?

-s



> On Jul 26, 2015, at 12:37 PM, andrew cooke <[email protected]> wrote:
> 
> 
> I have a stack of objects that changes size.  It never gets very large, but 
> it varies in size a lot.  Much of my program's time is spent creating objects 
> to push onto the stack and then GCing them when they are popped.
> 
> This seems like an "obvious" place to do some kind of manual pooling of 
> instances.  Has anyone tried this?
> 
> The simplest approach would be to allocate an array of these things, and then 
> mutate the existing instances instead of actually adding/deleting instances.  
> The array would need to grow a few times as the stack increases, but would 
> reach some limit and then be stable.
> 
> Currently the objects are Julia "immutable" types.  Presumably there's an 
> advantage to have them all in contiguous memory (inside the array/stack).  
> But then I cannot mutate them. 
> 
>  If I make them "type" instances then I don't see how I get them in 
> contiguous memory.  Particularly during re-allocation when the array grows.
> 
> So...
> 
> Does the above approach to memory pooling make sense?  Has anyone done 
> something similar?  Does the worry about memory locality make sense?  Is 
> there a solution?
> 
> Thanks,
> Andrew
> 

Reply via email to