Skip to content

Commit

Permalink
IO#write: return early if slice is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jun 26, 2018
1 parent 4547b6b commit dbafeac
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ module Crystal::System::FileDescriptor

private def unbuffered_write(slice : Bytes)
loop do
return if slice.empty?

bytes_written = LibC._write(@fd, slice, slice.size)
if bytes_written == -1
raise Errno.new("Error writing file")
end

slice += bytes_written
return if slice.size == 0
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/flate/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Flate::Writer < IO
def write(slice : Bytes)
check_open

return if slice.empty?

@stream.avail_in = slice.size
@stream.next_in = slice
consume_output LibZ::Flush::NO_FLUSH
Expand Down
2 changes: 2 additions & 0 deletions src/gzip/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Gzip::Writer < IO
def write(slice : Bytes) : Nil
check_open

return if slice.empty?

flate_io = write_header
flate_io.write(slice)

Expand Down
4 changes: 4 additions & 0 deletions src/http/server/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class HTTP::Server

# See `IO#write(slice)`.
def write(slice : Bytes)
return if slice.empty?

@output.write(slice)
end

Expand Down Expand Up @@ -159,6 +161,8 @@ class HTTP::Server
end

private def unbuffered_write(slice : Bytes)
return if slice.empty?

unless response.wrote_headers?
if response.version != "HTTP/1.0" && !response.headers.has_key?("Content-Length")
response.headers["Transfer-Encoding"] = "chunked"
Expand Down
2 changes: 2 additions & 0 deletions src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class HTTP::WebSocket::Protocol
end

def write(slice : Bytes)
return if slice.empty?

count = Math.min(@buffer.size - @pos, slice.size)
(@buffer + @pos).copy_from(slice.pointer(count), count)
@pos += count
Expand Down
2 changes: 2 additions & 0 deletions src/io/buffered.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ module IO::Buffered
def write(slice : Bytes)
check_open

return if slice.empty?

count = slice.size

if sync?
Expand Down
2 changes: 2 additions & 0 deletions src/io/hexdump.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class IO::Hexdump < IO
end

def write(buf : Bytes)
return if buf.empty?

@io.write(buf).tap do
@output.puts buf.hexdump if @write
end
Expand Down
2 changes: 2 additions & 0 deletions src/io/multi_writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class IO::MultiWriter < IO
def write(slice : Bytes)
check_open

return if slice.empty?

@writers.each { |writer| writer.write(slice) }
end

Expand Down
2 changes: 2 additions & 0 deletions src/io/stapled.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class IO::Stapled < IO
def write(slice : Bytes) : Nil
check_open

return if slice.empty?

@writer.write(slice)
end

Expand Down
30 changes: 17 additions & 13 deletions src/io/syscall.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,26 @@ module IO::Syscall
end

def write_syscall_helper(slice : Bytes, errno_msg : String) : Nil
loop do
bytes_written = yield slice
if bytes_written != -1
slice += bytes_written
return if slice.size == 0
else
if Errno.value == Errno::EAGAIN
wait_writable
return if slice.empty?

begin
loop do
bytes_written = yield slice
if bytes_written != -1
slice += bytes_written
return if slice.size == 0
else
raise Errno.new(errno_msg)
if Errno.value == Errno::EAGAIN
wait_writable
else
raise Errno.new(errno_msg)
end
end
end
end
ensure
if (writers = @writers) && !writers.empty?
add_write_event
ensure
if (writers = @writers) && !writers.empty?
add_write_event
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/openssl/digest/digest_io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module OpenSSL
end

def write(slice : Bytes)
return if slice.empty?

if @mode.write?
digest_algorithm.update(slice)
end
Expand Down
2 changes: 2 additions & 0 deletions src/openssl/ssl/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ abstract class OpenSSL::SSL::Socket < IO
def unbuffered_write(slice : Bytes)
check_open

return if slice.empty?

count = slice.size
bytes = LibSSL.ssl_write(@ssl, slice.pointer(count), count)
unless bytes > 0
Expand Down
2 changes: 2 additions & 0 deletions src/string/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class String::Builder < IO
end

def write(slice : Bytes)
return if slice.empty?

count = slice.size
new_bytesize = real_bytesize + count
if new_bytesize > @capacity
Expand Down
2 changes: 2 additions & 0 deletions src/zip/checksum_writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Zip
end

def write(slice : Bytes)
return if slice.empty?

@count += slice.size
@crc32 = CRC32.update(slice, @crc32) if @compute_crc32
io.write(slice)
Expand Down
2 changes: 2 additions & 0 deletions src/zlib/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Zlib::Writer < IO
def write(slice : Bytes) : Nil
check_open

return if slice.empty?

write_header unless @wrote_header

@flate_io.write(slice)
Expand Down

0 comments on commit dbafeac

Please sign in to comment.