From cfe9548c3e9da5402f83015166fc6c36b589e5cd Mon Sep 17 00:00:00 2001 From: yy214123 Date: Tue, 8 Apr 2025 13:08:31 +0800 Subject: [PATCH] Reorder win-checks to improve performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reorder win-check logic to favor row-wise over column-wise checks. This aligns better with C’s row-major memory layout, enhancing spatial locality. It reduces cache line crossings, cuts down total instruction count, and improves sequential memory access— especially useful for large boards or frequent evaluations. Benchmark on a 5×5 board (1M iterations): - Instructions: 20,042,179,354 → 9,528,171,690 (-52.5%) - Cache refs: 205,075 → 115,548 (-43.65%) - Cache misses: 28,503 → 20,176 (-29.23%) - Time elapsed: 0.971s → 0.597s (-38.5%) Despite a minor rise in miss rate, total misses declined due to fewer cache references. --- game.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game.c b/game.c index a78330a..06dbe53 100644 --- a/game.c +++ b/game.c @@ -19,8 +19,8 @@ _Static_assert(ALLOW_EXCEED == 0 || ALLOW_EXCEED == 1, "ALLOW_EXCEED must be a boolean that is 0 or 1"); const line_t lines[4] = { - {1, 0, 0, 0, BOARD_SIZE - GOAL + 1, BOARD_SIZE}, // COL {0, 1, 0, 0, BOARD_SIZE, BOARD_SIZE - GOAL + 1}, // ROW + {1, 0, 0, 0, BOARD_SIZE - GOAL + 1, BOARD_SIZE}, // COL {1, 1, 0, 0, BOARD_SIZE - GOAL + 1, BOARD_SIZE - GOAL + 1}, // PRIMARY {1, -1, 0, GOAL - 1, BOARD_SIZE - GOAL + 1, BOARD_SIZE}, // SECONDARY };