From 9ed89c17dee752154c36d71cae93d20cccfc71ef Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 12 Oct 2026 14:27:51 +0300 Subject: perf: optimize hot-path loops, data structures, and cache locality --- src/wayland/clipboard.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/wayland/clipboard.c') diff --git a/src/wayland/clipboard.c b/src/wayland/clipboard.c index 7df1a70..e3eec0f 100644 --- a/src/wayland/clipboard.c +++ b/src/wayland/clipboard.c @@ -118,21 +118,18 @@ static int transfer_io(int fd, short events, void *ud) if (t->offset == t->len || n == 0 || (n < 0 && errno != EAGAIN && errno != EINTR)) transfer_destroy(t); } else { - char buf[8192]; - ssize_t n = read(fd, buf, sizeof(buf)); + if (t->capacity - t->len < 4096) { + size_t capacity = t->capacity ? t->capacity * 2 : 8192; + if (capacity > CLIPBOARD_TEXT_MAX) capacity = CLIPBOARD_TEXT_MAX; + if (capacity <= t->len) { transfer_destroy(t); return 0; } + char *text = realloc(t->text, capacity); + if (!text) { transfer_destroy(t); return 0; } + t->text = text; + t->capacity = capacity; + } + ssize_t n = read(fd, t->text + t->len, t->capacity - t->len); if (n > 0) { - size_t need = t->len + (size_t)n; - if (need > CLIPBOARD_TEXT_MAX) { transfer_destroy(t); return 0; } - if (need > t->capacity) { - size_t capacity = t->capacity ? t->capacity * 2 : sizeof(buf); - if (capacity > CLIPBOARD_TEXT_MAX) capacity = CLIPBOARD_TEXT_MAX; - char *text = realloc(t->text, capacity); - if (!text) { transfer_destroy(t); return 0; } - t->text = text; - t->capacity = capacity; - } - memcpy(t->text + t->len, buf, (size_t)n); - t->len += n; + t->len += (size_t)n; } else if (n == 0) { t->owner->receive(t->text ? t->text : "", t->len, t->uri_list, t->drop_surface, t->owner->ud); -- cgit v1.2.3