]> git.lizzy.rs Git - rust.git/commitdiff
Move small-copy optimization into copy_from_slice
authorRuud van Asseldonk <dev@veniogames.com>
Thu, 3 Nov 2016 23:20:11 +0000 (00:20 +0100)
committerRuud van Asseldonk <dev@veniogames.com>
Wed, 30 Nov 2016 10:09:29 +0000 (11:09 +0100)
Ultimately copy_from_slice is being a bottleneck, not io::Cursor::read.
It might be worthwhile to move the check here, so more places can
benefit from it.

src/libcore/slice.rs
src/libstd/io/cursor.rs

index a4a90e7a9da7a3bc80df580ea4b86bf25d054e5d..b238623eabaa7752de5c40a27a3174d9d37ed37d 100644 (file)
@@ -515,9 +515,19 @@ fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
     fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
         assert!(self.len() == src.len(),
                 "destination and source slices have different lengths");
-        unsafe {
-            ptr::copy_nonoverlapping(
-                src.as_ptr(), self.as_mut_ptr(), self.len());
+        // First check if the amount of elements we want to copy is small:
+        // `copy_nonoverlapping` will do a memcopy, which involves an indirect
+        // function call when `memcpy` is in the dynamically-linked libc. For
+        // small elements (such as a single byte or pointer), the overhead is
+        // significant. If the element is big then the assignment is a memcopy
+        // anyway.
+        if self.len() == 1 {
+            self[0] = src[0];
+        } else {
+            unsafe {
+                ptr::copy_nonoverlapping(
+                    src.as_ptr(), self.as_mut_ptr(), self.len());
+            }
         }
     }
 
index 9b50168a954b7b182aa4770cb616ddb6f435958e..1b5023380a7833c9adc13422817cb75d2b0098fb 100644 (file)
@@ -219,21 +219,9 @@ fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        // First check if the amount of bytes we want to read is small: the read
-        // in the else branch will end up calling `<&[u8] as Read>::read()`,
-        // which will copy the buffer using a memcopy. If we only want to read a
-        // single byte, then the overhead of the function call is significant.
-        let num_read = {
-            let mut inner_buf = self.fill_buf()?;
-            if buf.len() == 1 && inner_buf.len() > 0 {
-                buf[0] = inner_buf[0];
-                1
-            } else {
-                Read::read(&mut inner_buf, buf)?
-            }
-        };
-        self.pos += num_read as u64;
-        Ok(num_read)
+        let n = Read::read(&mut self.fill_buf()?, buf)?;
+        self.pos += n as u64;
+        Ok(n)
     }
 }