c12d3a8b3e
std::string data{istreambuf_iterator{stream}, istreambuf_iterator{}}
copies a file one character at a time (each increment pays a stream
buffer-boundary check), instead of one bulk read. For small models this
is noise; for a large one it is seconds -- measured costing the bulk of
a ~16s gap between onnx-optimizer's path-based loadModel/saveModel and
the equivalent Python-side onnx.load on an 833MB model
(https://github.com/onnxsim/onnxsim/issues/633's investigation into
loadModel's path-based entry points, used by its own SimplifyPath fast
path).
Sizes the file up front via std::filesystem::file_size and does a single
read() into a pre-sized string; falls back to the old iterator-based
read if the size can't be determined (e.g. a pipe). Correctness is
checked via gcount() rather than the stream's good()/eof() flags, since
a read() that consumes exactly to EOF can set eofbit on a stream
implementation even though every requested byte was read.
(cherry picked from commit 4784075aa7d40a771eaf70c12d0eeaee3d5d3a17)
### Motivation and Context
Fixes #
Signed-off-by: take-cheeze <takechi101010@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>