Skip to content

Commit 02c51a3

Browse files
authored
also create a "junkyard" for the Frames to avoid having to recreate them over and over (#308)
1 parent d4596e3 commit 02c51a3

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
deps/build.log
55
docs/build/
66
test/results.md
7+
Manifest.toml

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Documenter, JuliaInterpreter, Test, CodeTracking
33
DocMeta.setdocmeta!(JuliaInterpreter, :DocTestSetup, :(
44
begin
55
using JuliaInterpreter
6-
empty!(JuliaInterpreter.junk)
6+
JuliaInterpreter.clear_caches()
77
JuliaInterpreter.remove()
88
end); recursive=true)
99

docs/src/internals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Sometimes you might have a whole sequence of expressions you want to run.
146146
In such cases, your first thought should be `prepare_thunk`.
147147
Here's a demonstration:
148148

149-
```jldoctest; setup=(using JuliaInterpreter; empty!(JuliaInterpreter.junk))
149+
```jldoctest; setup=(using JuliaInterpreter; JuliaInterpreter.clear_caches())
150150
using Test
151151
152152
ex = quote

src/construct.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ rather than recursed into via the interpreter.
2626
"""
2727
const compiled_modules = Set{Module}()
2828

29-
const junk = FrameData[] # to allow re-use of allocated memory (this is otherwise a bottleneck)
30-
const debug_recycle = Base.RefValue(false)
31-
@noinline _check_frame_not_in_junk(frame) = @assert frame.framedata junk
29+
const junk_framedata = FrameData[] # to allow re-use of allocated memory (this is otherwise a bottleneck)
30+
const junk_frames = Frame[]
31+
debug_recycle() = false
32+
@noinline function _check_frame_not_in_junk(frame)
33+
@assert frame.framedata junk_framedata
34+
@assert frame junk_frames
35+
end
36+
3237
@inline function recycle(frame)
33-
debug_recycle[] && _check_frame_not_in_junk(frame)
34-
push!(junk, frame.framedata)
38+
debug_recycle() && _check_frame_not_in_junk(frame)
39+
push!(junk_framedata, frame.framedata)
40+
push!(junk_frames, frame)
3541
end
3642

3743
function return_from(frame::Frame)
@@ -42,9 +48,10 @@ function return_from(frame::Frame)
4248
end
4349

4450
function clear_caches()
45-
empty!(junk)
51+
empty!(junk_framedata)
4652
empty!(framedict)
4753
empty!(genframedict)
54+
empty!(junk_frames)
4855
for bp in breakpoints()
4956
empty!(bp.instances)
5057
end
@@ -269,8 +276,8 @@ function prepare_framedata(framecode, argvals::Vector{Any}, lenv::SimpleVector=e
269276
slotnames = src.slotnames::SlotNamesType
270277
ssavt = src.ssavaluetypes
271278
ng, ns = isa(ssavt, Int) ? ssavt : length(ssavt::Vector{Any}), length(src.slotflags)
272-
if length(junk) > 0
273-
olddata = pop!(junk)
279+
if length(junk_framedata) > 0
280+
olddata = pop!(junk_framedata)
274281
locals, ssavalues, sparams = olddata.locals, olddata.ssavalues, olddata.sparams
275282
exception_frames, last_reference = olddata.exception_frames, olddata.last_reference
276283
last_exception = olddata.last_exception

src/types.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,20 @@ mutable struct Frame
184184
caller::Union{Frame,Nothing}
185185
callee::Union{Frame,Nothing}
186186
end
187-
Frame(framecode, framedata, pc=1, caller=nothing) = Frame(framecode, framedata, pc, 1, caller, nothing)
187+
function Frame(framecode, framedata, pc=1, caller=nothing)
188+
if length(junk_frames) > 0
189+
frame = pop!(junk_frames)
190+
frame.framecode = framecode
191+
frame.framedata = framedata
192+
frame.pc = pc
193+
frame.assignment_counter = 1
194+
frame.caller = caller
195+
frame.callee = nothing
196+
return frame
197+
else
198+
return Frame(framecode, framedata, pc, 1, caller, nothing)
199+
end
200+
end
188201

189202
caller(frame) = frame.caller
190203
callee(frame) = frame.callee

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if !isdefined(@__MODULE__, :read_and_parse)
77
include("utils.jl")
88
end
99

10-
JuliaInterpreter.debug_recycle[] = true
10+
Core.eval(JuliaInterpreter, :(debug_recycle() = true))
1111

1212
@testset "Main tests" begin
1313
include("interpret.jl")

0 commit comments

Comments
 (0)