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

Extend the Non-nullable pointer optimization to handle Option<Bucket<K, V>> #9378

Closed
bluss opened this issue Sep 21, 2013 · 1 comment · Fixed by #19765
Closed

Extend the Non-nullable pointer optimization to handle Option<Bucket<K, V>> #9378

bluss opened this issue Sep 21, 2013 · 1 comment · Fixed by #19765

Comments

@bluss
Copy link
Member

bluss commented Sep 21, 2013

The example is from hashmap, where a bucket is: struct Bucket<K,V> { hash: uint, key: K, value: V } and the hashmap keeps a vector of ~[Option<Bucket<K,V>>]

If this were a custom enum, the non-nullable pointer optimization could be used to eliminate the enum tag overhead if either K or V were such a non-nullable type.

Extend the optimization to handle a case like Option<Bucket> when possible.

Manual cusom enum was implemented in PR #9212 (not merged).

@huonw
Copy link
Member

huonw commented Feb 23, 2014

use std::mem;
struct Bucket<K,V> { x: uint, y: K, z: V }
fn main() {
     println!("{} {}",
              mem::size_of::<Bucket<~str, ~str>>(),
              mem::size_of::<Option<Bucket<~str, ~str>>>())
}

prints 24 32, i.e. not yet implemented.

bors added a commit that referenced this issue Dec 29, 2014
This extends the nullable enum opt to traverse beyond just the first level to find possible fields to use as the discriminant. So now, it'll work through structs, tuples, and fixed sized arrays. This also introduces a new lang item, NonZero, that you can use to wrap raw pointers or integral types to indicate to rustc that the underlying value is known to never be 0/NULL. We then use this in Vec, Rc and Arc to have them also benefit from the nullable enum opt.

As per rust-lang/rfcs#499 NonZero is not exposed via the `libstd` facade.

```
x86_64 Linux:
                        T       Option<T> (Before)      Option<T> (After)
----------------------------------------------------------------------------------
Vec<int>                24          32                      24
String                  24          32                      24
Rc<int>                 8           16                      8
Arc<int>                8           16                      8
[Box<int>, ..2]         16          24                      16
(String, uint)          32          40                      32
```

Fixes #19419.
Fixes #13194.
Fixes #9378.
Fixes #7576.
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 a pull request may close this issue.

2 participants