Skip to content

Commit

Permalink
Replace eval by generated function (#119)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rikhuijzer authored Feb 18, 2022
1 parent 8db6c3a commit 8aaea77
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
license = "MIT"
desc = "Tape based task copying in Turing"
repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.6.8"
version = "0.6.9"

[deps]
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
Expand Down
17 changes: 15 additions & 2 deletions src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,25 @@ function (instr::Instruction{F})() where F
end
end

"""
__new__(T, args)
Return a new instance of `T` with `args` even when there is no inner constructor for these args.
Source: https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/5
"""
@generated function __new__(T, args)
return Expr(:splatnew, :T, :args)
end

function _new end
function (instr::Instruction{typeof(_new)})()
# catch run-time exceptions / errors.
try
expr = Expr(:new, map(val, instr.input)...)
output = eval(expr)
input = map(val, instr.input)
T = input[1]
args = input[2:end]
output = __new__(T, args)

instr.output.val = output
instr.tape.counter += 1
catch e
Expand Down

2 comments on commit 8aaea77

@rikhuijzer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/54929

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.9 -m "<description of version>" 8aaea77a35681ac245ef1c6c8864e1572704b974
git push origin v0.6.9

Please sign in to comment.