mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* perf(models): read the .e3d file with one unbuffered fread The model loader read the whole .e3d file with std::ifstream::read. With MSVC that reads a file not in the system cache from a hard disk at about 24 MB/s, against about 38 MB/s for one unbuffered fread, and a cached file at less than half the speed. The new core helper read_whole_file() opens the file without a stream buffer and reads it with one fread. The loader parses the same bytes from memory as before; a missing, empty or short file gives the same result as with the file stream. * perf(models): the comment of read_whole_file quotes the measured read speeds
44 lines
1.0 KiB
C++
44 lines
1.0 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "utilities/wholefile.h"
|
|
|
|
#include <cstdio>
|
|
|
|
whole_file read_whole_file(std::string const &Path)
|
|
{
|
|
whole_file result;
|
|
|
|
auto *const file{std::fopen(Path.c_str(), "rb")};
|
|
if (file == nullptr)
|
|
{
|
|
return result;
|
|
}
|
|
// without a buffer of its own the stream hands the read to the system in one piece, straight into the memory
|
|
std::setvbuf(file, nullptr, _IONBF, 0);
|
|
#ifdef _WIN32
|
|
_fseeki64(file, 0, SEEK_END);
|
|
auto const length{_ftelli64(file)};
|
|
_fseeki64(file, 0, SEEK_SET);
|
|
#else
|
|
fseeko(file, 0, SEEK_END);
|
|
auto const length{ftello(file)};
|
|
fseeko(file, 0, SEEK_SET);
|
|
#endif
|
|
if (length > 0)
|
|
{
|
|
result.data.reset(new char[static_cast<std::size_t>(length)]);
|
|
result.size = std::fread(result.data.get(), 1, static_cast<std::size_t>(length), file);
|
|
}
|
|
std::fclose(file);
|
|
|
|
return result;
|
|
}
|