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 null-pointer enum optimization to nested enums #23228

Closed
bstrie opened this issue Mar 9, 2015 · 12 comments
Closed

Extend null-pointer enum optimization to nested enums #23228

bstrie opened this issue Mar 9, 2015 · 12 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@bstrie
Copy link
Contributor

bstrie commented Mar 9, 2015

fn main() {
    println!("{}", std::mem::size_of::<(Box<u8>, Box<u8>, Box<u8>)>());  // 24
    println!("{}", std::mem::size_of::<Option<Option<Option<(Box<u8>, Box<u8>, Box<u8>)>>>>());  // 40
}

Theoretically, Option<Option<Option<(Box<u8>, Box<u8>, Box<u8>)>>> should be able to leverage the null-pointer optimization such that it takes up no more space than (Box<u8>, Box<u8>, Box<u8>). Implement this for arbitrary levels of nesting.

@bstrie
Copy link
Contributor Author

bstrie commented Mar 9, 2015

cc @luqmana, @gankro, @cmr, @eddyb

@Gankra
Copy link
Contributor

Gankra commented Mar 9, 2015

Oh dang I thought this worked today. Been listening to too many pipedreams on internals.

@eddyb
Copy link
Member

eddyb commented Mar 9, 2015

@gankro well, actually, we already cover structural nesting, and the null-pointer enum optimization produces structures, so what's left is to also recurse through those - might be a bit hard to keep track of fields that are already "taken" - but everything else is there.
This might have contributed to vague or exaggerated claims.

@mdinger
Copy link
Contributor

mdinger commented Mar 10, 2015

Are these optimizations documented somewhere aside from the compiler internals? It seems like something that might come up fairly often though the list might get extremely long. I'm not sure.

@killercup
Copy link
Member

@mdinger The books has a paragraph on "nullable pointer optimization" in the FFI chapter.

@mdinger
Copy link
Contributor

mdinger commented Mar 10, 2015

@killercup Okay good.

@steveklabnik
Copy link
Member

Triage: no change.

@Mark-Simulacrum Mark-Simulacrum added I-slow Issue: Problems and improvements with respect to performance of generated code. and removed A-optimization labels Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@Emerentius
Copy link
Contributor

With rustc 1.26
std::mem::size_of::<Option<Option<Option<(Box<u8>, Box<u8>, Box<u8>)>>>>()
is now one word smaller at 32 bytes because of #45225

@eddyb You've stated in rust-lang/rfcs#1230 (comment) that further changes have tradeoffs that require RFCs.
This issue falls under two points in the closed one:

  • Use Multiple Invalid-Value Fields
  • Use all other fields as free bits when there exists another forbidden field

This should probably be closed then, right?

@SimonSapin
Copy link
Contributor

CC @rust-lang/wg-codegen

@nox
Copy link
Contributor

nox commented Jun 5, 2018

This requires more words to be initialised even in the case of None, and as far as I know that may lead to worse performance.

@bstrie
Copy link
Contributor Author

bstrie commented Oct 4, 2018

I'm perfectly willing to have this closed if someone on the compiler team confirms that this specific optimization requires too many tradeoffs to be valuable. Part of my original intent in filing this issue was to spur people to consider the theoretical limits of enum optimization, which was limited in 2015 but much more capable today.

@bstrie
Copy link
Contributor Author

bstrie commented Nov 14, 2019

Closing as per the rationale in my previous comment; Rust is far more capable of doing niche optimizations where it makes sense than back when this issue was originally filed in 2015, and it's unclear that this specific memory optimization is actually desirable.

@bstrie bstrie closed this as completed Nov 14, 2019
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. I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

10 participants