2007-04-16 Jeffrey Stedfast <fejj@gnome.org>
authorJeffrey Stedfast <fejj@novell.com>
Tue, 17 Apr 2007 03:33:59 +0000 (03:33 -0000)
committerJeffrey Stedfast <fejj@novell.com>
Tue, 17 Apr 2007 03:33:59 +0000 (03:33 -0000)
* CStreamWriter.cs (Write): Instead of writing 1 char at a time,
copy the bytes into a temporary char array (with a fixed max size)
so that we can minimize the number of Write() calls we make on the
underlying stream (and thus on the write() system call).

svn path=/trunk/mcs/; revision=75806

mcs/class/corlib/System/CStreamWriter.cs
mcs/class/corlib/System/ChangeLog

index c9d77f0ca68e23bdd3540cc3d0c87050bd360096..5ce0af8a403eaf955f925145958fcdaaa91f5982 100644 (file)
@@ -49,23 +49,50 @@ namespace System.IO {
                {
                        if (count <= 0)
                                return;
-
-                       if (driver.Initialized){
-                               lock (this){
-                                       for (int i = 0; i < count; i++) {
-                                               try {
-                                                       char val = buffer [index + i];
-                                                       if (driver.NotifyWrite (val))
-                                                               InternalWriteChar (val);
-                                               } catch (IOException){
-                                               }
-                                       }
-                               }
-                       } else {
+                       
+                       if (!driver.Initialized) {
                                try {
                                        base.Write (buffer, index, count);
-                               } catch (IOException){
+                               } catch (IOException) {
                                }
+                               
+                               return;
+                       }
+                       
+                       lock (this) {
+                               int i = 0, j = 0;
+                               char []buf;
+                               char c;
+                               
+                               // The idea here is that we want to limit our temp
+                               // buffer size - I picked 1k because the underlying
+                               // stream implementation seems to break writes into
+                               // 1k byte chunks.
+                               if (count > 1024)
+                                       buf = new char[1024];
+                               else
+                                       buf = new char[count];
+                               
+                               do {
+                                       do {
+                                               c = buffer [index + i++];
+                                               
+                                               if (driver.NotifyWrite (c)) {
+                                                       buf[j++] = c;
+                                                       break;
+                                               }
+                                       } while (i < count);
+                                       
+                                       if (j == buf.Length || i == count) {
+                                               // temp buffer is full, write it out
+                                               try {
+                                                       base.Write (buf, 0, j);
+                                               } catch (IOException) {
+                                               }
+                                               
+                                               j = 0;
+                                       }
+                               } while (i < count);
                        }
                }
 
index b16411858caf764a4cb45f5f2774bb9009a280f5..bf45887af9c6f27eb6e51c3ea68f63b086b1b3ad 100644 (file)
@@ -1,3 +1,10 @@
+2007-04-16  Jeffrey Stedfast  <fejj@gnome.org>
+
+       * CStreamWriter.cs (Write): Instead of writing 1 char at a time,
+       copy the bytes into a temporary char array (with a fixed max size)
+       so that we can minimize the number of Write() calls we make on the
+       underlying stream (and thus on the write() system call).
+
 2007-04-17  Alp Toker  <alp@atoker.com>
 
        * Array.cs: Make GetRank() icall private. Subclasses should use the