Coder Social home page Coder Social logo

Comments (4)

Krastanov avatar Krastanov commented on May 25, 2024

It seems that sometime in the last two years there was a significant regression to the benchmarks. What you describe is supposed to be fast (kinda the reason this library was created in the first place), but it was out of maintenance for a few years, so this might be due to a regression in the julia compiler or due to a minor modification in the library that was never tested for performance.

Similar issue is tracked here #72

I can not commit to fixing this in the near term, but do not hesitate to ask questions if you decide to give it a try.

from resumablefunctions.jl.

pepijndevos avatar pepijndevos commented on May 25, 2024

I'm trying this again on @gerlero's branch, and my conclusion so far seems to be that ResumableFunctions only really work reasonably in concrete typed functions.

Which is kind of a problem if you want to compose functions. You can't really write a function that takes an iterator and produces something with good performance.

using ResumableFunctions
using BenchmarkTools

bin(s, bins) = Iterators.map(((x, y),) -> (x, searchsortedfirst(bins, y)), s)

@resumable function any_hysteresis(s)
    bin = -1
    for (x, y) in s
        @yield if iseven(y)
            (x, (bin+1)÷2)
        else
            bin = y
            (x, (y+1)÷2)
        end
    end
end

@resumable function hysteresis(s::Vector{Tuple{Int,Int}})
    bin = -1
    for (x, y) in s
        @yield if iseven(y)
            (x, (bin+1)÷2)
        else
            bin = y
            (x, (y+1)÷2)
        end
    end
end

tolbins(bins, tol=1e-9) = collect(Iterators.flatmap((x -> (x-tol,x+tol)), bins))

hybin(s, bins, tol=1e-9) = bin(s, tolbins(bins, tol)) |> collect

s = collect(zip(1:1000, rand(1000)))

b = hybin(s, [0.5])

println("Any typed")
@btime for _ in any_hysteresis(b) end;
println("Struct types")
println(fieldtypes(typeof(any_hysteresis([(0,0)]))))
println("Vector typed")
@btime for _ in hysteresis(b) end;
println("Struct types")
println(fieldtypes(typeof(hysteresis([(0,0)]))))

Result:

Any typed
  202.546 μs (11470 allocations: 335.52 KiB)
Struct types
(UInt8, Any, Any, Any, Any, Any, Any)
Vector typed
  2.027 μs (1 allocation: 64 bytes)
Struct types
(UInt8, Int64, Vector{Tuple{Int64, Int64}}, Int64, Int64, Vector{Tuple{Int64, Int64}}, Int64)

from resumablefunctions.jl.

Krastanov avatar Krastanov commented on May 25, 2024

I tried to simplify the examples you gave a bit. While not covering everything you mentioned, I think a good first step would be to try to make the following faster:

@resumable function one_to_n(n)
    for i in 1:n
        @yield i
    end
end

@resumable function one_to_n_typed(n::Int)
    for i in 1:n
        @yield i
    end
end

function append_one_to_n(n)
    l = typeof(n)[]
    for i in 1:n
        push!(l, i)
    end
    l
end

const n = 100
@btime collect(one_to_n($n)) evals=1;       # 4.258 μs (306 allocations: 11.36 KiB)
@btime collect(one_to_n_typed($n)) evals=1; # 390.000 ns (5 allocations: 1.97 KiB)
@btime append_one_to_n($n);                 # 334.060 ns (4 allocations: 1.92 KiB)
@btime collect(1:$n) evals=1;               # 30.000 ns (1 allocation: 896 bytes)

This issue is quite multifaceted. If we start working on it, probably we should split it in smaller pieces along the lines of the topics you have already enumerated (type annotations in the generated structs, the need for function annotation, specialization, reporting length and eltype when possible).

from resumablefunctions.jl.

pepijndevos avatar pepijndevos commented on May 25, 2024

from resumablefunctions.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.