Skip to content

Commit

Permalink
Use more Tuples (#22)
Browse files Browse the repository at this point in the history
* Use more Tuples

* Performance tests
  • Loading branch information
jw3126 authored May 8, 2018
1 parent 4dfb99e commit 07d7528
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BenchmarkTools
37 changes: 37 additions & 0 deletions test/perf.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ else
end

include("checks.jl")
include("perf.jl")

0 comments on commit 07d7528

Please sign in to comment.