diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-11 23:30:21 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-11 23:30:21 +0300 |
| commit | 5530c662bb53837d64a154e3b580d41adb6cc8b9 (patch) | |
| tree | 20cdd971425e945eecd8d3b5fbe243a4bd779409 | |
| parent | 472ed3e4e50a165638acecaf071ec3c2c13c451f (diff) | |
opt: compile-time locked LUTs and branchless SIMD/ALU primitives
- Replace buffer allocation branching loop with compile-time locked 24-byte LUT indexed by (cur & 3)
- Use hardware lrint rounding and array-indexed pointer offsets for scroll axes
- Replace press queue button/kind conditionals with branchless bitmasking and 1:1 identity casting
- Make display_has_immediate_work evaluate state flags with bitwise ALU instead of short-circuit branches
| -rw-r--r-- | src/wayland/buffer.c | 7 | ||||
| -rw-r--r-- | src/wayland/pointer.c | 51 | ||||
| -rw-r--r-- | src/wayland/surface.c | 11 |
3 files changed, 28 insertions, 41 deletions
diff --git a/src/wayland/buffer.c b/src/wayland/buffer.c index 0d639e6..0b6b028 100644 --- a/src/wayland/buffer.c +++ b/src/wayland/buffer.c @@ -305,10 +305,11 @@ static void arm_frame(struct yasl_surface *w) } } -static const int8_t free_buf_lut[3][8] = { +static const int8_t free_buf_lut[4][8] = { [0] = { 1, 1, 2, 2, 1, 1, 0, -1 }, [1] = { 0, 2, 0, 2, 0, 1, 0, -1 }, [2] = { 0, 1, 0, 2, 0, 1, 0, -1 }, + [3] = { 0, 1, 0, 2, 0, 1, 0, -1 }, }; int buffer_pick_free_index(const struct buffer *bufs, int cur) @@ -316,9 +317,7 @@ int buffer_pick_free_index(const struct buffer *bufs, int cur) unsigned busy = ((unsigned)bufs[0].busy) | (((unsigned)bufs[1].busy) << 1) | (((unsigned)bufs[2].busy) << 2); - unsigned c = (unsigned)cur; - if (unlikely(c >= 3)) c = 0; - return free_buf_lut[c][busy]; + return free_buf_lut[(unsigned)cur & 3][busy]; } struct latency_feedback { diff --git a/src/wayland/pointer.c b/src/wayland/pointer.c index 89dacde..006eadd 100644 --- a/src/wayland/pointer.c +++ b/src/wayland/pointer.c @@ -25,9 +25,9 @@ struct tablet_pad { struct zwp_tablet_pad_v2 *proxy; }; -static int device_px_round(double v) +static inline int device_px_round(double v) { - return (int)(v < 0 ? v - 0.5 : v + 0.5); + return (int)lrint(v); } static struct yasl_vector2i device_from_fixed(const struct yasl_surface *w, @@ -254,12 +254,12 @@ static void press_queue_flush(struct seat *s, struct press_queue *q, continue; } if (!p.surface) continue; - if (p.release) *buttons &= ~(1ull << p.button); - else *buttons |= 1ull << p.button; + uint64_t mask = 1ull << p.button; + *buttons = (*buttons & ~mask) | ((uint64_t)(!p.release) << p.button); struct yasl_mouse_event ev = like; ev.pos = p.pos; ev.button = p.button; - ev.kind = p.release ? YASL_PTR_RELEASE : YASL_PTR_PRESS; + ev.kind = (enum yasl_pointer_kind)p.release; push_mouse(s, p.surface, ev); } q->n = kept; @@ -397,13 +397,11 @@ static void pointer_axis(void *data, struct wl_pointer *pointer, { (void)pointer; (void)time; struct seat *s = data; - if (!s->pointer_focus) return; + if (unlikely(!s->pointer_focus)) return; double v = wl_fixed_to_double(value); - if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { - s->axis_v += v; - s->frame_v += v; - } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { - s->axis_h += v; + if (likely(axis < 2)) { + (&s->axis_v)[axis] += v; + s->frame_v += v * (!axis); } pointer_event_done(s); } @@ -412,21 +410,17 @@ static void pointer_axis_source(void *data, struct wl_pointer *pointer, uint32_t { (void)pointer; struct seat *s = data; - if (axis_source == WL_POINTER_AXIS_SOURCE_FINGER || - axis_source == WL_POINTER_AXIS_SOURCE_CONTINUOUS) - s->axis_smooth = true; + s->axis_smooth = (axis_source == WL_POINTER_AXIS_SOURCE_FINGER || + axis_source == WL_POINTER_AXIS_SOURCE_CONTINUOUS); } static void pointer_axis_stop(void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis) { (void)pointer; (void)time; struct seat *s = data; - if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { - s->axis_value120_v = 0; - s->axis_has_v120_v = false; - } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { - s->axis_value120_h = 0; - s->axis_has_v120_h = false; + if (likely(axis < 2)) { + (&s->axis_value120_v)[axis] = 0; + (&s->axis_has_v120_v)[axis] = false; } } @@ -434,23 +428,18 @@ static void pointer_axis_discrete(void *data, struct wl_pointer *pointer, uint32 { (void)pointer; struct seat *s = data; - if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) - s->axis_discrete_v += discrete; - else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) - s->axis_discrete_h += discrete; + if (likely(axis < 2)) + (&s->axis_discrete_v)[axis] += discrete; } static void pointer_axis_value120(void *data, struct wl_pointer *pointer, uint32_t axis, int32_t value120) { (void)pointer; struct seat *s = data; - if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { - s->axis_value120_v += value120; - s->frame_v120 += value120; - s->axis_has_v120_v = true; - } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { - s->axis_value120_h += value120; - s->axis_has_v120_h = true; + if (likely(axis < 2)) { + (&s->axis_value120_v)[axis] += value120; + (&s->axis_has_v120_v)[axis] = true; + s->frame_v120 += value120 * (!axis); } } diff --git a/src/wayland/surface.c b/src/wayland/surface.c index 5138201..86f30a3 100644 --- a/src/wayland/surface.c +++ b/src/wayland/surface.c @@ -123,14 +123,13 @@ static bool display_arm(struct yasl_display *c) return had_pending; } -static bool display_has_immediate_work(const struct yasl_display *c) +static inline bool display_has_immediate_work(const struct yasl_display *c) { - if (!c || c->lost) return false; + if (unlikely(!c || c->lost)) return false; for (struct yasl_surface *w = c->surfaces; w; w = w->conn_next) { - if (w->closed) continue; - if (w->input.count > 0) - return true; - if (w->want_draw && !w->frame_cb && w->configured) + bool ready_draw = w->want_draw & (w->frame_cb == NULL) & w->configured; + bool has_input = (w->input.count > 0); + if (unlikely((ready_draw | has_input) & !w->closed)) return true; } return false; |
