From a875fbd06066900cc2c43c9a12ab59c35f4a3ab1 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Tue, 9 Sep 2025 16:32:15 +0800 Subject: [PATCH] opt: Reduce unnecessary layout calculations --- src/layout/horizontal.h | 33 +++++++++++---------------------- src/layout/vertical.h | 34 ++++++++++++---------------------- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/layout/horizontal.h b/src/layout/horizontal.h index 6183a4f..963ac01 100644 --- a/src/layout/horizontal.h +++ b/src/layout/horizontal.h @@ -11,8 +11,7 @@ void fibonacci(Monitor *mon, int s) { cur_gappoh = smartgaps && mon->visible_tiling_clients == 1 ? 0 : cur_gappoh; cur_gappov = smartgaps && mon->visible_tiling_clients == 1 ? 0 : cur_gappov; // Count visible clients - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, mon) && ISTILED(c)) - n++; + n = mon->visible_tiling_clients; if (n == 0) return; @@ -133,13 +132,7 @@ void grid(Monitor *m) { Client *c; n = 0; - // 第一次遍历,计算 n 的值 - wl_list_for_each(c, &clients, link) { - if (VISIBLEON(c, m) && !c->isunglobal && - ((m->isoverview && !client_should_ignore_focus(c)) || ISTILED(c))) { - n++; - } - } + n = m->isoverview ? m->visible_clients : m->visible_tiling_clients; if (n == 0) { return; // 没有需要处理的客户端,直接返回 @@ -252,7 +245,8 @@ void deck(Monitor *m) { cur_gappoh = smartgaps && m->visible_tiling_clients == 1 ? 0 : cur_gappoh; cur_gappov = smartgaps && m->visible_tiling_clients == 1 ? 0 : cur_gappov; - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, m) && ISTILED(c)) n++; + n = m->visible_tiling_clients; + if (n == 0) return; @@ -298,11 +292,10 @@ void deck(Monitor *m) { // 滚动布局 void scroller(Monitor *m) { - unsigned int i, n; + unsigned int i, n, j; Client *c, *root_client = NULL; Client **tempClients = NULL; // 初始化为 NULL - n = 0; struct wlr_box target_geom; int focus_client_index = 0; bool need_scroller = false; @@ -317,12 +310,7 @@ void scroller(Monitor *m) { unsigned int max_client_width = m->w.width - 2 * scroller_structs - cur_gappih; - // 第一次遍历,计算 n 的值 - wl_list_for_each(c, &clients, link) { - if (VISIBLEON(c, m) && ISTILED(c)) { - n++; - } - } + n = m->visible_tiling_clients; if (n == 0) { return; // 没有需要处理的客户端,直接返回 @@ -336,11 +324,11 @@ void scroller(Monitor *m) { } // 第二次遍历,填充 tempClients - n = 0; + j = 0; wl_list_for_each(c, &clients, link) { if (VISIBLEON(c, m) && ISTILED(c)) { - tempClients[n] = c; - n++; + tempClients[j] = c; + j++; } } @@ -436,7 +424,8 @@ void tile(Monitor *m) { unsigned int i, n = 0, h, r, ie = enablegaps, mw, my, ty; Client *c; - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, m) && ISTILED(c)) n++; + n = m->visible_tiling_clients; + if (n == 0) return; diff --git a/src/layout/vertical.h b/src/layout/vertical.h index 34d784e..b9ee8c2 100644 --- a/src/layout/vertical.h +++ b/src/layout/vertical.h @@ -10,9 +10,8 @@ void vertical_fibonacci(Monitor *mon, int s) { cur_gappiv = smartgaps && mon->visible_tiling_clients == 1 ? 0 : cur_gappiv; cur_gappoh = smartgaps && mon->visible_tiling_clients == 1 ? 0 : cur_gappoh; cur_gappov = smartgaps && mon->visible_tiling_clients == 1 ? 0 : cur_gappov; - // Count visible clients - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, mon) && ISTILED(c)) - n++; + + n = mon->visible_tiling_clients; if (n == 0) return; @@ -130,14 +129,8 @@ void vertical_grid(Monitor *m) { unsigned int dy; unsigned int rows, cols, overrows; Client *c; - n = 0; - wl_list_for_each(c, &clients, link) { - if (VISIBLEON(c, m) && !c->isunglobal && - ((m->isoverview && !client_should_ignore_focus(c)) || ISTILED(c))) { - n++; - } - } + n = m->isoverview ? m->visible_clients : m->visible_tiling_clients; if (n == 0) { return; @@ -247,7 +240,8 @@ void vertical_deck(Monitor *m) { cur_gappoh = smartgaps && m->visible_tiling_clients == 1 ? 0 : cur_gappoh; cur_gappov = smartgaps && m->visible_tiling_clients == 1 ? 0 : cur_gappov; - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, m) && ISTILED(c)) n++; + n = m->visible_tiling_clients; + if (n == 0) return; @@ -288,10 +282,9 @@ void vertical_deck(Monitor *m) { } void vertical_scroller(Monitor *m) { - unsigned int i, n; + unsigned int i, n, j; Client *c, *root_client = NULL; Client **tempClients = NULL; - n = 0; struct wlr_box target_geom; int focus_client_index = 0; bool need_scroller = false; @@ -306,11 +299,7 @@ void vertical_scroller(Monitor *m) { unsigned int max_client_height = m->w.height - 2 * scroller_structs - cur_gappiv; - wl_list_for_each(c, &clients, link) { - if (VISIBLEON(c, m) && ISTILED(c)) { - n++; - } - } + n = m->visible_tiling_clients; if (n == 0) { return; @@ -321,11 +310,11 @@ void vertical_scroller(Monitor *m) { return; } - n = 0; + j = 0; wl_list_for_each(c, &clients, link) { if (VISIBLEON(c, m) && ISTILED(c)) { - tempClients[n] = c; - n++; + tempClients[j] = c; + j++; } } @@ -421,7 +410,8 @@ void vertical_tile(Monitor *m) { unsigned int i, n = 0, w, r, ie = enablegaps, mh, mx, tx; Client *c; - wl_list_for_each(c, &clients, link) if (VISIBLEON(c, m) && ISTILED(c)) n++; + n = m->visible_tiling_clients; + if (n == 0) return;