From c9b897d18008ad0c649d1d4269bbeb426d62f397 Mon Sep 17 00:00:00 2001 From: zeyus Date: Sun, 8 Jun 2025 23:00:45 +0200 Subject: [PATCH] memory optimization --- fluid_sim.cpp | 60 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/fluid_sim.cpp b/fluid_sim.cpp index 909cb7e..ba37f62 100644 --- a/fluid_sim.cpp +++ b/fluid_sim.cpp @@ -252,27 +252,44 @@ void mpuActiveMonitorMode() { -struct GridCell { - uint8_t particles[10]; - uint8_t count; -}; - -GridCell grid[GRID_SIZE][GRID_SIZE]; +// Optimized spatial grid using single array + cell indices +uint8_t gridParticles[NUM_PARTICLES]; // All particles in grid order +uint16_t gridCellStart[GRID_SIZE * GRID_SIZE]; // Start index for each cell +uint8_t gridCellCount[GRID_SIZE * GRID_SIZE]; // Count for each cell #ifdef DEBUG bool blinkState = true; #endif void buildSpatialGrid() { - memset(grid, 0, sizeof(grid)); - - for(int i=0; i= GRID_SIZE) continue; if(gy+dy < 0 || gy+dy >= GRID_SIZE) continue; - GridCell &cell = grid[gx+dx][gy+dy]; - for(int c=0; c