Skip to content

Commit 8aaea77

Browse files
authored
Replace eval by generated function (#119)
* Replace `eval` by generated function Benchmarked on Turing.jl/test/stdlib/RandomMeasures.jl ``` julia> @benchmark run_chain() # eval BenchmarkTools.Trial: 1 sample with 1 evaluation. Single result which took 15.231 s (1.52% GC) to evaluate, with a memory estimate of 679.26 MiB, over 9452913 allocations. julia> @benchmark run_chain() # splatnew BenchmarkTools.Trial: 1 sample with 1 evaluation. Single result which took 14.665 s (1.55% GC) to evaluate, with a memory estimate of 655.23 MiB, over 8972863 allocations. julia> @benchmark run_chain() # eval BenchmarkTools.Trial: 1 sample with 1 evaluation. Single result which took 16.465 s (1.41% GC) to evaluate, with a memory estimate of 679.21 MiB, over 9452903 allocations. julia> @benchmark run_chain() # splatnew BenchmarkTools.Trial: 1 sample with 1 evaluation. Single result which took 15.409 s (1.50% GC) to evaluate, with a memory estimate of 655.34 MiB, over 8972879 allocations. ``` So about 3.5% runtime reduction. * Remove type annotation from generated function
1 parent 8db6c3a commit 8aaea77

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
33
license = "MIT"
44
desc = "Tape based task copying in Turing"
55
repo = "https://github.com/TuringLang/Libtask.jl.git"
6-
version = "0.6.8"
6+
version = "0.6.9"
77

88
[deps]
99
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"

src/tapedfunction.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,25 @@ function (instr::Instruction{F})() where F
173173
end
174174
end
175175

176+
"""
177+
__new__(T, args)
178+
179+
Return a new instance of `T` with `args` even when there is no inner constructor for these args.
180+
Source: https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/5
181+
"""
182+
@generated function __new__(T, args)
183+
return Expr(:splatnew, :T, :args)
184+
end
185+
176186
function _new end
177187
function (instr::Instruction{typeof(_new)})()
178188
# catch run-time exceptions / errors.
179189
try
180-
expr = Expr(:new, map(val, instr.input)...)
181-
output = eval(expr)
190+
input = map(val, instr.input)
191+
T = input[1]
192+
args = input[2:end]
193+
output = __new__(T, args)
194+
182195
instr.output.val = output
183196
instr.tape.counter += 1
184197
catch e

0 commit comments

Comments
 (0)