From c73e429df138ce32f5cdb5588a3af8257a946680 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 15 Aug 2023 21:49:01 +0200 Subject: [PATCH] improve file reading speeds --- solanaceae/message3/file_r_file.hpp | 13 ++++++++++++- solanaceae/message3/file_rw_file.hpp | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/solanaceae/message3/file_r_file.hpp b/solanaceae/message3/file_r_file.hpp index 27e7ffb..fccdebf 100644 --- a/solanaceae/message3/file_r_file.hpp +++ b/solanaceae/message3/file_r_file.hpp @@ -3,6 +3,8 @@ #include "./file.hpp" #include +#include +#include struct FileRFile : public FileI { std::ifstream _file; @@ -35,12 +37,21 @@ struct FileRFile : public FileI { // TODO: error check _file.seekg(pos); - // TODO: optimize +#if 0 std::vector chunk; int read_char; for (size_t i = 0; i < size && (_file_size == 0 || i+pos < _file_size) && (read_char = _file.get()) != std::ifstream::traits_type::eof(); i++) { chunk.push_back(read_char); } +#else + std::vector chunk(size); + const auto nread = _file.read(reinterpret_cast(chunk.data()), chunk.size()).gcount(); + if (nread != std::numeric_limits::max()) { + chunk.resize(nread); // usually a noop + } else { + chunk.clear(); + } +#endif _bytes_read += chunk.size(); diff --git a/solanaceae/message3/file_rw_file.hpp b/solanaceae/message3/file_rw_file.hpp index 8bb0d58..43604a7 100644 --- a/solanaceae/message3/file_rw_file.hpp +++ b/solanaceae/message3/file_rw_file.hpp @@ -3,6 +3,7 @@ #include "./file.hpp" #include +#include struct FileRWFile : public FileI { std::fstream _file; @@ -29,12 +30,21 @@ struct FileRWFile : public FileI { // TODO: error check _file.seekg(pos); - // TODO: optimize +#if 0 std::vector chunk; int read_char; for (size_t i = 0; i < size && (_file_size == 0 || i+pos < _file_size) && (read_char = _file.get()) != std::ifstream::traits_type::eof(); i++) { chunk.push_back(read_char); } +#else + std::vector chunk(size); + const auto nread = _file.read(reinterpret_cast(chunk.data()), chunk.size()).gcount(); + if (nread != std::numeric_limits::max()) { + chunk.resize(nread); // usually a noop + } else { + chunk.clear(); + } +#endif _bytes_read += chunk.size();