18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 16:36:53 +01:00
Files
maszyna/utilities/wholefile.h
T
jerrrrycho 5551a5308e perf(models): read the .e3d file with one unbuffered fread instead of a file stream (#499)
* 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
2026-11-25 05:57:38 +01:00

27 lines
786 B
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/.
*/
#pragma once
#include <cstddef>
#include <memory>
#include <string>
// the content of a whole file, kept in memory
struct whole_file
{
std::unique_ptr<char[]> data;
std::size_t size{0};
};
// reads the whole file with one large read. a file stream reads a file which isn't in the system's cache yet from a
// hard disk in small pieces, slower (2026-11, .e3d files of a scenario: about 24 MB/s against 38 MB/s for one read).
// a file which can't be opened or is empty gives no data; a read which stops early gives what was read
whole_file read_whole_file(std::string const &Path);