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

Optimize Option<f64> and friends with nanboxing #31236

Closed
Yoric opened this issue Jan 27, 2016 · 7 comments
Closed

Optimize Option<f64> and friends with nanboxing #31236

Yoric opened this issue Jan 27, 2016 · 7 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Yoric
Copy link
Contributor

Yoric commented Jan 27, 2016

Once #5977 has landed, this will open up a few possible optimizations for simple enums.

One such optimization would be Option<f64> (and friends). For the moment, size_of<f64>() == 8 while size_of<Option<f64>>() == 16, but I believe that we could get it down to 8 by using nanboxing.

The idea of nanboxing is simple. In a 64-bits float, IEEE-754 actually specifies 2^52 - 1 different ways of representing NaN. Actual CPUs only use a single, which gives us 2^52 - 2 possible values that we could pick as representations for None. SpiderMonkey (and LuaJIT, and others) already uses this trick to hide dynamic type information in 64-bit values. I believe that this would work nicely in Rust, too.

@Yoric Yoric changed the title Optimize Option<f64> & co with nanboxing Optimize Option<f64> and friends with nanboxing Jan 27, 2016
@rphmeier
Copy link
Contributor

See also rust-lang/rfcs#1230.

@hanna-kruppe
Copy link
Contributor

It's currently legal (as in, nothing says otherwise and thus people rightly assume it's legal) to transmute arbitrary u64 values to f64. This proposal would probably require adding "a float NaN value other than the one with bit pattern [...]" to the list of invalid values in primitive types.

@jonas-schievink
Copy link
Contributor

What if I want to use NaN tagging in my application? Is that just not gonna happen?

@oli-obk
Copy link
Contributor

oli-obk commented Jan 27, 2016

Similar to NonZero for integrals, there could be NotNan as a wrapper for floats.

This issue should probably go to the rfc repository.

@ranma42
Copy link
Contributor

ranma42 commented Jan 27, 2016

NotNat might also be used as a hint for (some) LLVM fast math optimisations

@Aatch
Copy link
Contributor

Aatch commented Jan 30, 2016

I agree with @oli-obk on both points. Explicit opt-in is probably best here as NaN is a valid "value" for floats. A RFC for adding NonNan (or whatever) would be good to see.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@Mark-Simulacrum
Copy link
Member

Closing in favor of an RFC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants