File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -1257,6 +1257,14 @@ impl<T> Vec<T> {
1257
1257
#[ inline]
1258
1258
#[ stable( feature = "append" , since = "1.4.0" ) ]
1259
1259
pub fn append ( & mut self , other : & mut Self ) {
1260
+ // Reuses other if doing so allows us to turn a certain reallocation within self
1261
+ // into a potential, future reallocation in other.
1262
+ // The capacity limit ensures that we are not stealing a large preallocation from `other`
1263
+ // that is not commensurate with the avoided reallocation in self.
1264
+ if self . len == 0 && self . capacity ( ) < other. len && other. capacity ( ) / 2 <= other. len {
1265
+ mem:: swap ( self , other) ;
1266
+ return ;
1267
+ }
1260
1268
unsafe {
1261
1269
self . append_elements ( other. as_slice ( ) as _ ) ;
1262
1270
other. set_len ( 0 ) ;
You can’t perform that action at this time.
0 commit comments