From b405973e91275d3ba921ca76a46b471982bf7fd6 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 17 Oct 2026 13:20:22 +0800 Subject: [PATCH] =?UTF-8?q?perf(gargantua):=20=E6=B8=B2=E6=9F=93=E6=8C=89?= =?UTF-8?q?=2064=20=E5=83=8F=E7=B4=A0=E5=88=86=E5=9D=97=E5=B9=B6=E6=AF=8F?= =?UTF-8?q?=E5=9D=97=E8=AE=A9=E5=87=BA=20CPU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现一次连续算 4 行(640 像素,单像素最坏 100 步),长时间不让出,单核下输入/合成全卡。改为 64 像素一块,每块后 process.yield_cpu();每 4 行仍 root.redraw() 保持渐进刷新节奏;事件轮询从每 4 行一次提升到每 64 像素一次。单次占用降到约 1/10,逐像素结果不变(仅调度顺序变化)。 --- Apps/Gargantua/main.vp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Apps/Gargantua/main.vp b/Apps/Gargantua/main.vp index 17e0ec9..86a0775 100644 --- a/Apps/Gargantua/main.vp +++ b/Apps/Gargantua/main.vp @@ -19,7 +19,8 @@ define DISK_OUT = 11.0 define CAM_DIST = 22.0 define CAM_ELEV = 9.0 define FOVF = 0.55 -define YIELD_ROWS = 4 +define SLICE_PIXELS = 64 +define FLUSH_ROWS = 4 define V3_POOL_SIZE = 8192 define V3_POOL_BYTES = V3_POOL_SIZE * 24 + 512 @@ -249,6 +250,8 @@ export def _start() -> int: gT: double = 0.0 rendered: int = 0 + rx: int = 0 + ry: int = 0 evt: window.InputEvent while window.is_active(root.win_id): @@ -263,22 +266,21 @@ export def _start() -> int: canvas.set_fb(fb, new_w, new_h) if rendered == 0: - y: int = 0 - while y < GH: - for x in range(GW): - pixel: uint32 = shade_pixel(x, y, gT) - fb[y * GW + x] = pixel - y += 1 - if y % YIELD_ROWS == 0: - root.redraw() - process.yield_cpu() - result2: int = window.poll_event(&evt) - if result2 != 0: - if evt.type == window.EVENT_TYPE_PING: - window.flush(root.win_id) - root.redraw() - rendered = 1 - gT += 50.0 + # 固定像素预算切片,保证单次占用时间片有上界 + budget: int = SLICE_PIXELS + while budget > 0 and ry < GH: + fb[ry * GW + rx] = shade_pixel(rx, ry, gT) + rx += 1 + budget -= 1 + if rx >= GW: + rx = 0 + ry += 1 + if ry % FLUSH_ROWS == 0: + root.redraw() + if ry >= GH: + root.redraw() + rendered = 1 + gT += 50.0 process.yield_cpu()