Skip to content

Instantly share code, notes, and snippets.

@Sixzero
Last active December 28, 2020 23:42
Show Gist options
  • Save Sixzero/149e391b41879ac10d0952d6e0e97fab to your computer and use it in GitHub Desktop.
Save Sixzero/149e391b41879ac10d0952d6e0e97fab to your computer and use it in GitHub Desktop.
opaque_closure_test
#%%
using BenchmarkTools
BenchmarkTools.DEFAULT_PARAMETERS.seconds = 2.50
using Base: @opaque
add_opaq = @opaque (a::Vector, b::Vector) -> a .+ b
@noinline add(a::Vector, b::Vector) = a .+ b
@noinline add!(c::Vector, a::Vector, b::Vector) = c .= a .+ b
@inline add_inline(a::Vector, b::Vector) = a .+ b
#%%
test() = begin
a = randn(1024)
b = randn(1024)
c = randn(1024)
@btime $c .= $add($a, $b)
@btime $c .= $add!($c, $a, $b)
@btime $c .= $add_inline($a, $b)
@btime $c .= $add_opaq($a, $b)
end
test()
;
# 816.818 ns (1 allocation: 8.12 KiB)
# 126.728 ns (0 allocations: 0 bytes)
# 857.111 ns (1 allocation: 8.12 KiB)
# 4.096 μs (11 allocations: 8.48 KiB)
#%%
using Base: @opaque
test2 = @opaque () -> begin
a = randn(1024)
b = randn(1024)
c = randn(1024)
@time c .= add(a, b)
@time c .= add!(c, a, b)
@time c .= add_inline(a, b)
@time c .= add_opaq(a, b)
end
test2()
;
# 0.000037 seconds (9 allocations: 8.344 KiB)
# 0.000037 seconds (8 allocations: 224 bytes)
# 0.000043 seconds (9 allocations: 8.344 KiB)
# 0.000074 seconds (16 allocations: 8.672 KiB)
@ericphanson
Copy link

maybe you need @btime $c .= $add_opaq($a, $b) since add_opaq is a non-constant global?

@Sixzero
Copy link
Author

Sixzero commented Dec 28, 2020

You are right, it is worth a try, so:

@btime $c .= $add_opaq($a, $b)
#  4.064 μs (11 allocations: 8.53 KiB)
@btime $c .= add_opaq($a, $b)
# 4.096 μs (10 allocations: 8.48 KiB)

Interestingly it increased allocations by 1.

One of my friend told me, that in C++ we have "move semantics" and "return value optimization" developed to prevent these allocations. What we need to know why julia can't control the LLVM to apply the optimizations in such situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment