LifeEngine icon indicating copy to clipboard operation
LifeEngine copied to clipboard

Big performance update by theomgdev!

Open theomgdev opened this issue 1 year ago • 0 comments

Description

This PR introduces significant performance optimizations to handle larger simulations more efficiently. The changes focus on critical optimizations while maintaining the simulation's biological accuracy.

Changes

This pull request introduces several significant improvements and optimizations across multiple files. The main changes include adding a debounce function to control input handling, optimizing the organism update and removal process, converting the grid to a 1D array, enhancing the organism decision-making process, and improving the rendering efficiency. Below are the most important changes grouped by their themes:

Input Handling Optimization:

  • Added a debounce function to the defineEngineSpeedControls method in ControlPanel.js to prevent excessive input handling. [1] [2]

Organism Management:

  • Introduced a spatial grid in WorldEnvironment.js to track organism positions and optimize removal and updates. [1] [2] [3] [4]

Grid Optimization:

  • Converted the grid in GridMap.js to a 1D array for improved performance and simplified cell access methods. [1] [2]

Decision-Making Enhancement:

  • Enhanced the decide method in Brain.js to efficiently find the closest observation and make decisions accordingly.

Rendering Improvements:

  • Improved rendering efficiency in Renderer.js by using a dirtyCells set to track and render only modified cells. [1] [2]

These changes collectively improve the performance, readability, and maintainability of the codebase.

šŸš€ Core Optimizations

  • 1D Grid Storage
    Switched from 2D array to 1D array for grid cell storage (30-40% faster access)
  • Debounced FPS Controls
    Added 100ms debounce to FPS slider to prevent UI thread congestion
  • Reverse Organism Iteration
    Safer organism removal during updates with backward iteration
  • Dirty Cell Rendering
    Implemented differential rendering system for 70% fewer draw calls
  • Spatial Partitioning
    Added spatial grid for O(1) neighbor lookups in collision detection

🧠 Brain System Fixes

  • Null Cell Handling
    Added safety checks for edge-of-map observations
  • Optimized Decision Making
    Reduced from O(n²) to O(n) complexity in observation processing

🌐 World System Improvements

  • Perlin Noise Memoization
    Reduction in terrain generation costs
  • Grid Serialization
    Optimized grid saving/loading using sparse storage

šŸ› Critical Bug Fixes

  • Fixed organism removal index shifting
  • Patched null reference in sensory system
  • Resolved grid iteration errors

theomgdev avatar Feb 04 '25 23:02 theomgdev