Others are more qualified to answer the specific question about SubArrays,
but you might check out the ArrayViews package. For your test, it
allocates a little under half the memory and is a little over twice as fast
(after warmup);
julia> const b = [1:5;];
julia> function f()
for i in 1:1_000_000 sub(b, 1:2) end
end
f (generic function with 1 method)
julia> using ArrayViews
julia> function f2()
for i in 1:1_000_000 view(b, 1:2) end
end
f2 (generic function with 1 method)
julia> @time f() # after warmup
elapsed time: 0.048006869 seconds (137 MB allocated, 6.80% gc time in 6
pauses with 0 full sweep)
julia> @time f2() # after warmup
elapsed time: 0.018902176 seconds (61 MB allocated, 6.60% gc time in 2
pauses with 0 full sweep)
Cheers,
Kevin
On Wed, Mar 25, 2015 at 11:18 AM, Sebastian Good <
[email protected]> wrote:
> I was surprised by two things in the SubArray implementation
>
> 1) They are big! About 175 bytes for a simple subset from a 1D array from
> my naive measurement.[*]
> 2) They are not flat. That is, they seem to get heap allocated and have
> indirections in them.
>
> I'm guessing this is because SubArrays aren't immutable, and tuples
> aren't always inlined into an immutable either, but I am really grasping at
> straws.
>
> I'm walking through a very large memory mapped structure and generating
> hundreds of thousands of subarrays to look at various windows of it. I was
> hoping that by using views I would reduce memory usage as compared with
> creating copies of those windows. Indeed I am, but by a lot less than I
> thought I would be.
>
> In other words: SubArrays are surprisingly expensive because they
> necessitate several memory allocations apiece.
>
> From the work that's gone into SubArrays I'm guessing that isn't meant to
> be. They are so carefully specialized that I would expect them to behave
> roughly like a (largish) struct in common use.
>
> Is this a misconception? Do I need to take more care about how I
> parameterize the container I put them in to take advantage?
>
> [*]
> > const b = [1:5;]
> > function f()
> for i in 1:1_000_000 sub(b, 1:2) end
> end
> > @time f()
> elapsed time: 0.071933306 seconds (175 MB allocated, 9.21% gc time in 8
> pauses with 0 full sweep)
>