From 4145406fae437dce2f0af6e078e35566634f2c19 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 12 Oct 2026 15:14:41 +0300 Subject: perf: unify platform-independent state, branchless queue masking and native handle caching --- src/common/input.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/common/input.c') diff --git a/src/common/input.c b/src/common/input.c index ff578e3..471cd2b 100644 --- a/src/common/input.c +++ b/src/common/input.c @@ -35,11 +35,11 @@ static void discard_pending(struct input_queue *q) bool input_queue_push(struct input_queue *q, const struct yasl_input_event *ev) { struct yasl_input_event copy = *ev; - size_t len; - const char *text = event_text(ev, &len); char *owned = NULL; if (ev->kind == YASL_INPUT_TEXT || ev->kind == YASL_INPUT_PASTE || ev->kind == YASL_INPUT_DROP_FILES) { + size_t len; + const char *text = event_text(ev, &len); if ((!text && len) || len == SIZE_MAX) goto failed; owned = malloc(len + 1); if (!owned) goto failed; @@ -65,7 +65,7 @@ bool input_queue_push(struct input_queue *q, const struct yasl_input_event *ev) q->capacity = capacity; q->head = 0; } - q->events[(q->head + q->count) % q->capacity] = copy; + q->events[(q->head + q->count) & (q->capacity - 1)] = copy; q->count++; return true; @@ -82,8 +82,10 @@ failed: bool input_queue_pop(struct input_queue *q, struct yasl_input_event *ev) { if (!ev || (!q->count && !q->reset_pending)) return false; - free(q->last_text); - q->last_text = NULL; + if (q->last_text) { + free(q->last_text); + q->last_text = NULL; + } if (q->reset_pending) { *ev = (struct yasl_input_event){ .surface = q->reset_surface, .kind = YASL_INPUT_RESET, @@ -92,9 +94,12 @@ bool input_queue_pop(struct input_queue *q, struct yasl_input_event *ev) return true; } *ev = q->events[q->head]; - size_t len; - q->last_text = (char *)event_text(ev, &len); - q->head = (q->head + 1) % q->capacity; + if (ev->kind == YASL_INPUT_TEXT || ev->kind == YASL_INPUT_PASTE || + ev->kind == YASL_INPUT_DROP_FILES) { + size_t len; + q->last_text = (char *)event_text(ev, &len); + } + q->head = (q->head + 1) & (q->capacity - 1); q->count--; return true; } -- cgit v1.2.3