Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent allocations when using views #10

Open
rickbeeloo opened this issue Aug 20, 2022 · 0 comments
Open

Prevent allocations when using views #10

rickbeeloo opened this issue Aug 20, 2022 · 0 comments

Comments

@rickbeeloo
Copy link

Hey!

First of all, cool packages!, I really did not want to go through the hassle of defining all iterator syntax... and this seems the prefect solution for that hehe

I'm working on a 0 alloc file reader and to make this work I use views of the lines, however when benchmarking I saw my code results in allocations when used with Continuables. I made this minimal example on array views (instead of char views):

using Continuables
using BenchmarkTools

view_test(n::Integer, arr::Vector{Int}) = @cont begin
  for i in 1:length(arr)
      v = view(arr, i:n)
      cont(v)
  end
end

function t1(arr::Vector{Int64})
    tot = 0
    for i in 1:length(arr)
        v = view(arr, i:length(arr))
        tot += length(v)
    end
    return tot
end

function t2(arr::Vector{Int64})
    tot = 0
    foreach(view_test(length(arr), arr)) do x
        tot += length(x)
    end
    return tot
end

test_arr = zeros(Int64, 1_000)
@btime t1($test_arr)
@btime t2($test_arr)

Which gives:

  1.780 μs (0 allocations: 0 bytes)
  26.453 μs (1490 allocations: 23.28 KiB)

P.s. I saw the doc about Ref(). While I might be able to use Ref(tot) here, in my file reader I basically "yield" lines based on numerous condition rather than simply adding up something (see my Julia discourse implementation of the reader).

Is there any way to prevent all these allocs? That would be awesome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant