JuliaLang / AllocCheck.jl

AllocCheck
Other
215 stars 8 forks source link

Repeated allocation instances #7

Closed baggepinnen closed 9 months ago

baggepinnen commented 11 months ago

I have the following little function I'm using to experiment with

using AllocCheck

send_control(u) = sum(abs2, u) # Dummy control function
calc_control() = 1.0
get_measurement() = [1.0]

function example_loop(data, t_start, y_start, y_old, ydf, Ts, N, r)
    for i = 1:N
        t = time() - t_start
        y = get_measurement()[] - y_start # Subtract initial position for a smoother experience
        yd = (y - y_old) / Ts
        ydf = 0.9*ydf + 0.1*yd
        # r = 45sin(2π*freq*t)
        u = calc_control()
        send_control([u])
        log = [t, y, ydf, u, r(t)]
        data[:, i] .= log
        y_old = y
    end
end

## Generate some example input data
r = t->(2 + 2floor(t/4)^2)
N = 10
data = Matrix{Float64}(undef, 5, N) 
t_start = 1.0
y_start = 0.0
y_old = 0.0
ydf = 0.0
Ts = 1.0

typetuple = typeof.((data, t_start, y_start, y_old, ydf, Ts, N, r))

AllocCheck.check_allocs(example_loop, typetuple, ignore_throw=true)

On Julia v1.10 beta3, I get several repeated AllocInstances for each actual allocation. With ignore_throw = true, I get 18 alloc instances, where at least 4 different sources of allocations are repeated 3 times each (12 instances for 4 real allocations)

Using BenchmarkTools, it appears as if the number of allocations actually happens in each loop iteration is 3 (N=10 loop iterations)

julia> @btime example_loop($data, $t_start, $y_start, $y_old, $ydf, $Ts, $N, $r)
  907.344 ns (30 allocations: 2.19 KiB)

This matches with what I would expect, get_measurement, [u] and log = [t, y, ydf, u, r(t)]

baggepinnen commented 11 months ago

Hmm, it seems me workflow using Revise in the terminal was not working as I expected, please ignore this issue for now until I've figured out a better strategy. Sorry for the noise :/


EDIT: I updated the original post