1
0
Fork 0

Use mmapping when reading Nasal scripts saving two memory copies out of for (copy from disk to the i/o buffer, copy from the i/o buffer tot the ifstream, copy from the ifstreamto a string and copy from a string to Nasal).

This commit is contained in:
Erik Hofman 2023-02-25 10:03:03 +01:00
parent d00c95f0e8
commit 098300bb51

View file

@ -47,6 +47,7 @@
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <simgear/structure/commands.hxx> #include <simgear/structure/commands.hxx>
#include <simgear/structure/event_mgr.hxx> #include <simgear/structure/event_mgr.hxx>
#include <simgear/io/sg_mmap.hxx>
#include <simgear/nasal/cppbind/from_nasal.hxx> #include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/to_nasal.hxx> #include <simgear/nasal/cppbind/to_nasal.hxx>
@ -1505,6 +1506,15 @@ bool FGNasalSys::loadModule(SGPath file, const char* module)
return false; return false;
} }
#if 1
// MMap the contents of the file.
// This saves an alloc, memcpy and free
SGMMapFile mmap(file);
mmap.open(SG_IO_IN);
auto pathStr = file.utf8Str();
return createModule(module, pathStr.c_str(), mmap.get(), mmap.get_size());
#else
sg_ifstream file_in(file); sg_ifstream file_in(file);
string buf; string buf;
while (!file_in.eof()) { while (!file_in.eof()) {
@ -1515,6 +1525,7 @@ bool FGNasalSys::loadModule(SGPath file, const char* module)
file_in.close(); file_in.close();
auto pathStr = file.utf8Str(); auto pathStr = file.utf8Str();
return createModule(module, pathStr.c_str(), buf.data(), buf.length()); return createModule(module, pathStr.c_str(), buf.data(), buf.length());
#endif
} }
// Parse and run. Save the local variables namespace, as it will // Parse and run. Save the local variables namespace, as it will