Skip to content

Commit

Permalink
Fix: hPutBuf issues unnecessary empty write syscalls for large writes…
Browse files Browse the repository at this point in the history
… (#13246)

Until now, any `hPutBuf` that wrote `>= the handle buffer size` would
trigger an unnecessary `write("")` system call before the actual write
system call.

This is fixed by making sure that we never flush an empty handle buffer:
Only flush `when (w > 0)`.

Reviewers: simonmar, austin, hvr, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D3119
  • Loading branch information
nh2 authored and bgamari committed Feb 12, 2017
1 parent 4e2e9b7 commit 805db96
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions libraries/base/GHC/IO/Handle/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,14 @@ bufWrite h_@Handle__{..} ptr count can_block =
writeIORef haByteBuffer old_buf{ bufR = w + count }
return count

-- else, we have to flush
else do debugIO "hPutBuf: flushing first"
old_buf' <- Buffered.flushWriteBuffer haDevice old_buf
-- TODO: we should do a non-blocking flush here
writeIORef haByteBuffer old_buf'
-- else, we have to flush any existing handle buffer data
-- and can then write out the data in `ptr` directly.
else do -- No point flushing when there's nothing in the buffer.
when (w > 0) $ do
debugIO "hPutBuf: flushing first"
flushed_buf <- Buffered.flushWriteBuffer haDevice old_buf
-- TODO: we should do a non-blocking flush here
writeIORef haByteBuffer flushed_buf
-- if we can fit in the buffer, then just loop
if count < size
then bufWrite h_ ptr count can_block
Expand Down

0 comments on commit 805db96

Please sign in to comment.