-
Notifications
You must be signed in to change notification settings - Fork 33
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
run string macros at compile, not runtime #162
Conversation
run string macros at compile, not runtime
Codecov ReportBase: 48.78% // Head: 49.12% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #162 +/- ##
==========================================
+ Coverage 48.78% 49.12% +0.33%
==========================================
Files 63 63
Lines 3425 3422 -3
==========================================
+ Hits 1671 1681 +10
+ Misses 1754 1741 -13
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Would really like this! I think having conversion of constants at compile time between a bunch of potentially different types ( function foo(x::T) where T
P = ("1.23", "1.24", "1.2345")
return evalpoly(x, parse.(T, P))
end
function foo2(x)
P = (df64"1.23", df64"1.24", df64"1.2345")
return evalpoly(x, P)
end
const P1 = (df64"1.23", df64"1.24", df64"1.2345")
foo3(x) = evalpoly(x, P1) Having all these behave similarly would be awesome... julia> x = Double64(1.2)
julia> @btime foo($x)
1.004 μs (15 allocations: 641 bytes)
4.49568
julia> @btime foo2($x)
1.012 μs (15 allocations: 641 bytes)
4.49568
julia> @btime foo3($x)
27.010 ns (0 allocations: 0 bytes)
4.49568 |
To make the first case work would require getting the compiler to constant-fold operations on strings; see this Discourse discussion. This PR should help with the second case though. |
On further thought, we don't need constant-folding to address the first case; we can use generated functions. This has good performance: @generated foo(x::T) where T = let P = map(x->parse(T, x), ("1.23", "1.24", "1.2345"))
:(evalpoly(x, $P))
end |
Per the docs, this fix can be even simpler because:
This means that the macro doesn't even need to return an expression. For example: macro df64_str(val::AbstractString)
Double64(val)
end does the trick; just unquote the expression. (See also: Discourse discussion) |
Can this be merged so I can forget about it? 🙏 |
yes do forget about it |
before:
after: