-
-
Notifications
You must be signed in to change notification settings - Fork 322
🐰 Wrap code in functions to make it faster #720
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
Conversation
|
Right now I did the rewrite but no cell is being function wrapped yet |
|
Getting there! |
|
Done! 🌈🍡💜💚🧡 TODO:
|
|
@NHDaly this is what we talked about a long long time ago 😊 |
|
|
||
| @gensym result | ||
| @gensym elapsed_ns | ||
| # we don't use `quote ... end` here to avoid the LineNumberNodes that it adds (these would taint the stack trace). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could use MacroTools.striplines(ex) for removing LineNumberNotes from expressions generated by quote (if this would be beneficial for you).
|
looks cool, I'll try it out tomorrow! |
|
@lungben Let me know if you find anything, but I'm pretty sure that it's done! Yayyy |
|
looks good so far! a) b) Timings: with this PR: Observations:
Suggestions:
|
|
About local variables: this is why Julia has About the new scoping rules: oops! Thank you for finding this, but like you mentioned, it's an accidental feature. 👍👍 About a blog post: maybe... I think that in the case of Pluto, documentation is a sign that I am doing something wrong. 🙃 For example, to me, the best of this PR is that I can remove this one line: https://github.com/fonsp/Pluto.jl/pull/720/files#diff-68739a2259e7362fd1fe549bef016303e9d476e26d9536797fce6cec7623484dL168-L172 Note that |
|
Thanks! |
|
🎉!! @fonsp this is awesome, I'm super glad to see this improvement! 🎉 Thanks for pinging me! :) 🤩 Amazing results!! 🤩 |
|
Also, wow, it's so cool how much faster this got. Hehe technically this isn't including the compilation time in the time result, but that's standard for what julia does for julia> @time 2+2
0.000000 seconds
4
julia> @time @eval 2+2
0.000970 seconds (53 allocations: 3.516 KiB)
4But still, after this change, even if you include compilation time, things get much faster because you've changed a non-const global into a function argument. For the old case before this PR, julia had to check every iteration of the loop that So even if you include the compilation time, the new system is still faster than evaling the block old way! 🎉 I guess Pluto could consider to separately report the compilation time as well? Julia 1.6 has started doing that: |





We turn a simple cell like
s1 = s2 = [sqrt(i) for i in x]into a function so that Julia can 1) compile a type-specialized version, which can also be 2) called a second time without compilation!The generated function takes the referenced globals as arguments, and returns the defined globals.
would become (skipping some details):
We can do this because Pluto already knows the set of referenced globals (function arguments) and defined globals (function outputs) -- we use it create reactivity!
Function generation is here:
https://github.com/fonsp/Pluto.jl/pull/720/files#diff-0cc97f3d6a0f647a05e5f913d416242ecb00f6e67e12004a7204b8846a3fa44cR96-R104
and it is called here:
https://github.com/fonsp/Pluto.jl/pull/720/files#diff-0cc97f3d6a0f647a05e5f913d416242ecb00f6e67e12004a7204b8846a3fa44cR112
Not every cell will be run as a function. Cells that import, define a function, or type, or call a macro still run with the old method:
https://github.com/fonsp/Pluto.jl/pull/720/files#diff-04c7252262a0e6de2db2cded7aba9b1130c1e5ba6adff25bf8ec3a34e36a4788R928-R946
And the best part is: all of this is completely invisible! You get superfast code without having to rewrite anything ❤