Skip to content

Commit

Permalink
Performance bump for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLauwens committed Jan 4, 2021
1 parent 52c4f68 commit b2d2db0
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
desc = "C# sharp style generators a.k.a. semi-coroutines for Julia."
authors = ["Ben Lauwens <ben.lauwens@gmail.com>"]
repo = "https://github.com/BenLauwens/ResumableFunctions.jl.git"
version = "0.5.2"
version = "0.6.0"

[deps]
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Expand Down
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,30 @@ end
```

## Benchmarks
The following block is the result of running `julia --project=. benchmark/benchmarks.jl` on a computer with the processor: `Intel(R) Core(TM) i5-5300U CPU @ 2.30GHz`. Julia version 1.1.1 was used.
The following block is the result of running `julia --project=. benchmark/benchmarks.jl` on a computer with the processor: `2,4 GHz 8-Core Intel Core i9`. Julia version 1.5.3 was used.

```
Direct:
49.724 ns (0 allocations: 0 bytes)
ResumableFunctions:
10.230 μs (281 allocations: 8.83 KiB)
Channels csize=0:
305.877 μs (465 allocations: 8.84 KiB)
Channels csize=1:
504.438 μs (379 allocations: 7.33 KiB)
Channels csize=20:
87.889 μs (206 allocations: 5.06 KiB)
Channels csize=100:
67.911 μs (198 allocations: 6.44 KiB)
Closure:
2.437 μs (83 allocations: 1.31 KiB)
Closure optimised:
245.676 ns (3 allocations: 64 bytes)
Closure statemachine:
40.149 ns (0 allocations: 0 bytes)
Iteration protocol:
63.635 ns (0 allocations: 0 bytes)
Direct:
27.162 ns (0 allocations: 0 bytes)
ResumableFunctions:
27.476 ns (0 allocations: 0 bytes)
Channels csize=0:
2.421 ms (101 allocations: 3.08 KiB)
Channels csize=1:
2.502 ms (23 allocations: 1.88 KiB)
Channels csize=20:
138.977 μs (26 allocations: 2.36 KiB)
Channels csize=100:
34.843 μs (28 allocations: 3.95 KiB)
Closure:
1.831 μs (82 allocations: 1.28 KiB)
Closure optimised:
21.537 ns (0 allocations: 0 bytes)
Closure statemachine:
27.486 ns (0 allocations: 0 bytes)
Iteration protocol:
40.306 ns (0 allocations: 0 bytes)
```

## Licence & References
Expand All @@ -82,12 +83,16 @@ Iteration protocol:

## Release notes

* 2020: v0.5.2 is Julia v1.6 compatible
* 2021: v0.6.0
* introduction of `@nosave` to keep a variable out of the saved structure.
* optimized `for` loop.

* 2020: v0.5.2 is Julia v1.6 compatible.

* 2019: v0.5.1
* inference problem solved: force iterator next value to be of type `Union` of `Tuple` and `Nothing`.

* 2019: v0.5.0 is Julia v1.2 compatible
* 2019: v0.5.0 is Julia v1.2 compatible.

* 2018: v0.4.2 prepare for Julia v1.1
* better inference caused a problem;).
Expand Down
119 changes: 67 additions & 52 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,131 @@ using ResumableFunctions

const n = 93

function direct_fib(n)
a = zero(Int)
b = a + one(a)
for i in 1:n-1
a, b = b, a+b
end#for
function direct(a::Int, b::Int)
b, a+b
end

function test_direct(n::Int)
a, b = zero(Int), one(Int)
for _ in 1:n-1
a, b = direct(a, b)
end
a
end#function
end

@resumable function fibonnaci_resumable(n)
a = zero(Int)
b = a + one(a)
for i in 1:n
@resumable function fibonnaci_resumable(n::Int)
a, b = zero(Int), one(Int)
for _ in 1:n
@yield a
a, b = b, a + b
end
end

function test_resumable(n)
@noinline function test_resumable(n::Int)
a = 0
for i in fibonnaci_resumable(n)
a = i
end#for
for v in fibonnaci_resumable(n)
a = v
end
a
end

function fibonnaci_channel(n, ch::Channel)
a = zero(Int)
b = a + one(a)
for i in 1:n
function fibonnaci_channel(n::Int, ch::Channel)
a, b = zero(Int), one(Int)
for _ in 1:n
put!(ch, a)
a, b = b, a + b
end
end

function test_channel(n, csize::Int)
@noinline function test_channel(n::Int, csize::Int)
fib_channel = Channel(c -> fibonnaci_channel(n, c); ctype=Int, csize=csize)
a = 0
for i in fib_channel
a = i
end#for
for v in fib_channel
a = v
end
a
end

function fibonnaci_closure()
a = zero(Int)
b = a + one(Int)
a, b = zero(Int), one(Int)
function()
tmp = a
a, b = b, a + b
tmp
end
end

function test_closure(n)
@noinline function test_closure(n::Int)
fib_closure = fibonnaci_closure()
a = 0
for i in 1:n
for _ in 1:n
a = fib_closure()
end
a
end

function fibonnaci_closure_opt()
a = Ref(zero(Int))
b = Ref(a[] + one(Int))
@noinline function()
b = Ref(one(Int))
function()
tmp = a[]
a[], b[] = b[], a[] + b[]
tmp
end
end

function test_closure_opt(n)
@noinline function test_closure_opt(n::Int)
fib_closure = fibonnaci_closure_opt()
a = 0
for i in 1:n
for _ in 1:n
a = fib_closure()
end
a
end

function fibonnaci_closure_stm()
a = Ref(zero(Int))
b = Ref(a[] + one(Int))
_state = Ref(zero(UInt8))
function fibonnaci_closure_stm(n::Int)
_state = Ref(0x00)
a = Ref{Int}()
b = Ref{Int}()
_iterstate_1 = Ref{Int}()
_iterator_1 = Ref{UnitRange{Int}}()

function(_arg::Any=nothing)
_state[] == 0x00 && @goto _STATE_0
_state[] == 0x01 && @goto _STATE_1
_state[] === 0x00 && @goto _STATE_0
_state[] === 0x01 && @goto _STATE_1
error("@resumable function has stopped!")
@label _STATE_0
_state[] = 0xff
_arg isa Exception && throw(_arg)
while true
a[], b[] = zero(Int), one(Int)
_iterator_1[] = 1:n
_iteratornext_1 = iterate(_iterator_1[])
while _iteratornext_1 !== nothing
(_, _iterstate_1[]) = _iteratornext_1
_state[] = 0x01
return a[]
@label _STATE_1
_state[] = 0xff
_arg isa Exception && throw(_arg)
a[], b[] = b[], a[] + b[]
(a[], b[]) = (b[], a[] + b[])
_iteratornext_1 = iterate(_iterator_1[], _iterstate_1[])
end
end
end

function test_closure_stm(n)
fib_closure = fibonnaci_closure_stm()
fib_clo_stm = fibonnaci_closure_stm(n)

function Base.iterate(f::typeof(fib_clo_stm), state=nothing)
a = f()
f._state[] === 0xff && return nothing
a, nothing
end

@noinline function test_closure_stm(n::Int)
fib_closure = fibonnaci_closure_stm(n)
a = 0
for i in 1:n
a = fib_closure()
for v in fibonnaci_closure_stm(n)
a = v
end
a
end
Expand All @@ -121,28 +137,27 @@ struct FibN
end

function Base.iterate(f::FibN, state::NTuple{3,Int}=(0, 1, 1))
@inbounds state[3] >= f.n && return nothing
a, b, iters = state
@inbounds b, (b, a + b, iters + 1)
iters > f.n && return nothing
a, (b, a + b, iters + 1)
end

function test_iteration_protocol(n)
fib = FibN(n)
@noinline function test_iteration_protocol(n::Int)
a = 0
for i in fib
a = i
for v in FibN(n)
a = v
end
a
end

isinteractive() || begin
println("Direct: ")
@btime direct_fib($n)
@assert direct_fib(n) == 7540113804746346429
@btime test_direct($n)
@assert test_direct(n) == 7540113804746346429

println("ResumableFunctions: ")
@btime test_resumable($n)
@assert direct_fib(n) == 7540113804746346429
@assert test_resumable(n) == 7540113804746346429

println("Channels csize=0: ")
@btime test_channel($n, $0)
Expand Down
7 changes: 4 additions & 3 deletions docs/src/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ mutable struct ##123 <: ResumableFunctions.FiniteStateMachineIterator
_iterstate :: Int64
i :: Int64

function ##123(n::Int64)
function ##123()
fsmi = new()
fsmi._state = 0x00
fsmi.n = n
fsmi
end
end
Expand All @@ -86,7 +85,9 @@ A call function is constructed that creates the previously defined composite typ

```julia
function fibonnaci(n::Int)
##123(n)
fsmi = ##123(n)
fsmi.n = n
fsmi
end
```

Expand Down
2 changes: 1 addition & 1 deletion src/ResumableFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ResumableFunctions
using MacroTools
using MacroTools: combinedef, combinearg, flatten, postwalk

export @resumable, @yield
export @resumable, @yield, @nosave

include("types.jl")
include("transforms.jl")
Expand Down
Loading

2 comments on commit b2d2db0

@BenLauwens
Copy link
Collaborator 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/27322

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.0 -m "<description of version>" b2d2db03ab541b0a28f8f7cc52bea864e066695c
git push origin v0.6.0

Please sign in to comment.