]> git.lizzy.rs Git - rust.git/commitdiff
Move small-copy optimization into <&[u8] as Read>
authorRuud van Asseldonk <dev@veniogames.com>
Sat, 12 Nov 2016 14:58:58 +0000 (15:58 +0100)
committerRuud van Asseldonk <dev@veniogames.com>
Wed, 30 Nov 2016 10:09:29 +0000 (11:09 +0100)
Based on the discussion in https://github.com/rust-lang/rust/pull/37573,
it is likely better to keep this limited to std::io, instead of
modifying a function which users expect to be a memcpy.

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

index b238623eabaa7752de5c40a27a3174d9d37ed37d..a4a90e7a9da7a3bc80df580ea4b86bf25d054e5d 100644 (file)
@@ -515,19 +515,9 @@ 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");
-        // 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());
-            }
+        unsafe {
+            ptr::copy_nonoverlapping(
+                src.as_ptr(), self.as_mut_ptr(), self.len());
         }
     }
 
index 6b26c016638a7e2c9df7fecca7daec162e93041c..f691289811bc6514d57262cf753f778e6a5af0ae 100644 (file)
@@ -157,7 +157,16 @@ impl<'a> Read for &'a [u8] {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
         let amt = cmp::min(buf.len(), self.len());
         let (a, b) = self.split_at(amt);
-        buf[..amt].copy_from_slice(a);
+
+        // First check if the amount of bytes we want to read is small:
+        // `copy_from_slice` will generally expand to a call to `memcpy`, and
+        // for a single byte the overhead is significant.
+        if amt == 1 {
+            buf[0] = a[0];
+        } else {
+            buf[..amt].copy_from_slice(a);
+        }
+
         *self = b;
         Ok(amt)
     }
@@ -169,7 +178,16 @@ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
                                   "failed to fill whole buffer"));
         }
         let (a, b) = self.split_at(buf.len());
-        buf.copy_from_slice(a);
+
+        // First check if the amount of bytes we want to read is small:
+        // `copy_from_slice` will generally expand to a call to `memcpy`, and
+        // for a single byte the overhead is significant.
+        if buf.len() == 1 {
+            buf[0] = a[0];
+        } else {
+            buf.copy_from_slice(a);
+        }
+
         *self = b;
         Ok(())
     }