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/backend.c | 19 ++++++++++++------- src/common/backend.h | 7 +++++++ src/common/input.c | 21 +++++++++++++-------- src/common/native.c | 8 ++++++-- 4 files changed, 38 insertions(+), 17 deletions(-) (limited to 'src/common') diff --git a/src/common/backend.c b/src/common/backend.c index c8d2caf..cab1dce 100644 --- a/src/common/backend.c +++ b/src/common/backend.c @@ -184,16 +184,17 @@ void yasl_surface_destroy(struct yasl_surface *s) struct yasl_display *yasl_surface_display(const struct yasl_surface *s) { - if (!s || !s->display->ops->surface_display) - return 0; - return s->display->ops->surface_display(s); + return s ? s->display : NULL; } void yasl_surface_set_callbacks(struct yasl_surface *s, const struct yasl_surface_cbs *cbs, void *ud) { - if (!s || !s->display->ops->surface_set_callbacks) + if (!s) return; - s->display->ops->surface_set_callbacks(s, cbs, ud); + s->callbacks = cbs ? *cbs : (struct yasl_surface_cbs){0}; + s->ud = ud; + if (s->display && s->display->ops && s->display->ops->surface_set_callbacks) + s->display->ops->surface_set_callbacks(s, cbs, ud); } void yasl_surface_set_title(struct yasl_surface *s, const char *title) @@ -250,9 +251,13 @@ bool yasl_surface_input_poll(struct yasl_surface *s, struct yasl_input_event *ev struct yasl_geometry yasl_surface_geometry(const struct yasl_surface *s) { - if (!s || !s->display->ops->surface_geometry) + if (!s) return (struct yasl_geometry){.scale_120 = YASL_SCALE_UNIT}; - return s->display->ops->surface_geometry(s); + if (s->scale_120) + return (struct yasl_geometry){s->size, s->scale_120}; + if (s->display && s->display->ops && s->display->ops->surface_geometry) + return s->display->ops->surface_geometry(s); + return (struct yasl_geometry){s->size, YASL_SCALE_UNIT}; } uint32_t yasl_surface_state(const struct yasl_surface *s) diff --git a/src/common/backend.h b/src/common/backend.h index 6ab7248..e1d29c5 100644 --- a/src/common/backend.h +++ b/src/common/backend.h @@ -9,13 +9,20 @@ struct yasl_backend_ops; struct yasl_display { const struct yasl_backend_ops *ops; + void *native_handle; struct yasl_display_cbs callbacks; struct yasl_loop *loop; }; struct yasl_surface { struct yasl_display *display; + void *native_handle; + struct yasl_vector2i size; + uint32_t scale_120; + uint32_t state; + uint32_t bg_color; struct yasl_surface_cbs callbacks; + void *ud; uint64_t frame_generation; yasl_timer_id *timers; int timer_count; 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; } diff --git a/src/common/native.c b/src/common/native.c index 6e1c69a..33837e4 100644 --- a/src/common/native.c +++ b/src/common/native.c @@ -12,13 +12,17 @@ void *yasl_display_get_native_handle(struct yasl_display *d) { - return d && d->ops->display_get_native_handle ? + if (!d) return NULL; + if (d->native_handle) return d->native_handle; + return d->ops && d->ops->display_get_native_handle ? d->ops->display_get_native_handle(d) : NULL; } void *yasl_surface_get_native_handle(struct yasl_surface *s) { - return s && s->display->ops->surface_get_native_handle ? + if (!s) return NULL; + if (s->native_handle) return s->native_handle; + return s->display && s->display->ops && s->display->ops->surface_get_native_handle ? s->display->ops->surface_get_native_handle(s) : NULL; } -- cgit v1.2.3