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

Boxed array initialisation with Box::new does not seem to optimise properly #41831

Closed
oyvindln opened this issue May 8, 2017 · 1 comment
Closed

Comments

@oyvindln
Copy link
Contributor

oyvindln commented May 8, 2017

One would expect that the compiler would be able to avoid putting a temporary array on the stack when creating a boxed array, at least in release mode. However this doesn't seem to be the case at the moment.

Looking at this example one would expect Box::new([100;100]) to produce a similar assembly output to using the unstable box syntax or a Vec. The assembly output suggests that when Box::new is used, a temporary array is created, filled in, and then an array is allocated on the heap, which and the values from the stack array are copied over with memcpy. This is obviously very suboptimal compared to the alternatives, which allocate on and write the values directly to the heap. What makes it even more confusing is that the implementation of Box::new consists purely of the line box x.

Now, in many situations, one could simply use Vec since it's more flexible anyhow. A boxed array does however seem to allow the compiler to use the size information to elude bounds checks in some cases where it can't with a Vec (e.g indexing an array larger than >255 with an u8 as usize (example). This means that it can be preferable in some situations, and the alternative would be potential slowdown, or having to use unsafe to index.

@oyvindln oyvindln changed the title Boxed array initialisation does not seem to optimise properly Boxed array initialisation with Box::new does not seem to optimise properly May 8, 2017
@nagisa
Copy link
Member

nagisa commented May 8, 2017

Duplicate of #41160

@nagisa nagisa closed this as completed May 8, 2017
victorporof added a commit to victorporof/rsx-parser that referenced this issue Dec 29, 2017
…ren and member expressions

Use box syntax because of rust-lang/rust#41831

Signed-off-by: Victor Porof <[email protected]>
victorporof added a commit to victorporof/rsx-parser that referenced this issue Dec 29, 2017
…ren and member expressions

Use box syntax because of rust-lang/rust#41831

Signed-off-by: Victor Porof <[email protected]>
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

2 participants