Conversation
Reuse existing element buffers and copy trivial elements in bulk.
|
r? @jhpratt rustbot has assigned @jhpratt. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| assert_eq!(d.len(), 4); | ||
| let mut e = d.clone(); | ||
| assert_eq!(e.len(), 4); | ||
| while !d.is_empty() { | ||
| assert_eq!(d.pop_back(), e.pop_back()); |
There was a problem hiding this comment.
why did you modify this test?
There was a problem hiding this comment.
hhmm that's a good question. I think I was trying to match the Vec tests closer at some point but accidentally remove this. I don't really see a good reason to remove it so I guess I'll bring it back.
|
I'm going to close this PR as the LLM usage you disclosed is not in line with1 our LLM usage policy. You are welcome to open additional PRs as long as they follow our policy. For additional information, see the guidelines in rustc-dev-guide. Footnotes
|
This PR adds two optimizations to
VecDeque'scloneandclone_fromThe first restores allocation reuse for elements in clone_from. While looking through the commit history, I noticed that the rewrite in #102991 had removed the optimization introduced in the original clone_from. Instead of clearing the destination and cloning every element again, we now call
clone_fromon existing elements and clones any remaining source elements.The only "trade-off" is that it can retain spare capacity in the destination elements for types such as
StringandVecwhereas the current implementation drops their allocations. This is Vec's current behavior though. Callers can useshrink_to_fiton individual elements if needed.The second one adds a TrivialClone specialization for both
cloneandclone_from. It copies the source slices directly into the destination buffer instead of going through iterators.ns/iteration on an AMD Ryzen 9 5950X:
mainclone_u64_contiguousclone_u64_wrappedclone_u64_small_wrappedclone_string_contiguousclone_from_u64_contiguousclone_from_u64_wrappedclone_from_string_contiguousclone_from_string_wrappedThe
clone_u64_small_wrappedweird result seems to be cause by either code-layout or weird glibc things. Running that benchmark alone by itself gives:15.73 → 15.52 ns (−1.3%)LLM disclosure: I used LLMs to review my implementation and to initially generate the tests and benchmarks which I then manually modified. I have personally reviewed these and I'm currently satisfied with them. The comments are my own except for the SAFETY comments which were corrected by an LLM.