Skip to content

c42f/FastClosures.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastClosures

Build Status

A workaround for JuliaLang/julia#15276, for julia-1.x, somewhat in the spirit of FastAnonymous.jl. Provides the @closure macro, which wraps a closure in a let block to make reading variable bindings private to the closure. In certain cases, this make using the closure - and the code surrouding it - much faster. Note that it's not clear that the let block trick implemented in this package helps at all in julia-0.5. However, julia-0.5 compatibility is provided for backward compatibility convenience.

Interface

    @closure closure_expression

Wrap the closure definition closure_expression in a let block to encourage the julia compiler to generate improved type information. For example:

callfunc(f) = f()

function foo(n)
   for i=1:n
       if i >= n
           # Unlikely event - should be fast.  However, capture of `i` inside
           # the closure confuses the julia-0.6 compiler and causes it to box
           # the variable `i`, leading to a 100x performance hit if you remove
           # the `@closure`.
           callfunc(@closure ()->println("Hello \$i"))
       end
   end
end

Here's a further example of where this helps:

using FastClosures

# code_warntype problem
function f1()
    if true
    end
    r = 1
    cb = ()->r
    identity(cb)
end

# code_warntype clean
function f2()
    if true
    end
    r = 1
    cb = @closure ()->r
    identity(cb)
end

@code_warntype f1()
@code_warntype f2()