Fix some memory issues in the Server.

This commit is contained in:
ykiko
2024-11-29 20:11:16 +08:00
parent c240c7e7bc
commit f6a023d156
15 changed files with 247 additions and 103 deletions

View File

@@ -12,29 +12,52 @@ void PCH::apply(Compiler& compiler) const {
}
}
struct Tracer {
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
auto duration() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - start);
}
};
async::promise<void> Scheduler::updatePCH(llvm::StringRef filepath,
llvm::StringRef content,
llvm::ArrayRef<const char*> args) {
log::info("Start building PCH for {0}", filepath.str());
std::string outpath = "/home/ykiko/C++/clice2/build/cache/xxx.pch";
clang::PreambleBounds bounds = {0, 0};
co_await async::schedule_task([&] {
Compiler compiler(filepath, content, args);
bounds = clang::Lexer::ComputePreamble(content, {}, false);
if(bounds.Size != 0) {
compiler.generatePCH(outpath, bounds.Size, bounds.PreambleEndsAtStartOfLine);
auto [iter, success] = pchs.try_emplace(filepath);
if(success || iter->second.needUpdate(content)) {
log::info("Start building PCH for {0}", filepath.str());
Tracer tracer;
clang::PreambleBounds bounds = {0, 0};
co_await async::schedule_task([&] {
Compiler compiler(filepath, content, args);
bounds = clang::Lexer::ComputePreamble(content, {}, false);
if(bounds.Size != 0) {
compiler.generatePCH(outpath, bounds.Size, bounds.PreambleEndsAtStartOfLine);
}
});
auto preamble = content.substr(0, bounds.Size).str();
if(bounds.PreambleEndsAtStartOfLine) {
preamble.append("@");
}
});
log::info("Build PCH success");
pchs[filepath] = PCH{
.path = outpath,
.preamble = preamble,
.deps = {},
};
auto preamble = content.substr(0, bounds.Size).str();
if(bounds.PreambleEndsAtStartOfLine) {
preamble.append("@");
log::info("PCH for {0} is up-to-date, elapsed {1}ms",
filepath.str(),
tracer.duration().count());
} else {
log::info("Reuse PCH from {0}", filepath.str());
}
pchs.try_emplace(filepath, PCH{.path = outpath, .preamble = std::move(preamble)});
co_return;
}
@@ -67,6 +90,7 @@ async::promise<void> Scheduler::buildAST(llvm::StringRef filepath, llvm::StringR
bool isModule = false;
co_await (isModule ? updatePCM() : updatePCH(filepath, content, args));
Tracer tracer;
log::info("Start building AST for {0}", filepath.str());
auto task = [&path, &content, &args, pch = pchs.at(filepath)] {
@@ -86,13 +110,17 @@ async::promise<void> Scheduler::buildAST(llvm::StringRef filepath, llvm::StringR
auto& file = files[path];
file.compiler = std::move(compiler);
log::info("Build AST success");
log::info("Build AST successfully for {0}, elapsed {1}ms",
filepath.str(),
tracer.duration().count());
if(!file.waitings.empty()) {
auto task = std::move(file.waitings.front());
async::schedule(task.waiting);
file.waitings.pop_front();
}
file.isIdle = true;
}
async::promise<void> Scheduler::add(llvm::StringRef path, llvm::StringRef content) {