Skip to content

[slow_vector_initialization]: clarify why Vec::new() + resize is worse #11310

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

Merged
merged 3 commits into from
Aug 9, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Aug 9, 2023

#11198 extended this lint to also warn on Vec::new() + resize(0, len), but did not update the lint documentation, so it left some confused (#10938 (comment)).
This PR should make it a bit more clear. (cc @djc @vi what do you think about this?)

More details

Godbolt for Vec::new() + .resize(x, 0): https://godbolt.org/z/e7q9xc9rG

The resize call first does a normal allocation (__rust_alloc):

alloc::raw_vec::finish_grow:
  ...
  cmp     qword ptr [rcx + 8], 0
  je      .LBB1_7  ; if capacity == 0 -> LBB1_7

.LBB1_7:
  ...
  call    qword ptr [rip + __rust_alloc@GOTPCREL]

Then a memset for zero initialization:

example::f:
  ...
  xor     esi, esi  ; 0
  call    qword ptr [rip + memset@GOTPCREL]

Godbolt for vec![0; len]: https://godbolt.org/z/M3vr53vWY

Important bit:

example::f:
  ...
  call    qword ptr [rip + __rust_alloc_zeroed@GOTPCREL]

changelog: [slow_vector_initialization]: clarify why Vec::new() + resize is worse than vec![0; len]

@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2023

r? @xFrednet

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 9, 2023
@vi
Copy link
Contributor

vi commented Aug 9, 2023

@vi what do you think about this?

It makes it clear that advantage is because of zero value. I though that zero values matter for .bss vs .data, but allocation optimisations based on zero values are maybe less typical yet.

Is the lint inhibited for Vec::new() + resize(len, 1), i.e. when it is statically known that the value is not zero, so vector initialisation needs to be an explicit step anyway? If not, the help message may be edited.

Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks!

@y21
Copy link
Member Author

y21 commented Aug 9, 2023

Is the lint inhibited for Vec::new() + resize(len, 1), i.e. when it is statically known that the value is not zero, so vector initialisation needs to be an explicit step anyway? If not, the help message may be edited.

It only lints if the value argument in .resize(len, val) (or .extend(repeat(val).take(len))) is the integer literal "0", so yep it wouldn't lint for .resize(len, 1)

@djc
Copy link
Contributor

djc commented Aug 9, 2023

Yup, looks great to me!

@xFrednet
Copy link
Member

xFrednet commented Aug 9, 2023

LGTM, thank you for the edit and review support @vi & @djc

bors r=xFrednet,djc

@xFrednet
Copy link
Member

xFrednet commented Aug 9, 2023

I forgot the @ ^^

@bors r=xFrednet,djc

@bors
Copy link
Contributor

bors commented Aug 9, 2023

📌 Commit dd25cc3 has been approved by xFrednet,djc

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 9, 2023

⌛ Testing commit dd25cc3 with merge add2722...

@bors
Copy link
Contributor

bors commented Aug 9, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet,djc
Pushing add2722 to master...

@bors bors merged commit add2722 into rust-lang:master Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants