I tested it on 0.4-dev+1310 and it fixed the allocations in the example 
above. But my real program was still allocating at the same spot. It seems 
to be a different issue unrelated to immutables. I've boiled it down to the 
example below. It's a little contrived as my real aim is to write to memory 
not allocated by Julia. It seems to be something to do with accessing 
arrays directly versus through a field? I've tried splitting it up into 
separate type-annotated functions but there was no change.

module immtest

type Container
array::Array{Float32}
size::Uint32
end

function runtest(reps)
dst = resize!(Float32[], 100_000)
src = resize!(Float32[], 10)
container = Container(dst, 1000)

@time begin
for i=1:reps
# This does not cause allocation
# copy!(dst, container.size+1, src, 1)

# This does
copy!(container.array, container.size+1, src, 1)
end
end
end

runtest(10_000_000)

end




On Sunday, December 7, 2014 3:06:52 AM UTC+11, Stefan Karpinski wrote:
>
> I can reproduce this on 0.3. Looking into it.
>
> On Sat, Dec 6, 2014 at 7:04 AM, Tim Holy <[email protected] <javascript:>> 
> wrote:
>
>> Curiously, I don't even see it on my copy of julia 0.3.
>>
>> Will, one tip: julia optimizes within functions. Anytime you see something
>> weird like this, try to separate allocation and operation into two 
>> separate
>> functions. That way, the function that's performing lots of computation 
>> will
>> receive a concrete type as an input, and be well-optimized.
>>
>> --Tim
>>
>> On Friday, December 05, 2014 06:38:28 PM John Myles White wrote:
>> > I think this might be a problem with Julia 0.3. I see it on Julia 0.3, 
>> but
>> > not on the development branch for Julia 0.4.
>> >
>> >  — John
>> >
>> > On Dec 5, 2014, at 6:27 PM, Will Dobbie <[email protected] <javascript:>> 
>> wrote:
>> > > Hi,
>> > >
>> > > I have a program which copies elements between two arrays of 
>> immutables in
>> > > a tight loop. The sizes of the arrays never change. I've been 
>> struggling
>> > > to get it to avoid spending a large chunk of its time in the garbage
>> > > collector. I have an example of what I mean below.
>> > >
>> > > With arrays of Int64 I get:
>> > > elapsed time: 0.164429425 seconds (0 bytes allocated)
>> > >
>> > > With arrays of an immutable the same size as Int64 I get:
>> > > elapsed time: 1.421834146 seconds (320000000 bytes allocated, 15.97% 
>> gc
>> > > time)
>> > >
>> > > My understanding was arrays of immutables should behave like arrays of
>> > > structs in C and not require heap allocation. Is there a way I can
>> > > achieve that? I'm using Julia 0.3.3.
>> > >
>> > > Thanks,
>> > > Will
>> > >
>> > >
>> > >
>> > > module immtest
>> > >
>> > > immutable Vec2
>> > >
>> > >     x::Float32
>> > >     y::Float32
>> > >
>> > > end
>> > >
>> > > # typealias element_type Vec2               # Results in allocation 
>> in the loop
>> below
>> > > typealias element_type Int64                # Does not cause 
>> allocation
>> > >
>> > > function runtest(reps)
>> > >
>> > >     dst = resize!(element_type[], 100_000)
>> > >     src = resize!(element_type[], 10)
>> > >
>> > >     @time begin
>> > >
>> > >             for i=1:reps
>> > >
>> > >                     copy!(dst, 1000, src, 1, length(src))
>> > >                     # dst[1000:1009] = src          # same 
>> performance as above
>> > >
>> > >             end
>> > >
>> > >     end
>> > >
>> > > end
>> > >
>> > > runtest(10_000_000)
>> > >
>> > > end
>>
>>
>

Reply via email to