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

Wasteful conversion to BigFloat? #484

Closed
andreasKroepelin opened this issue Jan 29, 2025 · 3 comments
Closed

Wasteful conversion to BigFloat? #484

andreasKroepelin opened this issue Jan 29, 2025 · 3 comments

Comments

@andreasKroepelin
Copy link

andreasKroepelin commented Jan 29, 2025

By profiling my code that uses logerf(a::Float64, b::Float64), I realised that most time is spent in performing conversion to BigFloat. The reason is that this line compares the input values to the irrational value invsqrt2. This comparison, in turn, is defined in terms of comparing BigFloats, and dwarfs the computation time of the rest of the function.

Actually, I wonder why the constant invsqrt2 is used at all. I think the compiler can very well compile inv(sqrt(oftype(a, 2)) into a single statically known number.

I can't judge the numerical situation, though. Is it necessary, for some reason, to have the higher precision in this comparison?

EDIT: A quick benchmark using BenchmarkTools shows a 7x speedup when switching to the "naive" computation of invsqrt2.

@stevengj
Copy link
Member

stevengj commented Jan 29, 2025

Seems like this is a problem with IrrationalConstants.jl: JuliaMath/IrrationalConstants.jl#40

A simple workaround should be to replace abs(a) ≤ invsqrt2 with:

abs(a)  oftype(float(a), invsqrt2)

and similarly for b.

@devmotion
Copy link
Member

devmotion commented Jan 29, 2025

I opened JuliaMath/IrrationalConstants.jl#41 which seems to fix the underlying issue caused by the recent change in JuliaLang/julia#55886.

@stevengj
Copy link
Member

Thanks! We can close this issue once JuliaRegistries/General#123932 is merged, it seems.

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

No branches or pull requests

3 participants