Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the null pointer optimization in Rust?

Tags:

rust

In Learning Rust With Entirely Too Many Linked Lists, the author mentions:

However, if we have a special kind of enum:

enum Foo {     A,     B(ContainsANonNullPtr), } 

the null pointer optimization kicks in, which eliminates the space needed for the tag. If the variant is A, the whole enum is set to all 0's. Otherwise, the variant is B. This works because B can never be all 0's, since it contains a non-zero pointer.

I guess that the author is saying that (assuming A is 4 bits, and B is 4 bits)

let test = Foo::A 

the memory layout is

0000 0000 

but

let test = Foo::B 

the memory layout is

some 8 bit non 0 value 

What exactly is optimized here? Aren't both representation always 8 bits What does it mean when the author claims

It means &, &mut, Box, Rc, Arc, Vec, and several other important types in Rust have no overhead when put in an Option

like image 511
Jal Avatar asked Oct 04 '17 05:10

Jal


People also ask

Can a raw pointer be null in rust?

See also the std::ptr module. Working with raw pointers in Rust is uncommon, typically limited to a few patterns. Raw pointers can be unaligned or null. However, when a raw pointer is dereferenced (using the * operator), it must be non-null and aligned.

Can rust pointers be larger than isize Max?

Rust types are never larger than isize::MAX and Rust allocations never wrap around the address space, so two pointers within some value of any Rust type T will always satisfy the last two conditions. The standard library also generally ensures that allocations never reach a size where an offset is a concern.

How does null pointer optimization work with ENUM variants?

the null pointer optimization kicks in, which eliminates the space needed for the tag. If the variant is A, the whole enum is set to all 0 's. Otherwise, the variant is B. This works because B can never be all 0 's, since it contains a non-zero pointer. I guess that the author is saying that (assuming A is 4 bits, and B is 4 bits)

What does unsized return when a pointer is null?

Sized , [src] [ −] Returns true if the pointer is null. Note that unsized types have many possible null pointers, as only the raw data pointer is considered, not their length, vtable, etc. Therefore, two pointers that are null may still not compare equal to each other.


1 Answers

The null pointer optimization basically means that if you have an enum with two variants, where one variant has no associated data, and the other variant has associated data where the bit pattern of all zeros isn't a valid value, then the enum itself will take exactly the same amount of space as that associated value, using the all zeroes bit pattern to indicate that it's the other variant.

In other words, this means that Option<&T> is exactly the same size as &T instead of requiring an extra word.

like image 111
Lily Ballard Avatar answered Oct 22 '22 02:10

Lily Ballard