- Notifications
You must be signed in to change notification settings - Fork6
Faster closure variable capture
License
c42f/FastClosures.jl
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A workaround forJuliaLang/julia#15276, for julia-1.x,somewhat in the spirit of FastAnonymous.jl. Provides the@closure
macro,which wraps a closure in alet
block to make reading variable bindings privateto the closure. In certain cases, this make using the closure - and the codesurrouding it - much faster. Note that it's not clear that thelet
blocktrick implemented in this package helps at all in julia-0.5. However, julia-0.5compatibility is provided for backward compatibility convenience.
@closure closure_expression
Wrap the closure definitionclosure_expression
in a let block to encouragethe julia compiler to generate improved type information. For example:
callfunc(f)=f()functionfoo(n)for i=1:nif 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"))endendend
Here's a further example of where this helps:
using FastClosures# code_warntype problemfunctionf1()iftrueend r=1 cb= ()->ridentity(cb)end# code_warntype cleanfunctionf2()iftrueend r=1 cb=@closure ()->ridentity(cb)end@code_warntypef1()@code_warntypef2()
About
Faster closure variable capture
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.