# Avoiding allocations in \`view\`s

**URL:** <https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983>\
**Category:** General Usage\
**Created:** [July 3, 2019, 1:05pm UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983 "2019-07-03T13:05:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 3, 2019, 1:05pm UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/1 "2019-07-03T13:05:29Z")

</div>

Is there a way to avoid allocations with `view` and `missing` values? If I change the inner loop to `sum` then there are allocations in both tests.

```julia
using BenchmarkTools

X = randn(10, 1000)
Xm = convert(Array{Union{Float64, Missing}}, X)

function colsums(x::Array{T}) where T
    y = Vector{T}(undef, size(x, 2))
    @inbounds for i in 1:size(x, 2)
        xx = view(x, :, i)
        x0 = 0
        for j in eachindex(xx)
            x0 += xx[j]
        end
        y[i] = x0
    end
    return y
end

@btime colsums(X);
@btime colsums(Xm);

```

```julia
julia> @btime colsums(X);
  9.974 μs (1 allocation: 7.94 KiB)

julia> @btime colsums(Xm);
  247.901 μs (20490 allocations: 329.08 KiB)

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 3, 2019, 1:38pm UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/2 "2019-07-03T13:38:10Z")

</div>

One option is UnsafeArrays; see e.g. this post: [Array views becoming dominant source of memory allocation - #32 by oschulz](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/32). The current rule of thumb is that creating an array view will allocate if the view is used as an argument to a non-inlined function or if it is returned from a function.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [July 3, 2019, 1:42pm UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/3 "2019-07-03T13:42:06Z")

</div>

Your `x0` is being initialized as an `Int` and winds up getting promoted on every iteration. This is definitely causing a lot of extra allocations, though I’m not quite sure why it goes so much more badly wrong with `missng`.

```julia
function colsums(x::Array)
    y = Vector{eltype(x)}(undef, size(x, 2))
    @inbounds for i ∈ 1:size(x, 2)
        xx = view(x, :, i)
        x0 = zero(eltype(x))
        for j ∈ eachindex(xx)
            x0 += xx[j]
        end
        y[i] = x0
    end
    y
end

```

```julia
julia> @btime colsums(Xm);
  11.640 μs (1 allocation: 8.94 KiB)

```

with your version I get

```julia
julia> @btime colsums(Xm);
  340.882 μs (20490 allocations: 329.08 KiB)

```

This somehow beats `vec(sum(Xm, dims=1))` which seems odd. I wonder if it’s worth opening an issue.

```julia
julia> @btime vec(sum(X, dims=1));
  4.937 μs (3 allocations: 8.02 KiB)

julia> @btime vec(sum(Xm, dims=1));
  15.836 μs (3 allocations: 8.98 KiB)

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 3, 2019, 3:11pm UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/4 "2019-07-03T15:11:56Z")

</div>

O, thanks for spotting this, this mistake was from the MWE only, it didn’t solve my problem. What solved big part of the problem was removing an unneeded `where T` from some functions.

`sum` is probably slower because it does pairwise aggregation for better accuracy and therefore has some overhead.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 4, 2019, 1:57am UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/5 "2019-07-04T01:57:53Z")

</div>

`sum(xx)` is slower than a loop because it isn’t inline, so the compiler heap allocates your views, and because 10 rows is too short for vectorization.

> What solved big part of the problem was removing an unneeded `where T` from some functions.

I keep getting bit by that, and would love to hear if anyone has a good solution.  
Once upon a time this triggered an error. I wish that we’re still the case.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 4, 2019, 6:46am UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/6 "2019-07-04T06:46:25Z")

</div>

> [@Elrod](#):
>
> I keep getting bit by that

A while a go I opened an issue for `LanguageServer.jl` to issue a warning.

> <https://github.com/julia-vscode/StaticLint.jl/issues/49>
>
> The following is valid julia code, but I guess it should warn that \`S\` is superf…luous:
> \`\`\`
> julia\> f(x::T) where T where S = 3
> \`\`\`
> \`\`\`
> julia\> f(x::T) where T where S = S
> \`\`\`
> gives a runtime error and should probably be marked as an error.

The code I am encountering this problem has some really strange other issues, too (e.g. it reaches the unreachable in julia 1.0, 1.1, 1.2, and master due to some typeinference bug and the only thing that helps is manually inlining everything) and I am really struggling to create a reproducible example.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 4, 2019, 7:30am UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/7 "2019-07-04T07:30:34Z")

</div>

Cool, following the issue. I should try getting `LanguageServer.jl` working in my emacs again.  
I didn’t try SpaceMacs because I wasn’t familiar with the vim keybindings either (although I’ve been using ergoemacs, which has only slightly different movement keys – maybe I should have gone evil/spacemacs instead), and have things configured in a way I like otherwise.  
That mostly means treemacs (which is also integrated into spacemacs), so I’ll probably give it a try sometime.

Having to manually inline everything is worse than having to `@inline` problem functions, like I do to work around [this isssue](https://github.com/JuliaLang/julia/issues/32414).

How far does the “everything” go in “manually inlining everything”? I’d hope you don’t have to inline `getindex` calls, for example.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 4, 2019, 7:54am UTC](https://discourse.julialang.org/t/avoiding-allocations-in-view-s/25983/8 "2019-07-04T07:54:36Z")

</div>

> [@Elrod](#):
>
> How far does the “everything” go in “manually inlining everything”? I’d hope you don’t have to inline `getindex` calls, for example.

Luckily not `getindex`, I have to manually inline all the imported functions from a package which I am extending (which is ~3 layers deep) . The weird/interesting/annoying part is, that it works fine in one case, but not in a very similar one.
