decrease how often LuaJIT GC works

This commit is contained in:
falsycat 2022-11-20 15:59:55 +09:00
parent 4f94ea4e3b
commit 482796bf60
2 changed files with 27 additions and 7 deletions

View File

@ -5,6 +5,7 @@
#include <functional>
#include <memory>
#include <mutex>
#include <type_traits>
#include <utility>
#include <tracy/Tracy.hpp>
@ -74,8 +75,14 @@ class Thread final : public nf7::Context,
ZoneScopedN("thread task execution");
for (nf7::Stopwatch sw; sw.dur() < kTaskDur; ++tasks_done_) {
auto t = q_.Pop();
if (!t) break;
runner_(std::move(t->second));
if (t) {
runner_(std::move(t->second));
} else {
if constexpr (std::is_invocable_v<Runner>) {
runner_(); // idle task
}
break;
}
}
}

View File

@ -45,6 +45,7 @@ class LuaContext final : public nf7::File, public nf7::DirItem {
return std::make_unique<LuaContext>(env, async_);
}
void Handle(const nf7::File::Event&) noexcept override;
void UpdateMenu() noexcept override;
void UpdateTooltip() noexcept override;
@ -69,17 +70,19 @@ class LuaContext::Queue final : public nf7::luajit::Queue,
Runner(const std::shared_ptr<SharedData>& data) noexcept : data_(data) {
}
void operator()(Task&& t) {
{
ZoneScopedN("LuaJIT task");
t(data_->L);
}
if (data_->L) {
ZoneScopedN("LuaJIT task");
t(data_->L);
require_gc_ = true;
}
void operator()() noexcept {
if (data_->L && std::exchange(require_gc_, false)) {
ZoneScopedNC("GC", tracy::Color::Gray);
lua_gc(data_->L, LUA_GCCOLLECT, 0);
}
}
private:
std::shared_ptr<SharedData> data_;
bool require_gc_ = false;
};
using Thread = nf7::Thread<Runner, Task>;
@ -129,6 +132,16 @@ class LuaContext::Queue final : public nf7::luajit::Queue,
std::shared_ptr<SharedData> data_;
};
void LuaContext::Handle(const nf7::File::Event& e) noexcept {
switch (e.type) {
case nf7::File::Event::kAdd:
q_->SetAsync(async_);
return;
default:
return;
}
}
void LuaContext::UpdateMenu() noexcept {
if (ImGui::MenuItem("async", nullptr, &async_)) {
q_->SetAsync(async_);