项目作者: c42f

项目描述 :
Faster closure variable capture
高级语言: Julia
项目地址: git://github.com/c42f/FastClosures.jl.git
创建时间: 2017-05-30T16:42:43Z
项目社区:https://github.com/c42f/FastClosures.jl

开源协议:Other

下载


FastClosures

Build Status

A workaround for https://github.com/JuliaLang/julia/issues/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

  1. @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:

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

Here’s a further example of where this helps:

  1. using FastClosures
  2. # code_warntype problem
  3. function f1()
  4. if true
  5. end
  6. r = 1
  7. cb = ()->r
  8. identity(cb)
  9. end
  10. # code_warntype clean
  11. function f2()
  12. if true
  13. end
  14. r = 1
  15. cb = @closure ()->r
  16. identity(cb)
  17. end
  18. @code_warntype f1()
  19. @code_warntype f2()