Skip to content
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

Merged
merged 3 commits into from
Feb 5, 2024

Conversation

uniment
Copy link
Contributor

@uniment uniment commented Feb 13, 2023

before:

julia> @btime df64"0.2";
  481.959 ns (4 allocations: 208 bytes)

after:

julia> @btime df64"0.2";
  1.100 ns (0 allocations: 0 bytes)

before:
julia> @Btime df64"0.2";
  481.959 ns (4 allocations: 208 bytes)

after:
julia> @Btime df64"0.2";
  1.100 ns (0 allocations: 0 bytes)
run string macros at compile, not runtime
@codecov
Copy link

codecov bot commented Feb 13, 2023

Codecov Report

Base: 48.78% // Head: 49.12% // Increases project coverage by +0.33% 🎉

Coverage data is based on head (b4cc725) compared to base (cde8772).
Patch coverage: 100.00% of modified lines in pull request are covered.

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     
Impacted Files Coverage Δ
src/type/parse.jl 38.09% <100.00%> (ø)
src/type/string.jl 58.33% <0.00%> (-1.13%) ⬇️
src/type/constructors.jl 57.57% <0.00%> (+3.03%) ⬆️
src/type/show.jl 31.57% <0.00%> (+26.57%) ⬆️

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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@heltonmc
Copy link
Member

Would really like this! I think having conversion of constants at compile time between a bunch of potentially different types (Float128, Double64, Float64x4) is really important. I fell into this trap in #134 so could probably close that issue too.

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

@uniment
Copy link
Contributor Author

uniment commented Feb 14, 2023

Having all these behave similarly

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.

@uniment
Copy link
Contributor Author

uniment commented Feb 15, 2023

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

@uniment
Copy link
Contributor Author

uniment commented Feb 21, 2023

Per the docs, this fix can be even simpler because:

expressions constructed by Julia code can have arbitrary run-time values without literal forms as args

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)

@uniment
Copy link
Contributor Author

uniment commented Feb 5, 2024

Can this be merged so I can forget about it? 🙏

@JeffreySarnoff
Copy link
Member

yes do forget about it
thank you

@JeffreySarnoff JeffreySarnoff merged commit 49c1944 into JuliaMath:main Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants