# Disabling allocations

**URL:** <https://discourse.julialang.org/t/disabling-allocations/51028>\
**Category:** Performance\
**Created:** [December 1, 2020, 5:06am UTC](https://discourse.julialang.org/t/disabling-allocations/51028 "2020-12-01T05:06:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 1, 2020, 5:06am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/1 "2020-12-01T05:06:54Z")

</div>

Is there a way of disabling allocations altogether within the body of a function? I optimized my code to avoid all allocations, but they keep creeping back in with changes and are annoying to locate. I would much rather have a compile-time error.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [December 1, 2020, 6:12am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/2 "2020-12-01T06:12:01Z")

</div>

I don’t think there is a way. I would also like to add that there is nothing inherently wrong with allocations. At some point, you need data in memory for the CPU to do its thing. Are the allocations affecting your runtime/performance? If your code is type stable (check using `@code_warntype`), I wouldn’t worry about small allocations.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 1, 2020, 6:41am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/3 "2020-12-01T06:41:29Z")

</div>

There are sometimes good reasons for avoiding allocations in specific parts of a program, usually for performance purposes.

You can measure allocations with `@allocated` and add a corresponding unit test.

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 1, 2020, 10:59pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/4 "2020-12-01T22:59:18Z")

</div>

I’m building a raytracer and allocations are indicative of a mistake (dynamic dispatch, accidental allocation, etc.) which degrades performance. Obviously the program as a whole will allocate memory but the actual render loop really should not. From my experience so far its fairly easy to screw up and accidentally cause things like dynamic dispatch (in some cases the reason can be pretty insidious, for instance a type annotation which is not specific enough). `@code_warntype` works great but isn’t recursive. This makes locating the source of an allocation very painful.

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 1, 2020, 11:00pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/5 "2020-12-01T23:00:21Z")

</div>

`@allocated` works great but actually locating the source of the allocation can get pretty tricky.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 1, 2020, 11:33pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/6 "2020-12-01T23:33:13Z")

</div>

Have you tried this:?[Profiling · The Julia Language](https://docs.julialang.org/en/v1/manual/profile/#Memory-allocation-analysis)

Also I use `@allocated` like this:

```julia
a = @allocated begin
    Block to test
end; if a > 0 println(a) end

```

It is not hard to find sources of allocations moving the test around.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [December 2, 2020, 8:03am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/7 "2020-12-02T08:03:06Z")

</div>

For Array allocations, or ones arising from mutable structures you know about, you can write a custom [Cassette.jl](https://github.com/jrevels/Cassette.jl) pass that throws an error if you try to allocate an Array or the known type. E.g.

```julia
using Cassette
Cassette.@context AlloCatcher
function Cassette.overdub(::AlloCatcher, ::Type{<:Array}, ::UndefInitializer, dims...)
    error("Tried to allocate an array")
end

```

Then if you had e.g.

```julia
bad_add!(dst, a, b) = dst .= a + b # forgot to broadcast a .+ b
good_add!(dst, a, b) = dst .= a .+ b 

dst, a, b = rand(5), rand(5), rand(5)

# runs like normal:
Cassette.overdub(AlloCatcher(), good_add!, dst, a, b) 

# errors, and you get the stack trace to the allocation:
Cassette.overdub(AlloCatcher(), bad_add!, dst, a, b) 

```

FWIW this is exactly what [AutoPreallocation.jl](https://github.com/oxinabox/AutoPreallocation.jl) does except instead of erroring, it saves the allocated array for you so you can reuse it the next time you call the same function.

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 3, 2020, 3:22pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/8 "2020-12-03T15:22:16Z")

</div>

Yes, this is what I do currently. I’m looking for a way of pinpointing the cause of the allocation without manually moving the `@allocated` test into every code branch.

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 3, 2020, 3:28pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/9 "2020-12-03T15:28:13Z")

</div>

This is really cool. What does `UndefInitializer` do in this example?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 3, 2020, 3:48pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/10 "2020-12-03T15:48:19Z")

</div>

Perhaps the manual is not clear. Check this example:

This is the code (file name here: test.jl):

```julia
struct A
  x
end

function test(n,x)
  y = Vector{A}(undef,n)
  for i in 1:n
    y[i] = A(i*x)
  end
  y
end

```

Run julia with:

```julia
julia --track-allocation=user

```

Within Julia, do:

```julia
julia> using Profile

julia> include("./test.jl")
test (generic function with 1 method)

julia> test(10,rand()); # gets compiled

julia> Profile.clear_malloc_data() # clear allocations

julia> test(10,rand());

```

Exit Julia, this will generate a file `test.jl.XXX.mem` (extension `.mem`), which, in this case, contains:

```julia
        -
        - struct A
        - x
        - end
        -
        - function test(n,x)
      160 y = Vector{A}(undef,n)
        0 for i in 1:n
      160 y[i] = A(i*x)
        - end
        0 y
        - end

```

Where the lines with non-zero numbers are the lines where allocations occur.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [December 3, 2020, 10:00pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/11 "2020-12-03T22:00:07Z")

</div>

Basically every array allocation at some point necessarily hits a constructor like `Array{T}(undef, M, N)` and that `overdub` rule catches that and rewrites it into an error.

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 8, 2020, 3:55am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/12 "2020-12-08T03:55:22Z")

</div>

Thanks, I didn’t understand that `Array` was always called with `undef` even for literals. I’ve looked a bit more into Cassette, it’s some pretty cool stuff!

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 8, 2020, 4:05am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/13 "2020-12-08T04:05:31Z")

</div>

> [@lmiq](#):
>
> julia --track-allocation=user

Thanks, this works great. Although I still wish I could just disable the darn thing!

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 8, 2020, 4:12am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/14 "2020-12-08T04:12:24Z")

</div>

Note that arrays are not the only possible source of allocations though, there’s also dynamic dispatch, boxing and mutable structs and such.

In theory, the new version 1.6 compiler stuff could be used to catch all allocations / dynamism and throw an error though.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [December 8, 2020, 4:13am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/15 "2020-12-08T04:13:36Z")

</div>

> [@Mason](#):
>
> In theory, the new version 1.6 compiler stuff could be used to catch all allocations / dynamism and throw an error though.

Yes please!

---

<div class="post-metadata">

**Author:** ![Ginter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginter/32/9829_2.png) [@Ginter](https://discourse.julialang.org/u/Ginter)\
**Post date:** [December 8, 2020, 11:27pm UTC](https://discourse.julialang.org/t/disabling-allocations/51028/16 "2020-12-08T23:27:58Z")

</div>

Yes, accidental dynamic dispatch is my main source of problems. I’d love to hear about the 1.6 compiler changes in question; if you could give me some pointers I would like to try and implement this feature!

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [December 9, 2020, 1:42am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/17 "2020-12-09T01:42:31Z")

</div>

I also tried to rewrite a raytracer serval months ago, [tinsel](https://github.com/mmacklin/tinsel). Currently, I can render the coffee cup example without scene bvh acceleration.  
I use `julia --track-allocation=user` to find unnecessary memory allocation and the Cthulhu.jl to find type unstable. Unfortunately, unless single dispatch/typed function pointer is possible in Julia, some allocation caused by dynamic dispatch are impossible to elimate.  
Another problem is the stack-allocated value. In a raytracer, it’s common to use a stack-allocated temporary value(for example, hit information) and pass its reference to other functions for further process. It’s impossible to do this in Julia, since the value might escape, which leads to allocation. Currently I have to preallocate all the intermediate values and passing them to the raytracers, which is quite fallible. Really hope there’s a way to force the value allocating on stacks.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [December 9, 2020, 8:20am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/18 "2020-12-09T08:20:42Z")

</div>

> [@anon56330260](#):
>
> I also tried to rewrite a raytracer serval months ago, [tinsel](https://github.com/mmacklin/tinsel). Currently, I can render the coffee cup example without scene bvh acceleration.

That’s an impressive piece of work (I never got that far with my path tracer). But… it seems to be written in C++ and not Julia? 🤨

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [December 9, 2020, 8:51am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/19 "2020-12-09T08:51:15Z")

</div>

> [@paulmelis](#):
>
> But… it seems to be written in C++ and not Julia? 🤨

I believe that tinsel is the original whereas @anon56330260 was referring to a port of the former.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [December 9, 2020, 8:57am UTC](https://discourse.julialang.org/t/disabling-allocations/51028/20 "2020-12-09T08:57:46Z")

</div>

Doh, I missed he said “rewrite” and not “write”, sorry! 😄

[Next page](https://discourse.julialang.org/t/disabling-allocations/51028.md?page=2)
