From 07d7528d0440b23990fcab87f201207021eb9740 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Tue, 8 May 2018 22:45:32 +0200 Subject: [PATCH] Use more Tuples (#22) * Use more Tuples * Performance tests --- src/checks.jl | 16 +++++++--------- test/REQUIRE | 1 + test/perf.jl | 37 +++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 test/REQUIRE create mode 100644 test/perf.jl diff --git a/src/checks.jl b/src/checks.jl index faf72ab..7dd7be5 100644 --- a/src/checks.jl +++ b/src/checks.jl @@ -23,19 +23,19 @@ struct CallErrorInfo <: AbstractErrorInfo checkflavor::AbstractCheckFlavor argument_expressions::Vector argument_values::Vector - options::Vector + options::Tuple end struct ComparisonErrorInfo <: AbstractErrorInfo code checkflavor::AbstractCheckFlavor argument_expressions::Vector argument_values::Vector - options::Vector + options::Tuple end struct FallbackErrorInfo <: AbstractErrorInfo code checkflavor::AbstractCheckFlavor - options::Vector + options::Tuple end """ @@ -101,7 +101,7 @@ function check(c, ::FallbackFlavor) info = Expr(:call, :FallbackErrorInfo, QuoteNode(c.code), c.checkflavor, - Expr(:vect, esc.(c.options)...)) + Expr(:tuple, esc.(c.options)...)) condition = esc(c.code) expr_error_block(info, condition) @@ -118,7 +118,7 @@ function check(c, ::CallFlavor) c.checkflavor, QuoteNode(c.code.args), Expr(:vect, variables...), - Expr(:vect, esc.(c.options)...)) + Expr(:tuple, esc.(c.options)...)) expr_error_block(info, condition, assignments...) end @@ -145,7 +145,7 @@ function check(c::Checker, ::ComparisonFlavor) c.checkflavor, QuoteNode([lhs, rhs]), Expr(:vect, vlhs, vrhs), - Expr(:vect, esc.(c.options)...)) + Expr(:tuple, esc.(c.options)...)) reti = expr_error_block(info, condition, assignment) append!(ret, reti.args) @@ -159,9 +159,7 @@ function expr_error_block(info, condition, preamble...) if $condition nothing else - info = $info - err = build_error(info) - throw(err) + throw(build_error($info)) end end end diff --git a/test/REQUIRE b/test/REQUIRE new file mode 100644 index 0000000..bf369b9 --- /dev/null +++ b/test/REQUIRE @@ -0,0 +1 @@ +BenchmarkTools diff --git a/test/perf.jl b/test/perf.jl new file mode 100644 index 0000000..d1249db --- /dev/null +++ b/test/perf.jl @@ -0,0 +1,37 @@ +# compare performance with plain assertion +using BenchmarkTools +using ArgCheck + +truthy(x) = true + +function fallback_argcheck(x) + @argcheck x +end +function comparison_argcheck(x) + @argcheck x == 42 +end +function call_argcheck(x) + @argcheck truthy(x) +end +function fallback_assert(x) + @assert x +end +function comparison_assert(x) + @assert x == 42 +end +function call_assert(x) + @assert truthy(x) +end + +benchmarks =[ + (fallback_assert, fallback_argcheck, true), + (call_assert, call_argcheck, 42), + (comparison_assert, comparison_argcheck, 42), + ] + +for (f_argcheck, f_assert, arg) in benchmarks + println(f_argcheck) + @btime ($f_argcheck)($arg) + println(f_assert) + @btime ($f_assert)($arg) +end diff --git a/test/runtests.jl b/test/runtests.jl index b0eb818..5813487 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,3 +6,4 @@ else end include("checks.jl") +include("perf.jl")