Skip to content

Commit

Permalink
feed quasiquote variables in as arguments (#30)
Browse files Browse the repository at this point in the history
* feed quasiquote variables in as arguments

interpolating these into the AST gives the optimizer too much flexibility
for a benchmark, that can mean the compiler will be able to just constant-fold away the work

* handle case where core isn't and Expr

not sure what it means to benchmark a constant value, but need to handle this case anyways
  • Loading branch information
vtjnash authored and jrevels committed Dec 7, 2016
1 parent faef93d commit cf5215c
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ function collectvars(ex::Expr, vars::Vector{Symbol} = Symbol[])
return vars
end

function quasiquote!(ex::Expr, vars::Vector{Expr})
if ex.head === :($)
lhs = ex.args[1]
rhs = isa(lhs, Symbol) ? gensym(lhs) : gensym()
push!(vars, Expr(:(=), rhs, ex))
return rhs
elseif ex.head !== :quote
for i in 1:length(ex.args)
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = quasiquote!(arg, vars)
end
end
end
return ex
end

macro benchmark(args...)
tmp = gensym()
_, params = prunekwargs(args...)
Expand All @@ -189,7 +206,7 @@ macro benchmarkable(args...)
core, params = prunekwargs(args...)

# extract setup/teardown if present, removing them from the original expression
setup, teardown = :(), :()
setup, teardown = nothing, nothing
delinds = Int[]
for i in eachindex(params)
ex = params[i]
Expand All @@ -203,8 +220,16 @@ macro benchmarkable(args...)
end
deleteat!(params, delinds)

if isa(core, Expr)
quote_vars = Expr[]
core = quasiquote!(core, quote_vars)
if !isempty(quote_vars)
setup = Expr(:block, setup, quote_vars...)
end
end

# extract any variable bindings shared between the core and setup expressions
setup_vars = collectvars(setup)
setup_vars = isa(setup, Expr) ? collectvars(setup) : []
core_vars = isa(core, Expr) ? collectvars(core) : []
out_vars = filter(var -> var in setup_vars, core_vars)

Expand Down Expand Up @@ -234,7 +259,7 @@ function generate_benchmark_definition(eval_module, out_vars, setup_vars,
samplefunc = gensym("sample")
signature = Expr(:call, corefunc, setup_vars...)
if length(out_vars) == 0
returns = :()
#returns = :(return $(Expr(:tuple, setup_vars...)))
invocation = signature
core_body = core
elseif length(out_vars) == 1
Expand Down

0 comments on commit cf5215c

Please sign in to comment.