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

Wrapping an Array in a struct allocates #34871

Closed
oxinabox opened this issue Feb 25, 2020 · 6 comments
Closed

Wrapping an Array in a struct allocates #34871

oxinabox opened this issue Feb 25, 2020 · 6 comments

Comments

@oxinabox
Copy link
Contributor

oxinabox commented Feb 25, 2020

Consider:

using BenchmarkTools

struct Foo{T}
    x::T
end

const m = [1,2,3]
const k = 1:3

@btime Foo(k);
@btime Foo(m);

Output:

julia> @btime Foo(k);

  0.029 ns (0 allocations: 0 bytes)

julia> @btime Foo(m);
  4.952 ns (1 allocation: 16 bytes)

It doesn't seem like Foo(m) should allocate when Foo(k) can avoid it.


@oxinabox
Copy link
Contributor Author

But @allocated does say 0

julia> @allocated Foo(k)
0

julia> @allocated Foo(m)
0

@KristofferC
Copy link
Member

Just seems like bad benchmarking?

julia> g(m) = (Foo(m); nothing)
g (generic function with 1 method)

julia> @btime g($m)
  0.035 ns (0 allocations: 0 bytes)

If you return the object it needs to be allocated?

@oxinabox
Copy link
Contributor Author

oxinabox commented Feb 25, 2020

Ah, I see.

But still why the difference betweek Foo(k) and Foo(m) ?

@KristofferC
Copy link
Member

Because one is isbits and not the other?

@vtjnash
Copy link
Member

vtjnash commented Feb 25, 2020

Will be closed by #34126

@JeffBezanson
Copy link
Member

Duplicate of #14955

@JeffBezanson JeffBezanson marked this as a duplicate of #14955 Feb 25, 2020
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

4 participants