Memcpy generated for Vec::push(Default::default()) instead of initialization in-place #125632
Labels
A-codegen
Area: Code generation
C-optimization
Category: An issue highlighting optimization opportunities or PRs implementing such
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
I was investigating a performance issue in code that initializes some larger structs from a binary protocol. The code tries hard to avoid unnecessary moves, for example by first creating default values and then initializing them, instead of returning structs by value. After a slight change to one of the structs there was a significant performance drop, which boiled down to an unnecessary call to
memcpy
.Optimization of this memcpy seems to depend on the size of the struct and also on the ratio of zero-initialized fields to other fields.
In the following example,
std_push_default
initializes a struct on the stack and then callsmemcpy
to append it to the vector, whileinlined_push_default
initializes the struct in-place. The implementation of the latter should match the internals ofVec::push
.Using rustc 1.78 in the compiler explorer. This might be a regression from 1.76, or the thresholds are slightly different.
The text was updated successfully, but these errors were encountered: