Skip to content

Commit e929b23

Browse files
committed
toggle FunctionWrapper usage
1 parent f1b7ac3 commit e929b23

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/tapedfunction.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Instruction and TapedFunction
22

33
abstract type AbstractInstruction end
4+
const RawTape = Vector{AbstractInstruction}
45

56
struct TypedVar{T}
67
id::Symbol
@@ -32,7 +33,8 @@ mutable struct TapedFunction{F}
3233
func::F # maybe a function, a constructor, or a callable object
3334
arity::Int
3435
ir::Core.CodeInfo
35-
tape::Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}
36+
tape::RawTape
37+
unified_tape::Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}
3638
counter::Int
3739
bindings::Dict{Symbol, Any}
3840
retval::TypedVar
@@ -49,20 +51,29 @@ mutable struct TapedFunction{F}
4951
end
5052

5153
ir = CodeInfoTools.code_inferred(f, args_type...)
52-
tape = Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}()
54+
tape = RawTape()
55+
utape = Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}()
5356
bindings = translate!(tape, ir)
5457

55-
tf = new{F}(f, length(args), ir, tape, 1, bindings, TypedVar{Any}(:none))
58+
tf = new{F}(f, length(args), ir, tape, utape, 1, bindings, TypedVar{Any}(:none))
5659
TRCache[cache_key] = tf # set cache
60+
# unify!(tf)
5761
return tf
5862
end
5963

6064
function TapedFunction(tf::TapedFunction{F}) where {F}
61-
new{F}(tf.func, tf.arity, tf.ir, tf.tape,
65+
new{F}(tf.func, tf.arity, tf.ir, tf.tape, tf.unified_tape,
6266
tf.counter, tf.bindings, TypedVar{Any}(:none))
6367
end
6468
end
6569

70+
function unify!(tf::TapedFunction)
71+
length(tf.tape) == length(tf.unified_tape) && return
72+
for ins in tf.tape
73+
push!(tf.unified_tape, FunctionWrapper{Nothing, Tuple{TapedFunction}}(ins))
74+
end
75+
end
76+
6677
const TRCache = LRU{Tuple, TapedFunction}(maxsize=10)
6778

6879
val(x) = x
@@ -82,8 +93,10 @@ function (tf::TapedFunction)(args...; callback=nothing)
8293
end
8394

8495
# run the raw tape
96+
tape = length(tf.tape) == length(tf.unified_tape) ?
97+
tf.unified_tape : tf.tape
8598
while true
86-
ins = tf.tape[tf.counter]
99+
ins = tape[tf.counter]
87100
ins(tf)
88101
callback !== nothing && callback()
89102
tf.retval.id !== :none && break
@@ -199,13 +212,13 @@ function bind_var!(var::Symbol, bindings::Dict{Symbol, Any}, ::Type{T}) where T
199212
TypedVar{T}(var)
200213
end
201214

202-
function translate!(tape::Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}, ir::Core.CodeInfo)
215+
function translate!(tape::RawTape, ir::Core.CodeInfo)
203216
bindings = Dict{Symbol, Any}()
204217

205218
for (idx, line) in enumerate(ir.code)
206219
isa(line, Core.Const) && (line = line.val) # unbox Core.Const
207220
ins = translate!!(Core.SSAValue(idx), line, bindings, ir)
208-
push!(tape, FunctionWrapper{Nothing, Tuple{TapedFunction}}(ins))
221+
push!(tape, ins)
209222
end
210223
return bindings
211224
end

0 commit comments

Comments
 (0)