From b92914ce66f5605f3de9c7414d2c114f56679dde Mon Sep 17 00:00:00 2001 From: yha Date: Tue, 9 Apr 2024 17:43:41 +0300 Subject: [PATCH 1/6] Support for named tuple destructuring --- src/execution.jl | 7 ++++++- test/ExecutionTests.jl | 10 ++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/execution.jl b/src/execution.jl index f3526476..849d64e5 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -346,7 +346,12 @@ function collectvars(ex::Expr, vars::Vector{Symbol}=Symbol[]) if isa(lhs, Symbol) push!(vars, lhs) elseif isa(lhs, Expr) && lhs.head == :tuple - append!(vars, lhs.args) + args = lhs.args + if !isempty(args) && isa(first(args), Expr) + # named tuple destructuring + args = first(args).args + end + append!(vars, args) end elseif (ex.head == :comprehension || ex.head == :generator) arg = ex.args[1] diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 57666815..4fad5852 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -216,10 +216,12 @@ tune!(b) "good") @test_throws UndefVarError local_var @benchmark some_var = "whatever" teardown = (@test_throws UndefVarError some_var) -@benchmark foo, bar = "good", "good" setup = (foo = "bad"; bar = "bad") teardown = (@test foo == - "good" && - bar == - "good") +@benchmark foo, bar = "good", "good" setup = (foo = "bad"; bar = "bad") teardown = @test( + foo == "good" && bar == "good" +) +@benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( + foo == "good" && bar == "good" +) # test variable assignment with `@benchmark(args...)` form @benchmark( From a2817216b687cef372c030fe89ed92e39a055423 Mon Sep 17 00:00:00 2001 From: yha Date: Tue, 9 Apr 2024 18:33:22 +0300 Subject: [PATCH 2/6] Julia version check --- test/ExecutionTests.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 4fad5852..96da4875 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -219,9 +219,11 @@ tune!(b) @benchmark foo, bar = "good", "good" setup = (foo = "bad"; bar = "bad") teardown = @test( foo == "good" && bar == "good" ) -@benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( - foo == "good" && bar == "good" -) +if VERSION >= v"1.6" + @benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( + foo == "good" && bar == "good" + ) +end # test variable assignment with `@benchmark(args...)` form @benchmark( From 83bc880df49526a61d5454d494a1634d48aa020f Mon Sep 17 00:00:00 2001 From: yha Date: Tue, 9 Apr 2024 18:39:02 +0300 Subject: [PATCH 3/6] fix version check --- test/ExecutionTests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 96da4875..15dbda4f 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -219,7 +219,7 @@ tune!(b) @benchmark foo, bar = "good", "good" setup = (foo = "bad"; bar = "bad") teardown = @test( foo == "good" && bar == "good" ) -if VERSION >= v"1.6" +if VERSION >= v"1.7" @benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( foo == "good" && bar == "good" ) From 98bdf2bb7632f215fafc77da3e1586db38d7e107 Mon Sep 17 00:00:00 2001 From: yha Date: Sun, 2 Jun 2024 17:00:30 +0300 Subject: [PATCH 4/6] more tests --- test/ExecutionTests.jl | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 15dbda4f..aa3b0e92 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -216,13 +216,27 @@ tune!(b) "good") @test_throws UndefVarError local_var @benchmark some_var = "whatever" teardown = (@test_throws UndefVarError some_var) -@benchmark foo, bar = "good", "good" setup = (foo = "bad"; bar = "bad") teardown = @test( +@benchmark foo, bar = "good", "good" setup = ((foo, bar) = ("bad", "bad")) teardown = @test( foo == "good" && bar == "good" ) +let # assignment with interpolation + two_good = "good", "good" + two_bad = "bad", "bad" + @benchmark foo, bar = $two_good setup = ((foo, bar) = $two_bad) teardown = @test( + foo == "good" && bar == "good" + ) +end if VERSION >= v"1.7" - @benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( + @benchmark (; foo, bar) = (foo="good", bar="good") setup = ((; foo, bar) = (foo="bad", bar="bad")) teardown = @test( foo == "good" && bar == "good" ) + let # assignment with interpolation + good_nt = (foo="good", bar="good") + bad_nt = (foo="bad", bar="bad") + @benchmark foo, bar = $good_nt setup = ((; foo, bar) = $bad_nt) teardown = @test( + foo == "good" && bar == "good" + ) + end end # test variable assignment with `@benchmark(args...)` form @@ -236,6 +250,11 @@ end setup = (foo = "bad"; bar = "bad"), teardown = (@test foo == "good" && bar == "good") ) +if VERSION >= v"1.7" + @benchmark (; foo, bar) = (foo="good", bar="good") setup = (foo = "bad"; bar = "bad") teardown = @test( + foo == "good" && bar == "good" + ) +end # test kwargs separated by `,` @benchmark( @@ -331,12 +350,13 @@ str = String(take!(io)) # BenchmarkTools.DEFAULT_PARAMETERS.overhead = BenchmarkTools.estimate_overhead() # @test time(minimum(@benchmark nothing)) == 1 -@test [:x, :y, :z, :v, :w] == BenchmarkTools.collectvars( +@test [:x, :y, :z, :v, :w, :r, :s] == BenchmarkTools.collectvars( quote x = 1 + 3 y = 1 + x z = (a = 4; y + a) v, w = 1, 2 + (; r, s) = (r = 1, s = 2, t = 3) [u^2 for u in [1, 2, 3]] end, ) From 1cd34c092cf10651ca330b249a1d4d552063d5b9 Mon Sep 17 00:00:00 2001 From: yha Date: Sun, 2 Jun 2024 17:01:08 +0300 Subject: [PATCH 5/6] More defensive checks --- src/execution.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/execution.jl b/src/execution.jl index 849d64e5..9f808544 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -347,8 +347,12 @@ function collectvars(ex::Expr, vars::Vector{Symbol}=Symbol[]) push!(vars, lhs) elseif isa(lhs, Expr) && lhs.head == :tuple args = lhs.args - if !isempty(args) && isa(first(args), Expr) + if !all(arg -> isa(arg, Symbol), args) # named tuple destructuring + if length(args) > 1 || !isa(first(args), Expr) || first(args).head !== :parameters || + !all(arg -> isa(arg, Symbol), first(args).args) + @error "Unrecognized expression type in benchmark code: $ex" + end args = first(args).args end append!(vars, args) From 0ae7e9c3518898102bc377c205918467ef4bcbdf Mon Sep 17 00:00:00 2001 From: yha Date: Sun, 2 Jun 2024 17:50:09 +0300 Subject: [PATCH 6/6] formatting --- src/execution.jl | 6 ++++-- test/ExecutionTests.jl | 14 +++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/execution.jl b/src/execution.jl index 9f808544..a60492bc 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -349,8 +349,10 @@ function collectvars(ex::Expr, vars::Vector{Symbol}=Symbol[]) args = lhs.args if !all(arg -> isa(arg, Symbol), args) # named tuple destructuring - if length(args) > 1 || !isa(first(args), Expr) || first(args).head !== :parameters || - !all(arg -> isa(arg, Symbol), first(args).args) + if length(args) > 1 || + !isa(first(args), Expr) || + first(args).head !== :parameters || + !all(arg -> isa(arg, Symbol), first(args).args) @error "Unrecognized expression type in benchmark code: $ex" end args = first(args).args diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index aa3b0e92..b72a8cf0 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -224,18 +224,18 @@ let # assignment with interpolation two_bad = "bad", "bad" @benchmark foo, bar = $two_good setup = ((foo, bar) = $two_bad) teardown = @test( foo == "good" && bar == "good" - ) + ) end if VERSION >= v"1.7" - @benchmark (; foo, bar) = (foo="good", bar="good") setup = ((; foo, bar) = (foo="bad", bar="bad")) teardown = @test( - foo == "good" && bar == "good" - ) + @benchmark (; foo, bar) = (foo="good", bar="good") setup = ( + (; foo, bar) = (foo="bad", bar="bad") + ) teardown = @test(foo == "good" && bar == "good") let # assignment with interpolation good_nt = (foo="good", bar="good") - bad_nt = (foo="bad", bar="bad") + bad_nt = (foo="bad", bar="bad") @benchmark foo, bar = $good_nt setup = ((; foo, bar) = $bad_nt) teardown = @test( foo == "good" && bar == "good" - ) + ) end end @@ -356,7 +356,7 @@ str = String(take!(io)) y = 1 + x z = (a = 4; y + a) v, w = 1, 2 - (; r, s) = (r = 1, s = 2, t = 3) + (; r, s) = (r=1, s=2, t=3) [u^2 for u in [1, 2, 3]] end, )