Skip to content

Commit 9bafecf

Browse files
committed
use vector to store arg indices
1 parent 03d971b commit 9bafecf

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/tapedfunction.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mutable struct TapedFunction{F, TapeType}
6868
tape::TapeType
6969
counter::Int
7070
bindings::Bindings
71-
slots::Dict{Int, Int} # slot indices in bindings
71+
arg_indices::Vector{Int} # arg indices in bindings
7272
retval::Int # 0 indicates the function has not returned
7373

7474
function TapedFunction{F, T}(f::F, args...; cache=false) where {F, T}
@@ -94,7 +94,7 @@ mutable struct TapedFunction{F, TapeType}
9494

9595
function TapedFunction{F, T0}(tf::TapedFunction{F, T1}) where {F, T0, T1}
9696
new{F, T0}(tf.func, tf.arity, tf.ir, tf.tape,
97-
tf.counter, tf.bindings, tf.slots, 0)
97+
tf.counter, tf.bindings, tf.arg_indices, 0)
9898
end
9999

100100
TapedFunction(tf::TapedFunction{F, T}) where {F, T} = TapedFunction{F, T}(tf)
@@ -155,10 +155,10 @@ function (tf::TapedFunction)(args...; callback=nothing, continuation=false)
155155
# set args
156156
if tf.counter <= 1
157157
# The first slot in `bindings` is assumed to be `tf.func`.
158-
haskey(tf.slots, 1) && _update_var!(tf, tf.slots[1], tf.func)
159-
for i in 1:length(args) # the subsequent slots are arguments
158+
tf.arg_indices[1] > 0 && _update_var!(tf, tf.arg_indices[1], tf.func)
159+
for i in 1:length(args) # the subsequent arg_indices are arguments
160160
slot = i + 1
161-
haskey(tf.slots, slot) && _update_var!(tf, tf.slots[slot], args[i])
161+
tf.arg_indices[slot] > 0 && _update_var!(tf, tf.arg_indices[slot], args[i])
162162
end
163163
end
164164

@@ -330,7 +330,11 @@ function translate!(tape::RawTape, ir::Core.CodeInfo)
330330
for (k, v) in tbind.book
331331
isa(k, Core.SlotNumber) && (slots[k.id] = v)
332332
end
333-
return (bindings, slots, tape)
333+
arg_indices = fill(0, maximum(keys(slots)))
334+
for (k, v) in slots
335+
arg_indices[k] = v
336+
end
337+
return (bindings, arg_indices, tape)
334338
end
335339

336340
function _const_instruction(var::IRVar, v, tbind::TempBindings, ir)

src/tapedtask.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function Base.copy(t::TapedTask; args=())
174174
# the task is running, we find the real args from the copied bindings
175175
map(1:length(t.args)) do i
176176
s = i + 1
177-
haskey(tf.slots, s) ? tf.bindings[tf.slots[s]] : t.args[i]
177+
tf.arg_indices[s] > 0 ? tf.bindings[tf.arg_indices[s]] : t.args[i]
178178
end
179179
else
180180
# the task is not started yet, but no args is given

0 commit comments

Comments
 (0)