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

Unexpected broadcast materialization #46081

Open
chrstphrbrns opened this issue Jul 17, 2022 · 1 comment
Open

Unexpected broadcast materialization #46081

chrstphrbrns opened this issue Jul 17, 2022 · 1 comment
Labels
domain:broadcast Applying a function over a collection kind:breaking This change will break code

Comments

@chrstphrbrns
Copy link
Contributor

Does f need to materialize the broadcast here?

julia> VERSION
v"1.9.0-DEV.990"

julia> f(a) = length(sin.(a))
f (generic function with 1 method)

julia> g(a) = length(Broadcast.Broadcasted(sin, (a,)))
g (generic function with 1 method)

julia> a = 1:10000000
1:10000000

julia> @btime f(a)
  204.279 ms (3 allocations: 76.29 MiB)
10000000

julia> @btime g(a)
  14.118 ns (1 allocation: 16 bytes)
10000000

julia> @code_lowered f(a)
CodeInfo(
1%1 = Base.broadcasted(Main.sin, a)
│   %2 = Base.materialize(%1)
│   %3 = Main.length(%2)
└──      return %3
)
@antoine-levitt
Copy link
Contributor

antoine-levitt commented Jul 18, 2022

Dot expressions will always end up materializing at the boundary (because it has to work with arbitrary functions that expect Arrays). Same thing with (the more useful) sum, etc. Changing that is breaking, but would be a cool 2.0 addition, I think there are already open issues for that (search lazy broadcast). Or I guess there could be a mechanism by which the effective return type of a broadcast could change depending on the methods of the caller, but I'd imagine that would cause more trouble than it's worth...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:broadcast Applying a function over a collection kind:breaking This change will break code
Projects
None yet
Development

No branches or pull requests

3 participants