Set file stream buffers to pre-allocated 1MB chunk on web builds
This commit is contained in:
parent
32bbc93eea
commit
ae3b91e7b1
2 changed files with 46 additions and 10 deletions
|
|
@ -4,19 +4,40 @@
|
|||
|
||||
#include <filesystem>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
|
||||
namespace psemek::io
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
struct buffer_hook
|
||||
{
|
||||
static constexpr std::size_t buffer_size = 1 << 20; // 1 MB
|
||||
std::unique_ptr<char[]> buffer{new char[buffer_size]};
|
||||
|
||||
void apply(FILE * file)
|
||||
{
|
||||
std::setvbuf(file, buffer.get(), _IOFBF, buffer_size);
|
||||
}
|
||||
};
|
||||
#else
|
||||
struct buffer_hook
|
||||
{
|
||||
void apply(FILE *) {}
|
||||
};
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
struct file_istream
|
||||
: istream
|
||||
{
|
||||
file_istream() = default;
|
||||
|
||||
file_istream(FILE * file)
|
||||
: file_{file}
|
||||
{}
|
||||
|
||||
file_istream(FILE * file);
|
||||
file_istream(std::filesystem::path const & path);
|
||||
|
||||
file_istream(file_istream && s)
|
||||
|
|
@ -49,6 +70,7 @@ namespace psemek::io
|
|||
|
||||
private:
|
||||
FILE * file_ = nullptr;
|
||||
[[no_unique_address]] detail::buffer_hook buffer_hook_;
|
||||
};
|
||||
|
||||
struct file_ostream
|
||||
|
|
@ -58,10 +80,7 @@ namespace psemek::io
|
|||
|
||||
file_ostream() = default;
|
||||
|
||||
file_ostream(FILE * file)
|
||||
: file_{file}
|
||||
{}
|
||||
|
||||
file_ostream(FILE * file);
|
||||
file_ostream(std::filesystem::path const & path, unsigned flags = 0);
|
||||
|
||||
file_ostream(file_ostream && s)
|
||||
|
|
@ -94,6 +113,7 @@ namespace psemek::io
|
|||
|
||||
private:
|
||||
FILE * file_ = nullptr;
|
||||
[[no_unique_address]] detail::buffer_hook buffer_hook_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,9 +69,17 @@ namespace psemek::io
|
|||
|
||||
}
|
||||
|
||||
file_istream::file_istream(FILE * file)
|
||||
: file_{file}
|
||||
{
|
||||
buffer_hook_.apply(file_);
|
||||
}
|
||||
|
||||
file_istream::file_istream(std::filesystem::path const & path)
|
||||
: file_{safe_fopen(path, fopen_read_mode())}
|
||||
{}
|
||||
{
|
||||
buffer_hook_.apply(file_);
|
||||
}
|
||||
|
||||
void file_istream::reset()
|
||||
{
|
||||
|
|
@ -92,9 +100,17 @@ namespace psemek::io
|
|||
return std::feof(file_) != 0;
|
||||
}
|
||||
|
||||
file_ostream::file_ostream(FILE * file)
|
||||
: file_{file}
|
||||
{
|
||||
buffer_hook_.apply(file_);
|
||||
}
|
||||
|
||||
file_ostream::file_ostream(std::filesystem::path const & path, unsigned flags)
|
||||
: file_{safe_fopen(path, fopen_write_mode(flags))}
|
||||
{}
|
||||
{
|
||||
buffer_hook_.apply(file_);
|
||||
}
|
||||
|
||||
void file_ostream::reset()
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue