Skip to content

Commit c846467

Browse files
committed
Fix script bytebuffer vector preallocation
We guesstimate the size of the buffer based on the initial script size. Not very accurate, but better than the alternatives.
1 parent cad3d55 commit c846467

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

ElunaLoader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ void ElunaLoader::LoadScripts()
166166

167167
int ElunaLoader::LoadBytecodeChunk(lua_State* /*L*/, uint8* bytes, size_t len, BytecodeBuffer* buffer)
168168
{
169-
buffer->reserve(buffer->size() + len);
170169
buffer->insert(buffer->end(), bytes, bytes + len);
171170
return 0;
172171
}
@@ -231,7 +230,8 @@ void ElunaLoader::ReadFiles(lua_State* L, std::string path)
231230

232231
// was file, try add
233232
std::string filename = dir_iter->path().filename().generic_string();
234-
ProcessScript(L, filename, fullpath, mapId);
233+
size_t filesize = fs::file_size(dir_iter->path());
234+
ProcessScript(L, filename, filesize, fullpath, mapId);
235235
}
236236
}
237237
}
@@ -272,7 +272,7 @@ bool ElunaLoader::CompileScript(lua_State* L, LuaScript& script)
272272
return true;
273273
}
274274

275-
void ElunaLoader::ProcessScript(lua_State* L, std::string filename, const std::string& fullpath, int32 mapId)
275+
void ElunaLoader::ProcessScript(lua_State* L, std::string filename, const size_t& filesize, const std::string& fullpath, int32 mapId)
276276
{
277277
ELUNA_LOG_DEBUG("[Eluna]: ProcessScript checking file `%s`", fullpath.c_str());
278278

@@ -293,6 +293,7 @@ void ElunaLoader::ProcessScript(lua_State* L, std::string filename, const std::s
293293
script.filename = filename;
294294
script.filepath = fullpath;
295295
script.modulepath = fullpath.substr(0, fullpath.length() - filename.length() - ext.length());
296+
script.bytecode.reserve(filesize);
296297
script.mapId = mapId;
297298

298299
// if compilation fails, we don't add the script

ElunaLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ElunaLoader
6969
void ReloadScriptCache();
7070
void ReadFiles(lua_State* L, std::string path);
7171
void CombineLists();
72-
void ProcessScript(lua_State* L, std::string filename, const std::string& fullpath, int32 mapId);
72+
void ProcessScript(lua_State* L, std::string filename, const size_t& filesize, const std::string& fullpath, int32 mapId);
7373
bool CompileScript(lua_State* L, LuaScript& script);
7474
static int LoadBytecodeChunk(lua_State* L, uint8* bytes, size_t len, BytecodeBuffer* buffer);
7575

0 commit comments

Comments
 (0)