-1
$\begingroup$

Does the answer depend on the number of bytes requested? If it is forced to keep a buffer due to some unknown device/diver, can it optimize and skip the buffer if it's going to be a disk read? I'm looking for any insight beyond "It's implementation dependent".

How I got here:

C++ implementations of POSIX compliant systems can bypass the basic_streambuf's process local buffers by directly calling the read() system call.

https://en.cppreference.com/w/cpp/io/basic_istream/read : When using a non-converting locale (the default locale is non-converting), the overrider of this function in std::basic_ifstream may be optimized for zero-copy bulk I/O (by means of overriding std::streambuf::xsgetn)

https://en.cppreference.com/w/cpp/io/basic_streambuf/sgetn : The rule about "more efficient implementations" permits bulk I/O without intermediate buffering: that's how std::ifstream::read simply passes the pointer to the POSIX read() system call in some implementations of iostreams

But the manual for read() (https://man7.org/linux/man-pages/man2/read.2.html) provides only input/output descriptions and does not say anything about implementation/guarantees provided by the system call. While reasonable for a standard to not be too heavy handed, it still raises the question - how is it actually implemented? And is it even an optimization over not having a buffer in std::basic_streambuf if the system call has a buffer anyway?

$\endgroup$
1

1 Answer 1

1
$\begingroup$

ssize_t read(int fd, void *buf, size_t count);

This asks the OS to copy into buf, all available data, but no more than count bytes.

For storage devices where seeking isn't practical or possible (e.g. tape drives and punched card readers), it is the caller's responsibility to ensure that the provided buffer is large enough to contain a full physical block from the device. Typically one would provide a buffer that's too large, and consider it an error if the full amount was read (as there might be additional data that was discarded).

Whether the OS, or the filesystem, or the storage device itself decide to use a buffer to read-ahead in order to optimize future requests should be irrelevant as far as the caller is concerned.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.