improve file reading speeds

This commit is contained in:
Green Sky 2023-08-15 21:49:01 +02:00
parent d2d6cfbf53
commit c73e429df1
No known key found for this signature in database
2 changed files with 23 additions and 2 deletions

View File

@ -3,6 +3,8 @@
#include "./file.hpp"
#include <fstream>
#include <ios>
#include <limits>
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<uint8_t> 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<uint8_t> chunk(size);
const auto nread = _file.read(reinterpret_cast<char*>(chunk.data()), chunk.size()).gcount();
if (nread != std::numeric_limits<std::streamsize>::max()) {
chunk.resize(nread); // usually a noop
} else {
chunk.clear();
}
#endif
_bytes_read += chunk.size();

View File

@ -3,6 +3,7 @@
#include "./file.hpp"
#include <fstream>
#include <limits>
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<uint8_t> 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<uint8_t> chunk(size);
const auto nread = _file.read(reinterpret_cast<char*>(chunk.data()), chunk.size()).gcount();
if (nread != std::numeric_limits<std::streamsize>::max()) {
chunk.resize(nread); // usually a noop
} else {
chunk.clear();
}
#endif
_bytes_read += chunk.size();