BenchmarkTools.jl icon indicating copy to clipboard operation
BenchmarkTools.jl copied to clipboard

$x should become $(Ref(x))[]?

Open stevengj opened this issue 4 years ago • 8 comments

It's a bit annoying when you have to use this convoluted Ref trick to prevent the compiler from constant-folding the whole calculation (ala #130):

julia> @btime exp(1);
  0.052 ns (0 allocations: 0 bytes)

julia> @btime exp($(Ref(1))[]);
  13.785 ns (0 allocations: 0 bytes)

It would be nicer if we could just use @btime exp($1) and the $ did the trick automatically.

(Another case just came up on discourse. cc @stillyslalom)

stevengj avatar Apr 14 '21 15:04 stevengj

+1 for a more convenient syntax, but I'm not sure whether $x is the right syntax. In my understanding of interpolation, x = 42; :(foo($x)) and foo(42) should be one and the same thing.

ettersi avatar Nov 13 '21 14:11 ettersi

Individual macros can do whatever they want with interpolation syntax; it doesn't have to be limited to expression interpolation.

stevengj avatar Nov 15 '21 16:11 stevengj

Of course you can write a macro such that @extremely_large_value_of_2(2+2) == 5, but that probably wouldn't be very useful unless you are running a T-shirt company.

A concrete example where (I believe) the proposed syntax would be problematic is x = 2.0; n = 2; @btime $(Ref(x))[]^$n. In this case, you might legitimately want n to be "hardwired" into the benchmark expression but not x.

ettersi avatar Nov 16 '21 02:11 ettersi