Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BattleEngine in Rust for better memory efficiency with large battles #517

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

lanedirt
Copy link
Owner

@lanedirt lanedirt commented Jan 5, 2025

Summary

The existing PHP implementation of BattleEngine functions correctly but encounters significant memory and performance issues when handling large-scale battles. Specifically, it exhausts available memory and experiences prolonged processing times.

Changes

This pull request introduces a new BattleEngine written in Rust, which is interfaced with PHP using Foreign Function Interface (FFI). Leveraging Rust reduces overhead and allows for the utilization of more primitive data types, resulting in lower memory consumption.

Benefits

  • Improved Memory Efficiency: Rust's memory management significantly decreases the memory footprint compared to PHP.
  • Enhanced Performance: Reduced overhead leads to faster processing times for large battles.
  • Scalability: Ability to handle millions of units without running into memory limitations.

Memory Usage Comparison (Expected)

Implementation PHP Objects PHP Arrays PHP Fixed Arrays C++/Rust Go
Memory Usage 5 GB 3 GB 2 GB 100-120 MB 150 MB

Detailed Overview

  • PHP Objects: Managing 10 million units as PHP objects consumes approximately 5 GB of memory.
  • PHP Arrays: Utilizing PHP arrays for the same number of units requires around 3 GB.
  • PHP Fixed Arrays: Switching to fixed arrays in PHP reduces memory usage to 2 GB.
  • C++/Rust Implementation: The Rust-based BattleEngine should dramatically lower the memory usage to between 100-120 MB.

Todo

  • Add support for Rust to ogamex-app Dockerfile so it can run Rust.
  • Add Rust hello-world implementation and add feature test to verify it can be executed via PHP FFI.
  • Create RustBattleEngine class that can be swapped out with existing BattleEngine class.
  • Fix prepareBattleInput so it contains all required information, including amount of units.
  • Make Rust logic runnable and debuggable outside of PHP FFI context for better developer UX
  • Port BattleEngine::fightBattleRounds() method to call Rust module for processing via FFI.
  • Test that all battle engine related tests pass with the new Rust Battle Engine to ensure it works and takes into account all rules
  • Refactor BattleEngine classes to inherit from abstract BattleEngine class that contains shared methods.
  • Rename current BattleEngine to PhpBattleEngine.
  • Add (manual) test command that tracks processing time and memory usage when calling different battle engines for easier testing, debugging and comparison.
  • Test performance by running testLargeScaleAttackWithDebrisField with PHP version (+/- 60sec processing time) and Rust version.
  • Double check Rust compiled binary workflow: do we want to commit the (final) binary to the Git repo to make it part of the codebase or keep the current system where the Rust modules are compiled on Docker container startup. Compiling current workspace with external dependencies can take a few seconds: we probably want to do this as part of the build or statically include it in the git repo.

@lanedirt lanedirt added enhancement New feature or request docker Pull requests that update Docker code fleet dispatch system Issues regarding the fleet dispatch system labels Jan 5, 2025
@lanedirt lanedirt linked an issue Jan 5, 2025 that may be closed by this pull request
1 task
@Tsaranoga
Copy link

for an improved battleengine you may want to have a look at the julia battleengine of pr0game.com https://codeberg.org/pr0game/pr0game

@lanedirt
Copy link
Owner Author

lanedirt commented Jan 7, 2025

for an improved battleengine you may want to have a look at the julia battleengine of pr0game.com https://codeberg.org/pr0game/pr0game

Thanks for the link! Will take a look at it for ideas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker Pull requests that update Docker code enhancement New feature or request fleet dispatch system Issues regarding the fleet dispatch system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve battle engine efficiency with large number of units
2 participants