julia icon indicating copy to clipboard operation
julia copied to clipboard

A macro that forbids allocation – turning allocation into error

Open mgkuhn opened this issue 4 years ago • 4 comments

When implementing real-time applications (robotics control, streaming audio-signal processing, software-defined radio, etc.) in Julia, the unpredictable timing of heap allocation and garbage collection is a significant hazard. Developers who want to use Julia for such applications have to ensure that all required heap memory allocations happen in advance, and not inside time-critical loops. What would help enormously with learning and practicing such allocation-free time-critical programming is a macro that turns allocations into errors. The main purposes of such a macro would be:

  • Developers can get compile-time feedback on whether they have succeeded in implementing algorithms in a way that cannot trigger allocations and garbage collection
  • Users of existing allocation-free methods can see in the source code a compiler-enforced promise that this method will not cause allocations

This feature suggestion was originally made by @rdeits at the end of his wonderful JuliaCon 2018 talk “JuliaRobotics: Making robots walk with Julia” https://youtu.be/dmWQtI3DFFo?t=2263 (also mentioning @tkoolen ) in response to my question of what tools he could envisage to make allocation-free real-time programming easier in Julia: https://youtu.be/dmWQtI3DFFo?t=2160 @JeffBezanson then asked “Would it be enough for the turn-allocation-into-error feature to be lexically scoped”, and after the speaker agreed, promised “OK, you got it!” (followed by audience applause): https://youtu.be/dmWQtI3DFFo?t=2330

What is the status of this feature?

mgkuhn avatar Jan 03 '20 14:01 mgkuhn

It seems like the best and most efficient approach to this would be a static analysis, but I suspect that will run into some difficulties. The first that comes to mind is code that only allocates on error paths. Assuming we can handle that somehow though, this is still quite a bit of work.

JeffBezanson avatar Jan 06 '20 18:01 JeffBezanson

A really easy, quick-and-dirty, but inefficient approach to this is to add a global flag (in gc.c, similar to gc_enable) that every allocation function checks and throws an error if it's set. Then you just set and reset the flag around a block of code. If you're willing to do that as an explicit debugging step (as opposed to it being guaranteed all the time), I would recommend trying to add this.

JeffBezanson avatar Jan 06 '20 18:01 JeffBezanson

+1 this would be really useful for real time applications

HiperMaximus avatar Aug 01 '22 08:08 HiperMaximus

Related news: there is a sampling allocation profiler coming in Julia 1.8: https://live.juliacon.org/talk/YHYSEM

mgkuhn avatar Aug 01 '22 10:08 mgkuhn

I would really like this feature added, since in practice, a lot of performance problems are allocations related.

A really easy, quick-and-dirty, but inefficient approach to this is to add a global flag (in gc.c, similar to gc_enable) that every allocation function checks and throws an error if it's set.

I think that is approach is pretty valid at least for solving time dependent problems:

main() = begin
  # initialization
  [...]  # bunch of array allocations
  
  # time loop, repeated allocations are costly
  enable_allocations(false)
  time = 0.
  while time < ...
     solve_time_dependent_problem(...)
     time += compute_time_step(...)
  end
  enable_allocations(true)

  # post treatment step, allocations are cheap
  [...]
end

I anyone has pointers on where to start, or some implementation recommendations ...

t-bltg avatar Nov 02 '22 10:11 t-bltg

This should be solved by https://github.com/JuliaLang/AllocCheck.jl, right?

carstenbauer avatar Nov 19 '23 08:11 carstenbauer