-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
anonymous function calls have a huge overhead #1864
Comments
I believe this was also one of the major sources of slowdowns in a previous discussion about the use of closures for MCMC. Very worth getting right. |
See also the discussion on how this penalizes FFT performance. |
Ouch. I really love anonymous function syntax. +1 for getting it right. |
Is this the same issue I'm seeing? Loops written directly at the REPL are much slower than inside a function:
It takes no time at all to compile the function, this behaviour is surprising! |
@crisluengo See "Avoid global variables" at http://docs.julialang.org/en/latest/manual/performance-tips/ |
No, that's a different issue due to the difference between global and local variables: http://docs.julialang.org/en/latest/manual/performance-tips/#avoid-global-variables. |
Is the priority here really 1.0 milestone? Just bumping to check as this came up in a user thread again: https://groups.google.com/forum/#!topic/julia-users/BhQjHGUx2lA |
Does this one deserve a |
I'd think yes! This is worth fixing sooner than later. |
It only deserves a |
I almost have this fixed over here. There's still one bit of sorcery left (needed to make them work properly when created inside a function), and unfortunately the proper metaprogramming incantations are proving to be surprisingly tricky. It will probably boil down to about 5 lines of code, but I've spent about a day on those 5 lines so far to no avail 😦. I'm annoyed at myself because I thought my metaprogramming-fu was well beyond having trouble with this kind of stuff. |
@tknopp of course. I'm just creating some noise and hoping it will happen. ;) This is beyond me to take on. |
@shashi: Yes bumping issues is absolutely ok. But the 0.4 tag should IMHO only be applied if someone plans to work on it. |
@timholy very cool. Patched to allow for functions with no name ;) timholy/FastAnonymous.jl#1 |
@timholy your implementation generates the "anon" function at toplevel scope so the resulting anon function won't have the correct scoping semantics when generated inside a function. I don't think any amount of macro foo can get around that limitation. |
No, it can. Think I almost have it now. Crossing fingers. |
Ta-dah! Done. It's amazing what a catalytic effect complaining has 😄. |
We still probably want to leave this issue open, however, because aside from needing the
rather than
Still, it seems to finally be a workable, general solution to the problem. |
I don't want to be a downer but: julia> function test()
y = 0
fn = @anon x -> x * y
while y < 50
y += 10; @show fn(10)
sleep(1)
end
end
test (generic function with 1 method)
julia> test()
fn(10) => 0
fn(10) => 0
fn(10) => 0
fn(10) => 0
fn(10) => 0
julia> function test()
y = 0
fn = (x) -> x * y
while y < 50
y += 10; @show fn(10)
sleep(1)
end
end
test (generic function with 1 method)
julia> test()
fn(10) => 100
fn(10) => 200
fn(10) => 300
fn(10) => 400
fn(10) => 500 |
Fair enough. But that's what I'd call a serious corner case. 99% of the time (for me at least), once I define an anonymous function I don't want it interacting further with local variables. |
In other words, |
Though that could be my matlab upbringing (for which anonymous functions work like |
Your last few statements made me cry a bit inside as a lisper :-) |
@stevengj, if I were proposing to merge this into base, it would have been a PR, not a package. I'm not personally worried about the GC issue, because my anonymous functions use small parameters. May not serve your needs, but it will serve mine. @rfourquet, yes, that would work, although the dict lookup would be slow. You're right that for a fast implementation, basically what seems to be missing is the ability to define Once we also get
to get translated into something like this:
That would also solve @stevengj's concern about GC. |
@timholy that would not work either as the |
If the user puts it in the wrong place, that's his/her problem. It was only intended to work when it contains the entire scope (that's why I put it outside the But one likely problem with the scheme outlined above is if the compiler insists on compiling |
@timholy I meant that anonymous functions created with -> syntax actually appear to be faster in that gist. This holds for simpler forms of @stevengj's original cases as well. julia> a(x) = (x, 2)
a (generic function with 1 method)
julia> b(x) = y -> (x, y)
b (generic function with 1 method)
julia> c = b(2)
julia> @time c(1)
elapsed time: 0.001894234 seconds (15064 bytes allocated)
(2,1)
julia> @time c(1)
elapsed time: 3.484e-6 seconds (112 bytes allocated)
(2,1)
julia> @time a(1)
elapsed time: 0.002761751 seconds (7144 bytes allocated)
(1,2)
julia> @time a(1)
elapsed time: 4.714e-6 seconds (112 bytes allocated)
|
Obviously normal anonymous functions should be fast. @rfourquet is right that callable objects enable some new more efficient approaches. |
On a related note, I tried a bit to implement function types for anonymous functions a while ago. I got some julia time right now that I'd prefer spend on the gc stuff, but I pushed the branch to my clone if anybody wants to look at/work on top of it : https://github.com/carnaval/julia/tree/fty. (Test suite doesn't pass with > 1 worker, I suspect due to some lambda serialization issue).
but perhaps more importantly :
so if I'm not mistaken this is could be a step forward for the infamous "first element type of map" issue. The function type is determined by declaration (it only looks at the last statement for the return signature for now, so it can be fooled) and not by inference. However inference understands and (should?) correctly apply function types. |
Since no one responded here to @carnaval, I'll just say: excited to hear you have some Julia time coming! As always you are willing & able to tackle hard problems. We need more of that, and I'll look forward to whatever comes from your efforts. |
@carnaval This is very cool! I wonder if efficient closures can be implemented in a simpler way after #8712 Could someone explain why the following is the case? julia> x = rand(int(1e8));
julia> @time map(y->y, x);
elapsed time: 2.734845271 seconds (2400001232 bytes allocated, 24.31% gc time)
julia> @time broadcast(y->y, x);
elapsed time: 5.599463778 seconds (4000311128 bytes allocated, 25.05% gc time)
julia> id(a) = a
julia> @time map(id, x);
elapsed time: 5.109159967 seconds (4000002344 bytes allocated, 26.13% gc time)
julia> @time broadcast(id, x);
elapsed time: 0.545501696 seconds (800329264 bytes allocated, 19.08% gc time) |
@shashi, it's because Lines 69 to 81 in ffebd6a
Lines 183 to 193 in ffebd6a
Lines 217 to 229 in ffebd6a
|
@carnaval any progress on the typed anonymous functions? I would appreciate having those a lot! Of course, having typed generic functions will be nice too. However, typed anonymous functions would cover 90% of my use-cases already and would be nice to have in itself. |
Hum. That branch must have bitrotten since then. I remember there was (unsurprisingly) problems rebasing on top of the call overload thing. I'll try to get it back in proof-of-concept state but I won't have time to actively develop it for the coming month. Maybe opening a PR will bait someone in taking up the work :) |
fixed by #13412. |
🍹 |
:D |
$ git log --pretty=oneline --abbrev-commit f9a5cc7..d8fde7e d8fde7e fix typo in telemetry notice (#1877) 188893d Merge pull request #1871 from JuliaLang/sk/telemetry ab88ea9 telemetry notice: reword a bit, factor into function 341dfd0 telemetry: don't send salt hash header ae99ba2 telemetry notice: only print once per process 636d333 Add a poor-mans file lock for telemetry file. b24ca68 telemetry: print legal notice first time talking to each pkg server bd07a8d telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2 telemetry: HyperLogLog estimator 3e3f9d7 README: howto build Julia with git checkout of Pkg (#1864) 100eaa9 Fix bug which made `registry up` a no-op instead of updating all user-registries. (#1862)
$ git log --pretty=oneline --abbrev-commit f9a5cc7..f80d443 f80d443 Disable SIGQUIT test that regularly segfaults on CI. (#1883) 2328d1b telemetry: don't send hash without active project 08fe651 telemetry: use RandomDevice for random values c213096 Improve Artifacts documentation by adding a Basic Usage section (#1879) d8fde7e fix typo in telemetry notice (#1877) 188893d Merge pull request #1871 from JuliaLang/sk/telemetry ab88ea9 telemetry notice: reword a bit, factor into function 341dfd0 telemetry: don't send salt hash header ae99ba2 telemetry notice: only print once per process 636d333 Add a poor-mans file lock for telemetry file. b24ca68 telemetry: print legal notice first time talking to each pkg server bd07a8d telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2 telemetry: HyperLogLog estimator 3e3f9d7 README: howto build Julia with git checkout of Pkg (#1864) 100eaa9 Fix bug which made `registry up` a no-op instead of updating all user-registries. (#1862)
$ git log --pretty=oneline --abbrev=commit f9a5cc7..531861f 531861f6677f434f6d594212d1085ac5bc309328 Make tree hash computation non-fatal when installing artifacts. (#1885) ce4a41ee7d232b90da925fa9ebe246618de8ada4 Misc fixes (#1884) f80d44345f61d0c1a02650f33322f9a330aa77d1 Disable SIGQUIT test that regularly segfaults on CI. (#1883) 2328d1bcd919bd9fc038af3f1cedf43a22d28e30 telemetry: don't send hash without active project 08fe651707d6deb392950f71c3ea776a2c8337c9 telemetry: use RandomDevice for random values c213096ea6210f66945443658b6548012dff5ec9 Improve Artifacts documentation by adding a Basic Usage section (#1879) d8fde7e85b72c86cdae395a1ea13485c193dc35e fix typo in telemetry notice (#1877) 188893dfdb6608cd5bcab18c6d365843cf1c6056 Merge pull request #1871 from JuliaLang/sk/telemetry ab88ea9c3ea05a70c2e014e6ceb5ce55a7fa9cf7 telemetry notice: reword a bit, factor into function 341dfd0bfafb994bd7c71271d2a458e314f3af4c telemetry: don't send salt hash header ae99ba2ed9d9cd7f9962a2515fd62054eb644097 telemetry notice: only print once per process 636d333a263640650d09b08a568e8e128bf6c00c Add a poor-mans file lock for telemetry file. b24ca68ed000139507d179f49a04df7c7b93d14c telemetry: print legal notice first time talking to each pkg server bd07a8d6439ffbe55f9aaf22ea5e2c451ee480cb telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2c59c4110a6c4775414202b4df840acf21 telemetry: HyperLogLog estimator 3e3f9d7a3baf3a96c860d2b1bc895d5fd2d40d35 README: howto build Julia with git checkout of Pkg (#1864) 100eaa9b0ea0a2b95072c77bdf7060e72479fe13 Fix bug which made `registry up` a no-op instead of updating all user-registries. (#1862)
$ git log --pretty=oneline --abbrev=commit f9a5cc7..13456b9 13456b944fd21ec180111c6c29d06b610d47d088 Deprecate the REPL command `] generate` (#1923) 3eee6eb25974c2f320706da1b81ce1f7e1d82165 BinaryPlatforms: Recognize MacOS(:aarch64) (#1916) a8cc6d670ebe55002c5d5efb7fb19315c0c1cd55 Telemetry CI variables: add CI_SERVER, JULIA_PKGEVAL, JULIA_REGISTRYCI_AUTOMERGE, and PKGEVAL (#1906) ae897bc44070f902b5b01da80478ed51c1b518c0 Fix invalidations from loading OrderedCollections (#1897) 69bc387b3932d3d709b656e1ef929c4a2592fcee fix path returned from find_install (#1908) 46c9d430d545ecd70b147942857d775b2834e54b Update environments.md (#1896) 20f9b9e14ab59d81991b5984a2f9eb87d38b6230 Merge pull request #1869 from JuliaLang/teh/inval3 605f48849fa6b3bf29e6785967d4ef2249da9976 - only test on nightly, test without Pkg server as well - CI: build docs on nightly 7e0c911591f5d62acb476f74c59a9cad9c7a1f7d Avoid a strange inference limit when broadcasting f92ee5786468a7d0e836a94b0bb09711faef78da Use `maximum(itr; init=default)` d524d0f7f8cfef8abe429d3b947157eacb84f8ea Avoid calling ==(::Any, ::Nothing) in read_field 2acb2f9ca7f8b908954d6fdde074b65e30b8395f Add more type info 9b5e38e0ee2a0747b5ca7125a3a1f347cbd61708 Improve inference for Platform 0ebffb95d9793689d4c72198ae4804b0745ee16e Pre-allocate `seen` to improve inference in `unique(f, itr)` 160efbea2b56dc8a7a9a5ed8f795fa8995a4f09b Eliminate some boxing cdfc445873fc1d5df8838dd080e125bcce587dc6 docs: delete note about gen-project script (#1887) 531861f6677f434f6d594212d1085ac5bc309328 Make tree hash computation non-fatal when installing artifacts. (#1885) ce4a41ee7d232b90da925fa9ebe246618de8ada4 Misc fixes (#1884) f80d44345f61d0c1a02650f33322f9a330aa77d1 Disable SIGQUIT test that regularly segfaults on CI. (#1883) 2328d1bcd919bd9fc038af3f1cedf43a22d28e30 telemetry: don't send hash without active project 08fe651707d6deb392950f71c3ea776a2c8337c9 telemetry: use RandomDevice for random values c213096ea6210f66945443658b6548012dff5ec9 Improve Artifacts documentation by adding a Basic Usage section (#1879) d8fde7e85b72c86cdae395a1ea13485c193dc35e fix typo in telemetry notice (#1877) 188893dfdb6608cd5bcab18c6d365843cf1c6056 Merge pull request #1871 from JuliaLang/sk/telemetry ab88ea9c3ea05a70c2e014e6ceb5ce55a7fa9cf7 telemetry notice: reword a bit, factor into function 341dfd0bfafb994bd7c71271d2a458e314f3af4c telemetry: don't send salt hash header ae99ba2ed9d9cd7f9962a2515fd62054eb644097 telemetry notice: only print once per process 636d333a263640650d09b08a568e8e128bf6c00c Add a poor-mans file lock for telemetry file. b24ca68ed000139507d179f49a04df7c7b93d14c telemetry: print legal notice first time talking to each pkg server bd07a8d6439ffbe55f9aaf22ea5e2c451ee480cb telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2c59c4110a6c4775414202b4df840acf21 telemetry: HyperLogLog estimator 3e3f9d7a3baf3a96c860d2b1bc895d5fd2d40d35 README: howto build Julia with git checkout of Pkg (#1864) 100eaa9b0ea0a2b95072c77bdf7060e72479fe13 Fix bug which made `registry up` a no-op instead of updating all user-registries. (#1862)
$ git log --pretty=oneline --abbrev=commit f9a5cc7..13456b9 13456b944fd21ec180111c6c29d06b610d47d088 Deprecate the REPL command `] generate` (#1923) 3eee6eb25974c2f320706da1b81ce1f7e1d82165 BinaryPlatforms: Recognize MacOS(:aarch64) (#1916) a8cc6d670ebe55002c5d5efb7fb19315c0c1cd55 Telemetry CI variables: add CI_SERVER, JULIA_PKGEVAL, JULIA_REGISTRYCI_AUTOMERGE, and PKGEVAL (#1906) ae897bc44070f902b5b01da80478ed51c1b518c0 Fix invalidations from loading OrderedCollections (#1897) 69bc387b3932d3d709b656e1ef929c4a2592fcee fix path returned from find_install (#1908) 46c9d430d545ecd70b147942857d775b2834e54b Update environments.md (#1896) 20f9b9e14ab59d81991b5984a2f9eb87d38b6230 Merge pull request #1869 from JuliaLang/teh/inval3 605f48849fa6b3bf29e6785967d4ef2249da9976 - only test on nightly, test without Pkg server as well - CI: build docs on nightly 7e0c911591f5d62acb476f74c59a9cad9c7a1f7d Avoid a strange inference limit when broadcasting f92ee5786468a7d0e836a94b0bb09711faef78da Use `maximum(itr; init=default)` d524d0f7f8cfef8abe429d3b947157eacb84f8ea Avoid calling ==(::Any, ::Nothing) in read_field 2acb2f9ca7f8b908954d6fdde074b65e30b8395f Add more type info 9b5e38e0ee2a0747b5ca7125a3a1f347cbd61708 Improve inference for Platform 0ebffb95d9793689d4c72198ae4804b0745ee16e Pre-allocate `seen` to improve inference in `unique(f, itr)` 160efbea2b56dc8a7a9a5ed8f795fa8995a4f09b Eliminate some boxing cdfc445873fc1d5df8838dd080e125bcce587dc6 docs: delete note about gen-project script (#1887) 531861f6677f434f6d594212d1085ac5bc309328 Make tree hash computation non-fatal when installing artifacts. (#1885) ce4a41ee7d232b90da925fa9ebe246618de8ada4 Misc fixes (#1884) f80d44345f61d0c1a02650f33322f9a330aa77d1 Disable SIGQUIT test that regularly segfaults on CI. (#1883) 2328d1bcd919bd9fc038af3f1cedf43a22d28e30 telemetry: don't send hash without active project 08fe651707d6deb392950f71c3ea776a2c8337c9 telemetry: use RandomDevice for random values c213096ea6210f66945443658b6548012dff5ec9 Improve Artifacts documentation by adding a Basic Usage section (#1879) d8fde7e85b72c86cdae395a1ea13485c193dc35e fix typo in telemetry notice (#1877) 188893dfdb6608cd5bcab18c6d365843cf1c6056 Merge pull request #1871 from JuliaLang/sk/telemetry ab88ea9c3ea05a70c2e014e6ceb5ce55a7fa9cf7 telemetry notice: reword a bit, factor into function 341dfd0bfafb994bd7c71271d2a458e314f3af4c telemetry: don't send salt hash header ae99ba2ed9d9cd7f9962a2515fd62054eb644097 telemetry notice: only print once per process 636d333a263640650d09b08a568e8e128bf6c00c Add a poor-mans file lock for telemetry file. b24ca68ed000139507d179f49a04df7c7b93d14c telemetry: print legal notice first time talking to each pkg server bd07a8d6439ffbe55f9aaf22ea5e2c451ee480cb telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2c59c4110a6c4775414202b4df840acf21 telemetry: HyperLogLog estimator 3e3f9d7a3baf3a96c860d2b1bc895d5fd2d40d35 README: howto build Julia with git checkout of Pkg (#1864) 100eaa9b0ea0a2b95072c77bdf7060e72479fe13 Fix bug which made `registry up` a no-op instead of updating all user-registries. (#1862)
$ git log --pretty=oneline --abbrev=commit f9a5cc7..13456b9 13456b944fd21ec180111c6c29d06b610d47d088 Deprecate the REPL command `] generate` (JuliaLang#1923) 3eee6eb25974c2f320706da1b81ce1f7e1d82165 BinaryPlatforms: Recognize MacOS(:aarch64) (JuliaLang#1916) a8cc6d670ebe55002c5d5efb7fb19315c0c1cd55 Telemetry CI variables: add CI_SERVER, JULIA_PKGEVAL, JULIA_REGISTRYCI_AUTOMERGE, and PKGEVAL (JuliaLang#1906) ae897bc44070f902b5b01da80478ed51c1b518c0 Fix invalidations from loading OrderedCollections (JuliaLang#1897) 69bc387b3932d3d709b656e1ef929c4a2592fcee fix path returned from find_install (JuliaLang#1908) 46c9d430d545ecd70b147942857d775b2834e54b Update environments.md (JuliaLang#1896) 20f9b9e14ab59d81991b5984a2f9eb87d38b6230 Merge pull request JuliaLang#1869 from JuliaLang/teh/inval3 605f48849fa6b3bf29e6785967d4ef2249da9976 - only test on nightly, test without Pkg server as well - CI: build docs on nightly 7e0c911591f5d62acb476f74c59a9cad9c7a1f7d Avoid a strange inference limit when broadcasting f92ee5786468a7d0e836a94b0bb09711faef78da Use `maximum(itr; init=default)` d524d0f7f8cfef8abe429d3b947157eacb84f8ea Avoid calling ==(::Any, ::Nothing) in read_field 2acb2f9ca7f8b908954d6fdde074b65e30b8395f Add more type info 9b5e38e0ee2a0747b5ca7125a3a1f347cbd61708 Improve inference for Platform 0ebffb95d9793689d4c72198ae4804b0745ee16e Pre-allocate `seen` to improve inference in `unique(f, itr)` 160efbea2b56dc8a7a9a5ed8f795fa8995a4f09b Eliminate some boxing cdfc445873fc1d5df8838dd080e125bcce587dc6 docs: delete note about gen-project script (JuliaLang#1887) 531861f6677f434f6d594212d1085ac5bc309328 Make tree hash computation non-fatal when installing artifacts. (JuliaLang#1885) ce4a41ee7d232b90da925fa9ebe246618de8ada4 Misc fixes (JuliaLang#1884) f80d44345f61d0c1a02650f33322f9a330aa77d1 Disable SIGQUIT test that regularly segfaults on CI. (JuliaLang#1883) 2328d1bcd919bd9fc038af3f1cedf43a22d28e30 telemetry: don't send hash without active project 08fe651707d6deb392950f71c3ea776a2c8337c9 telemetry: use RandomDevice for random values c213096ea6210f66945443658b6548012dff5ec9 Improve Artifacts documentation by adding a Basic Usage section (JuliaLang#1879) d8fde7e85b72c86cdae395a1ea13485c193dc35e fix typo in telemetry notice (JuliaLang#1877) 188893dfdb6608cd5bcab18c6d365843cf1c6056 Merge pull request JuliaLang#1871 from JuliaLang/sk/telemetry ab88ea9c3ea05a70c2e014e6ceb5ce55a7fa9cf7 telemetry notice: reword a bit, factor into function 341dfd0bfafb994bd7c71271d2a458e314f3af4c telemetry: don't send salt hash header ae99ba2ed9d9cd7f9962a2515fd62054eb644097 telemetry notice: only print once per process 636d333a263640650d09b08a568e8e128bf6c00c Add a poor-mans file lock for telemetry file. b24ca68ed000139507d179f49a04df7c7b93d14c telemetry: print legal notice first time talking to each pkg server bd07a8d6439ffbe55f9aaf22ea5e2c451ee480cb telemetry: ~/.julia/servers/telemetry.toml for defaults 70dfbd2c59c4110a6c4775414202b4df840acf21 telemetry: HyperLogLog estimator 3e3f9d7a3baf3a96c860d2b1bc895d5fd2d40d35 README: howto build Julia with git checkout of Pkg (JuliaLang#1864) 100eaa9b0ea0a2b95072c77bdf7060e72479fe13 Fix bug which made `registry up` a no-op instead of updating all user-registries. (JuliaLang#1862)
A surprising factor of 100 slowdown in this simple example:
I'm guessing there is some lack of JIT-ing going on? This affects #1856.
The text was updated successfully, but these errors were encountered: