Commit 805db965 authored by Niklas Hambüchen's avatar Niklas Hambüchen Committed by Ben Gamari
Browse files

Fix: hPutBuf issues unnecessary empty write syscalls for large writes (#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
parent 4e2e9b73
...@@ -745,11 +745,14 @@ bufWrite h_@Handle__{..} ptr count can_block = ...@@ -745,11 +745,14 @@ bufWrite h_@Handle__{..} ptr count can_block =
writeIORef haByteBuffer old_buf{ bufR = w + count } writeIORef haByteBuffer old_buf{ bufR = w + count }
return count return count
-- else, we have to flush -- else, we have to flush any existing handle buffer data
else do debugIO "hPutBuf: flushing first" -- and can then write out the data in `ptr` directly.
old_buf' <- Buffered.flushWriteBuffer haDevice old_buf else do -- No point flushing when there's nothing in the buffer.
-- TODO: we should do a non-blocking flush here when (w > 0) $ do
writeIORef haByteBuffer old_buf' 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 we can fit in the buffer, then just loop
if count < size if count < size
then bufWrite h_ ptr count can_block then bufWrite h_ ptr count can_block
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment