diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-12 14:27:51 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-12 14:27:51 +0300 |
| commit | 9ed89c17dee752154c36d71cae93d20cccfc71ef (patch) | |
| tree | a32cd894643ff6f6ebce83dd745bd24b35eb6520 /src/wayland/clipboard.c | |
| parent | aedd863e6f66fd446227e9b107bc9c1970092634 (diff) | |
perf: optimize hot-path loops, data structures, and cache locality
Diffstat (limited to 'src/wayland/clipboard.c')
| -rw-r--r-- | src/wayland/clipboard.c | 25 |
1 files changed, 11 insertions, 14 deletions
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); |
