Always use 8k buffers instead of BUFSIZ
authorSimon Marlow <marlowsd@gmail.com>
Tue, 21 Dec 2010 15:51:54 +0000 (15:51 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Tue, 21 Dec 2010 15:51:54 +0000 (15:51 +0000)
This makes a huge difference to I/O performance for me on Windows,
where BUFSIZ is 512.  It might help on Mac too.

GHC/IO/FD.hs
GHC/IO/Handle/Internals.hs

index 659f610..f2ce275 100644 (file)
@@ -99,8 +99,15 @@ instance GHC.IO.Device.IODevice FD where
   dup           = dup
   dup2          = dup2
 
+-- We used to use System.Posix.Internals.dEFAULT_BUFFER_SIZE, which is
+-- taken from the value of BUFSIZ on the current platform.  This value
+-- varies too much though: it is 512 on Windows, 1024 on OS X and 8192
+-- on Linux.  So let's just use a decent size on every platform:
+dEFAULT_FD_BUFFER_SIZE :: Int
+dEFAULT_FD_BUFFER_SIZE = 8096
+
 instance BufferedIO FD where
-  newBuffer _dev state = newByteBuffer dEFAULT_BUFFER_SIZE state
+  newBuffer _dev state = newByteBuffer dEFAULT_FD_BUFFER_SIZE state
   fillReadBuffer    fd buf = readBuf' fd buf
   fillReadBuffer0   fd buf = readBufNonBlocking fd buf
   flushWriteBuffer  fd buf = writeBuf' fd buf
index 2aad185..76e8b00 100644 (file)
@@ -390,7 +390,7 @@ handleFinalizer fp m = do
 -- using an 8k char buffer instead of 32k improved performance for a
 -- basic "cat" program by ~30% for me.  --SDM
 dEFAULT_CHAR_BUFFER_SIZE :: Int
-dEFAULT_CHAR_BUFFER_SIZE = dEFAULT_BUFFER_SIZE `div` 4
+dEFAULT_CHAR_BUFFER_SIZE = 2048 -- 8k/sizeof(HsChar)
 
 getCharBuffer :: IODevice dev => dev -> BufferState
               -> IO (IORef CharBuffer, BufferMode)