Cleanup / optimizations

This commit is contained in:
2026-02-12 01:01:52 +00:00
parent e9d164d769
commit c683754d49
8 changed files with 141 additions and 78 deletions

View File

@@ -2,19 +2,15 @@
#define LSP_H #define LSP_H
#include "editor/editor.h" #include "editor/editor.h"
#include "main.h"
#include "pch.h" #include "pch.h"
#include "utils/utils.h" #include "utils/utils.h"
#include <cstdint>
#include <sys/types.h>
#define LSP_TIMEOUT 3000 #define LSP_TIMEOUT_START 3000
#define LSP_TIMEOUT_QUIT_NORMAL 300
namespace lsp { #define LSP_TIMEOUT_QUIT_FORCE 80
extern std::mutex lsp_mutex;
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
active_lsps;
extern Queue<std::string> need_opening;
extern std::unordered_set<std::string> opened;
extern std::vector<Editor *> new_editors;
} // namespace lsp
void lsp_worker(); void lsp_worker();
@@ -48,6 +44,16 @@ struct LSPMessage {
std::function<void(const LSPMessage &)> callback; std::function<void(const LSPMessage &)> callback;
}; };
namespace lsp {
extern std::mutex lsp_mutex;
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
active_lsps;
extern Queue<std::string> need_opening;
extern std::unordered_set<std::string> opened;
extern std::vector<Editor *> new_editors;
extern Queue<std::unique_ptr<LSPMessage>> response_queue;
} // namespace lsp
struct LSPInstance { struct LSPInstance {
const LSP *lsp_info; const LSP *lsp_info;
std::string root_dir; std::string root_dir;
@@ -70,7 +76,6 @@ struct LSPInstance {
Queue<json> inbox; Queue<json> inbox;
Queue<json> outbox; Queue<json> outbox;
std::unordered_map<uint32_t, std::unique_ptr<LSPMessage>> pending; std::unordered_map<uint32_t, std::unique_ptr<LSPMessage>> pending;
std::vector<std::unique_ptr<LSPMessage>> lsp_response_queue;
std::vector<Editor *> editors; std::vector<Editor *> editors;
LSPInstance(std::string lsp_id) { LSPInstance(std::string lsp_id) {
@@ -89,7 +94,15 @@ struct LSPInstance {
{"capabilities", client_capabilities}}}}; {"capabilities", client_capabilities}}}};
send_raw(initialize_message); send_raw(initialize_message);
pollfd pfd{stdout_fd, POLLIN, 0}; pollfd pfd{stdout_fd, POLLIN, 0};
poll(&pfd, 1, LSP_TIMEOUT); uint32_t waited = 0;
while (waited < LSP_TIMEOUT_START) {
poll(&pfd, 1, 50);
if (pfd.revents & POLLIN)
break;
if (!running)
return;
waited += 50;
}
if (!(pfd.revents & POLLIN)) { if (!(pfd.revents & POLLIN)) {
exited = true; exited = true;
return; return;
@@ -179,34 +192,39 @@ struct LSPInstance {
send_raw(initialized_message); send_raw(initialized_message);
} }
~LSPInstance() { ~LSPInstance() {
uint32_t timeout =
running ? LSP_TIMEOUT_QUIT_NORMAL : LSP_TIMEOUT_QUIT_FORCE;
for (auto &ed : editors) for (auto &ed : editors)
ed->lsp.store(nullptr); ed->lsp.store(nullptr);
initialized = false; initialized = false;
exited = true; exited = true;
if (pid == -1) if (pid == -1)
return; return;
json shutdown = {{"id", ++last_id}, {"method", "shutdown"}}; json shutdown = {
{"jsonrpc", "2.0"}, {"id", ++last_id}, {"method", "shutdown"}};
send_raw(shutdown); send_raw(shutdown);
pollfd pfd{stdout_fd, POLLIN, 0}; pollfd pfd{stdout_fd, POLLIN, 0};
poll(&pfd, 1, 500); poll(&pfd, 1, timeout);
json exit_msg = {{"method", "exit"}}; if (pfd.revents & POLLIN) {
send_raw(exit_msg); json exit_msg = {{"jsonrpc", "2.0"}, {"method", "exit"}};
int waited = 0; send_raw(exit_msg);
while (waited < 100) { int waited = 0;
int status; while (waited < timeout) {
pid_t res = waitpid(pid, &status, WNOHANG); int status;
if (res == pid) pid_t res = waitpid(pid, &status, WNOHANG);
break; if (res == pid)
std::this_thread::sleep_for(std::chrono::milliseconds(10)); break;
waited += 10; std::this_thread::sleep_for(10ms);
waited += 10;
}
} }
close(stdin_fd);
close(stdout_fd);
if (kill(pid, 0) == 0) { if (kill(pid, 0) == 0) {
kill(pid, SIGKILL); kill(pid, SIGKILL);
waitpid(pid, nullptr, 0); waitpid(pid, nullptr, 0);
} }
pid = -1; pid = -1;
close(stdin_fd);
close(stdout_fd);
} }
bool init_process() { bool init_process() {
int in_pipe[2]; int in_pipe[2];
@@ -312,7 +330,7 @@ struct LSPInstance {
if (it != pending.end()) { if (it != pending.end()) {
if (it->second->editor) { if (it->second->editor) {
it->second->message = *msg; it->second->message = *msg;
lsp_response_queue.push_back(std::move(it->second)); lsp::response_queue.push(std::move(it->second));
} else { } else {
auto message = *std::move(it->second); auto message = *std::move(it->second);
message.message = *msg; message.message = *msg;
@@ -335,7 +353,7 @@ struct LSPInstance {
response->message = *msg; response->message = *msg;
response->callback = editor_handle_wrapper; response->callback = editor_handle_wrapper;
if (ed) if (ed)
lsp_response_queue.push_back(std::move(response)); lsp::response_queue.push(std::move(response));
else else
lsp_handle(*msg); lsp_handle(*msg);
} }
@@ -344,11 +362,6 @@ struct LSPInstance {
inline static void editor_handle_wrapper(const LSPMessage &message) { inline static void editor_handle_wrapper(const LSPMessage &message) {
message.editor->lsp_handle(message.message); message.editor->lsp_handle(message.message);
} }
void callbacks() {
for (auto &message : lsp_response_queue)
message->callback(*message);
lsp_response_queue.clear();
}
inline void send_raw(const json &msg) { inline void send_raw(const json &msg) {
std::string payload = msg.dump(); std::string payload = msg.dump();
std::string header = std::string header =

View File

@@ -6,7 +6,7 @@
#include "pch.h" #include "pch.h"
#include "syntax/trie.h" #include "syntax/trie.h"
#define MAX_LINES_LOOKBEHIND 80 #define MAX_LINES_LOOKAROUND 512
struct Highlight { struct Highlight {
uint32_t fg{0xFFFFFF}; uint32_t fg{0xFFFFFF};

View File

@@ -59,15 +59,8 @@ struct LineMap {
} }
void apply_edit(uint32_t start, int64_t delta) { void apply_edit(uint32_t start, int64_t delta) {
if (delta < 0) { if (delta < 0)
int64_t count = -delta; batch_remove(start, -delta);
for (int64_t i = 0; i < count; i++) {
auto key = resolve_line(start + i);
if (!key)
continue;
lines.erase(*key);
}
}
current_version++; current_version++;
edit_log.push_back({start, delta}); edit_log.push_back({start, delta});
} }
@@ -79,6 +72,45 @@ private:
uint32_t current_version; uint32_t current_version;
void batch_remove(uint32_t line, uint32_t count) {
std::vector<uint32_t> lines_t;
lines_t.reserve(count);
for (uint32_t i = 0; i < count; i++)
lines_t.push_back(line + i);
for (int64_t v = current_version; v >= 0; v--) {
for (auto &l : lines_t) {
if (l == UINT32_MAX)
continue;
LineKey key = {l, (uint32_t)v};
if (lines.find(key) != lines.end()) {
lines.erase(key);
l = UINT32_MAX;
}
}
bool all_removed = true;
const auto &edit = edit_log[v];
for (auto &l : lines_t) {
if (l == UINT32_MAX)
continue;
all_removed = false;
if (edit.delta > 0) {
if (l >= edit.start_line) {
if (l < edit.start_line + edit.delta)
l = UINT32_MAX;
else
l -= edit.delta;
}
} else {
if (l >= edit.start_line)
l -= edit.delta;
}
}
if (all_removed)
break;
}
return;
}
std::optional<LineKey> resolve_line(uint32_t line) { std::optional<LineKey> resolve_line(uint32_t line) {
uint32_t current_line = line; uint32_t current_line = line;
for (int64_t v = current_version; v >= 0; v--) { for (int64_t v = current_version; v >= 0; v--) {

View File

@@ -6,18 +6,19 @@
template <typename T> struct Queue { template <typename T> struct Queue {
void push(T val) { void push(T val) {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
q.push(val); q.push(std::move(val));
} }
std::optional<T> front() { std::optional<T> front() {
std::lock_guard<std::mutex> lock(m);
if (q.empty()) if (q.empty())
return std::nullopt; return std::nullopt;
return q.front(); return std::move(q.front());
} }
bool pop(T &val) { bool pop(T &val) {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
if (q.empty()) if (q.empty())
return false; return false;
val = q.front(); val = std::move(q.front());
q.pop(); q.pop();
return true; return true;
} }

View File

@@ -339,6 +339,5 @@ puts 'Ruby syntax highlighting test complete.'
__END__ __END__
Anything here should be ignored >><< Anything here should be ignored >><<
{{{}}}[[[]]](((000))) {{{}}}[[[]]](((000)))

View File

@@ -6,6 +6,7 @@ std::unordered_map<std::string, std::unique_ptr<LSPInstance>> active_lsps;
std::unordered_set<std::string> opened; std::unordered_set<std::string> opened;
Queue<std::string> need_opening; Queue<std::string> need_opening;
std::vector<Editor *> new_editors; std::vector<Editor *> new_editors;
Queue<std::unique_ptr<LSPMessage>> response_queue;
} // namespace lsp } // namespace lsp
void lsp_worker() { void lsp_worker() {

View File

@@ -44,34 +44,41 @@ int main(int argc, char *argv[]) {
std::thread lsp_thread(background_lsp); std::thread lsp_thread(background_lsp);
while (running) { while (running) {
KeyEvent event = throttle(1ms, read_key); uint8_t consumed = 0;
if (event.key_type != KEY_NONE) { KeyEvent event;
if (event.key_type == KEY_CHAR && event.len == 1 && while (++consumed < 32 && (event = read_key()).key_type != KEY_NONE) {
event.c[0] == CTRL('q')) { if (event.key_type != KEY_NONE) {
free(event.c); if (event.key_type == KEY_CHAR && event.len == 1 &&
running = false; event.c[0] == CTRL('q')) {
break; free(event.c);
} running = false;
if (mode != RUNNER) { goto quit;
if (event.key_type == KEY_MOUSE) {
handle_click(event);
} else {
layout::focused_window->handle_event(event);
} }
} else { if (mode != RUNNER) {
ui::bar.handle_event(event); if (event.key_type == KEY_MOUSE) {
handle_click(event);
} else {
layout::focused_window->handle_event(event);
}
} else {
ui::bar.handle_event(event);
}
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) &&
event.c)
free(event.c);
} }
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) &&
event.c)
free(event.c);
} }
for (auto &lsp_inst : lsp::active_lsps) std::unique_ptr<LSPMessage> msg;
lsp_inst.second->callbacks(); while (lsp::response_queue.pop(msg)) {
msg->callback(*msg);
};
layout::focused_window->work(); layout::focused_window->work();
throttle(4ms, render); render();
throttle(4ms, io_render); throttle(8ms, io_render);
} }
quit:
if (lsp_thread.joinable()) if (lsp_thread.joinable())
lsp_thread.join(); lsp_thread.join();

View File

@@ -46,20 +46,26 @@ void Parser::work() {
return; return;
std::vector<uint32_t> batch; std::vector<uint32_t> batch;
uint32_t line_count = editor->root->line_count + 1; uint32_t line_count = editor->root->line_count + 1;
int64_t start = MIN(scroll_max + 10, line_count - 1); uint32_t min_line =
int64_t end = MAX(0, (int64_t)scroll_max - MAX_LINES_LOOKBEHIND); scroll_max > MAX_LINES_LOOKAROUND ? scroll_max - MAX_LINES_LOOKAROUND : 0;
for (int64_t i = start; i >= end; --i) { uint32_t max_line = MIN(scroll_max + MAX_LINES_LOOKAROUND, line_count - 1);
auto l_opt = line_map.at(i); bool sequential = false;
if (!l_opt || (l_opt && !l_opt->out_state)) for (uint32_t i = min_line; i <= max_line; ++i) {
LineData *ld = line_map.at(i);
if ((!ld || !ld->in_state || !ld->out_state) && !sequential) {
batch.push_back(i); batch.push_back(i);
sequential = true;
continue;
}
sequential = false;
} }
for (uint32_t c_line : batch) { for (uint32_t c_line : batch) {
if (!running.load(std::memory_order_relaxed)) if (!running.load(std::memory_order_relaxed))
break; break;
uint32_t min_line = scroll_max > MAX_LINES_LOOKBEHIND uint32_t min_line = scroll_max > MAX_LINES_LOOKAROUND
? scroll_max - MAX_LINES_LOOKBEHIND ? scroll_max - MAX_LINES_LOOKAROUND
: 0; : 0;
uint32_t max_line = scroll_max + 10; uint32_t max_line = scroll_max + MAX_LINES_LOOKAROUND;
if (c_line < min_line || c_line > max_line) if (c_line < min_line || c_line > max_line)
continue; continue;
uint32_t scroll_snapshot = scroll_max; uint32_t scroll_snapshot = scroll_max;
@@ -78,8 +84,12 @@ void Parser::work() {
break; break;
if (scroll_snapshot != scroll_max) if (scroll_snapshot != scroll_max)
break; break;
if (cur_line < min_line || cur_line > max_line) if (cur_line < min_line || cur_line > max_line) {
LineData *line_data = line_map.at(cur_line);
if (line_data)
line_data->out_state = nullptr;
break; break;
}
uint32_t len; uint32_t len;
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) if (!line)