From 098300bb51d97ae7c037c61964bcd7969e3b20b5 Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Sat, 25 Feb 2023 10:03:03 +0100 Subject: [PATCH] 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). --- src/Scripting/NasalSys.cxx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 59b01b3b5..8bef6c916 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -1505,6 +1506,15 @@ bool FGNasalSys::loadModule(SGPath file, const char* module) 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); string buf; while (!file_in.eof()) { @@ -1515,6 +1525,7 @@ bool FGNasalSys::loadModule(SGPath file, const char* module) file_in.close(); auto pathStr = file.utf8Str(); return createModule(module, pathStr.c_str(), buf.data(), buf.length()); +#endif } // Parse and run. Save the local variables namespace, as it will