Improve FileStream block reading performance little bit
authorMarek Safar <marek.safar@gmail.com>
Tue, 25 Jan 2011 17:55:38 +0000 (17:55 +0000)
committerMarek Safar <marek.safar@gmail.com>
Wed, 26 Jan 2011 08:59:11 +0000 (08:59 +0000)
mcs/class/corlib/System.IO/FileStream.cs

index 2073d7fc8baddc39c25660e3424bfac8039b0f67..86d5d59e95083cdcd507196dfcd1f0dfe64b55b9 100644 (file)
@@ -535,27 +535,20 @@ namespace System.IO
 
                int ReadInternal (byte [] dest, int offset, int count)
                {
-                       int copied = 0;
-
                        int n = ReadSegment (dest, offset, count);
-                       copied += n;
-                       count -= n;
-                       
-                       if (count == 0) {
-                               /* If there was already enough
-                                * buffered, no need to read
-                                * more from the file.
-                                */
-                               return (copied);
+                       if (n == count) {
+                               return count;
                        }
-
+                       
+                       int copied = n;
+                       count -= n;
                        if (count > buf_size) {
                                /* Read as much as we can, up
                                 * to count bytes
                                 */
                                FlushBuffer();
                                n = ReadData (handle, dest,
-                                             offset+copied,
+                                             offset+n,
                                              count);
                        
                                /* Make the next buffer read
@@ -569,9 +562,7 @@ namespace System.IO
                                                 count);
                        }
 
-                       copied += n;
-
-                       return copied;
+                       return copied + n;
                }
 
                delegate int ReadDelegate (byte [] buffer, int offset, int count);
@@ -971,18 +962,15 @@ namespace System.IO
 
                private int ReadSegment (byte [] dest, int dest_offset, int count)
                {
-                       if (count > buf_length - buf_offset) {
-                               count = buf_length - buf_offset;
-                       }
+                       count = Math.Min (count, buf_length - buf_offset);
                        
                        if (count > 0) {
-                               Buffer.BlockCopy (buf, buf_offset,
-                                                 dest, dest_offset,
-                                                 count);
+                               // Use the fastest method, all range checks has been done
+                               Buffer.BlockCopyInternal (buf, buf_offset, dest, dest_offset, count);
                                buf_offset += count;
                        }
                        
-                       return(count);
+                       return count;
                }
 
                private int WriteSegment (byte [] src, int src_offset,