Skip to content

Commit

Permalink
fix: reduce peak memory usage (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
hchunhui authored Feb 6, 2024
1 parent cf2c572 commit 7f3eca2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ std::shared_ptr<LuaObj> Lua::getglobal(const std::string &v) {
return o;
}

void Lua::gc() {
lua_gc(L_, LUA_GCCOLLECT, 0);
}

LuaObj::LuaObj(lua_State *L, int i) : L_(L) {
lua_pushvalue(L, i);
id_ = luaL_ref(L, LUA_REGISTRYINDEX);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Lua {

std::shared_ptr<LuaObj> newthreadx(lua_State *L, int nargs);

void gc();

template <typename ... I>
std::shared_ptr<LuaObj> newthread(I ... input);

Expand Down
4 changes: 4 additions & 0 deletions src/lua_gears.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ bool LuaTranslation::Next() {
}
}

LuaTranslation::~LuaTranslation() {
lua_->gc();
}

static std::vector<std::string> split_string(const std::string& str, const std::string& delimiter) {
std::vector<std::string> result;
size_t pos = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/lua_gears.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LuaTranslation : public Translation {
return c_;
}

virtual ~LuaTranslation();

private:
Lua *lua_;
an<Candidate> c_;
Expand Down

0 comments on commit 7f3eca2

Please sign in to comment.