Skip to content

Commit

Permalink
Avoid double file buffering (#13780)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
  • Loading branch information
carlhoerberg and straight-shoota authored Sep 22, 2023
1 parent e3d79fa commit 4099c7f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/crystal/system/unix/getrandom.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Crystal::System::Random
return unless urandom.info.type.character_device?

urandom.close_on_exec = true
urandom.sync = true # don't buffer bytes
urandom.read_buffering = false # don't buffer bytes
@@urandom = urandom
end
end
Expand Down
2 changes: 2 additions & 0 deletions src/digest/digest.cr
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ abstract class Digest
# Reads the file's content and updates the digest with it.
def file(file_name : Path | String) : self
File.open(file_name) do |io|
# `#update` works with big buffers so there's no need for additional read buffering in the file
io.read_buffering = false
self << io
end
end
Expand Down
2 changes: 2 additions & 0 deletions src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,10 @@ class File < IO::FileDescriptor
open(filename, mode, perm, encoding: encoding, invalid: invalid) do |file|
case content
when Bytes
file.sync = true
file.write(content)
when IO
file.sync = true
IO.copy(content, file)
else
file.print(content)
Expand Down

0 comments on commit 4099c7f

Please sign in to comment.