File tree Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -760,9 +760,20 @@ impl<'a, T: Clone> CloneableVector<T> for &'a [T] {
760
760
/// Returns a copy of `v`.
761
761
#[ inline]
762
762
fn to_owned ( & self ) -> ~[ T ] {
763
- let mut result = with_capacity ( self . len ( ) ) ;
764
- for e in self . iter ( ) {
765
- result. push ( ( * e) . clone ( ) ) ;
763
+ let len = self . len ( ) ;
764
+ let mut result = with_capacity ( len) ;
765
+ unsafe {
766
+ // Unsafe code so this can be optimised to a memcpy (or something
767
+ // similarly fast) when T is Copy. LLVM is easily confused, so any
768
+ // extra operations during the loop can prevent this optimisation
769
+ result. set_len ( len) ;
770
+ let mut i = 0 ;
771
+ while i < len {
772
+ mem:: move_val_init (
773
+ result. unsafe_mut_ref ( i) ,
774
+ self . unsafe_ref ( i) . clone ( ) ) ;
775
+ i = i + 1 ;
776
+ }
766
777
}
767
778
result
768
779
}
@@ -2584,7 +2595,8 @@ pub mod bytes {
2584
2595
impl < A : Clone > Clone for ~[ A ] {
2585
2596
#[ inline]
2586
2597
fn clone ( & self ) -> ~[ A ] {
2587
- self . iter ( ) . map ( |item| item. clone ( ) ) . collect ( )
2598
+ // Use the fast to_owned on &[A] for cloning
2599
+ self . as_slice ( ) . to_owned ( )
2588
2600
}
2589
2601
2590
2602
fn clone_from ( & mut self , source : & ~[ A ] ) {
Original file line number Diff line number Diff line change @@ -311,7 +311,26 @@ impl<T: Clone> Vec<T> {
311
311
312
312
impl < T : Clone > Clone for Vec < T > {
313
313
fn clone ( & self ) -> Vec < T > {
314
- self . iter ( ) . map ( |x| x. clone ( ) ) . collect ( )
314
+ let len = self . len ;
315
+ let mut vector = Vec :: with_capacity ( len) ;
316
+ vector. len = len;
317
+ // Unsafe code so this can be optimised to a memcpy (or something
318
+ // similarly fast) when T is Copy. LLVM is easily confused, so any
319
+ // extra operations during the loop can prevent this optimisation
320
+ {
321
+ let slice = vector. as_mut_slice ( ) ;
322
+ let this_slice = self . as_slice ( ) ;
323
+ let mut i = 0 ;
324
+ while i < len {
325
+ unsafe {
326
+ mem:: move_val_init (
327
+ slice. unsafe_mut_ref ( i) ,
328
+ this_slice. unsafe_ref ( i) . clone ( ) ) ;
329
+ }
330
+ i = i + 1 ;
331
+ }
332
+ }
333
+ vector
315
334
}
316
335
317
336
fn clone_from ( & mut self , other : & Vec < T > ) {
You can’t perform that action at this time.
0 commit comments