File tree Expand file tree Collapse file tree 2 files changed +16
-18
lines changed Expand file tree Collapse file tree 2 files changed +16
-18
lines changed Original file line number Diff line number Diff line change @@ -515,9 +515,19 @@ impl<T> SliceExt for [T] {
515515 fn copy_from_slice ( & mut self , src : & [ T ] ) where T : Copy {
516516 assert ! ( self . len( ) == src. len( ) ,
517517 "destination and source slices have different lengths" ) ;
518- unsafe {
519- ptr:: copy_nonoverlapping (
520- src. as_ptr ( ) , self . as_mut_ptr ( ) , self . len ( ) ) ;
518+ // First check if the amount of elements we want to copy is small:
519+ // `copy_nonoverlapping` will do a memcopy, which involves an indirect
520+ // function call when `memcpy` is in the dynamically-linked libc. For
521+ // small elements (such as a single byte or pointer), the overhead is
522+ // significant. If the element is big then the assignment is a memcopy
523+ // anyway.
524+ if self . len ( ) == 1 {
525+ self [ 0 ] = src[ 0 ] ;
526+ } else {
527+ unsafe {
528+ ptr:: copy_nonoverlapping (
529+ src. as_ptr ( ) , self . as_mut_ptr ( ) , self . len ( ) ) ;
530+ }
521531 }
522532 }
523533
Original file line number Diff line number Diff line change @@ -219,21 +219,9 @@ impl<T> io::Seek for Cursor<T> where T: AsRef<[u8]> {
219219#[ stable( feature = "rust1" , since = "1.0.0" ) ]
220220impl < T > Read for Cursor < T > where T : AsRef < [ u8 ] > {
221221 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
222- // First check if the amount of bytes we want to read is small: the read
223- // in the else branch will end up calling `<&[u8] as Read>::read()`,
224- // which will copy the buffer using a memcopy. If we only want to read a
225- // single byte, then the overhead of the function call is significant.
226- let num_read = {
227- let mut inner_buf = self . fill_buf ( ) ?;
228- if buf. len ( ) == 1 && inner_buf. len ( ) > 0 {
229- buf[ 0 ] = inner_buf[ 0 ] ;
230- 1
231- } else {
232- Read :: read ( & mut inner_buf, buf) ?
233- }
234- } ;
235- self . pos += num_read as u64 ;
236- Ok ( num_read)
222+ let n = Read :: read ( & mut self . fill_buf ( ) ?, buf) ?;
223+ self . pos += n as u64 ;
224+ Ok ( n)
237225 }
238226}
239227
You can’t perform that action at this time.
0 commit comments