Big performance update by theomgdev! #171
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
debounce
function to thedefineEngineSpeedControls
method inControlPanel.js
to prevent excessive input handling. [1] [2]Organism Management:
WorldEnvironment.js
to track organism positions and optimize removal and updates. [1] [2] [3] [4]Grid Optimization:
GridMap.js
to a 1D array for improved performance and simplified cell access methods. [1] [2]Decision-Making Enhancement:
decide
method inBrain.js
to efficiently find the closest observation and make decisions accordingly.Rendering Improvements:
Renderer.js
by using adirtyCells
set to track and render only modified cells. [1] [2]These changes collectively improve the performance, readability, and maintainability of the codebase.
🚀 Core Optimizations
Switched from 2D array to 1D array for grid cell storage (30-40% faster access)
Added 100ms debounce to FPS slider to prevent UI thread congestion
Safer organism removal during updates with backward iteration
Implemented differential rendering system for 70% fewer draw calls
Added spatial grid for O(1) neighbor lookups in collision detection
🧠 Brain System Fixes
Added safety checks for edge-of-map observations
Reduced from O(n²) to O(n) complexity in observation processing
🌐 World System Improvements
Reduction in terrain generation costs
Optimized grid saving/loading using sparse storage
🐛 Critical Bug Fixes