Skip to content

feat(ai): optimize game simulation speed with fast in-memory copying and iterative loops#10647

Open
suspectedspam wants to merge 1 commit into
Card-Forge:masterfrom
suspectedspam:feat/ai-performance-optimizations
Open

feat(ai): optimize game simulation speed with fast in-memory copying and iterative loops#10647
suspectedspam wants to merge 1 commit into
Card-Forge:masterfrom
suspectedspam:feat/ai-performance-optimizations

Conversation

@suspectedspam
Copy link
Copy Markdown

This PR introduces several optimizations to the Forge AI engine to significantly improve the performance of deep-tree game simulations.

Key Changes:

  • Fast Cloning: Implemented CardCopyService.fastGameCopy for rapid deep-cloning of card objects. This avoids the overhead of the legacy PaperCard re-parsing method.
    • Loop Refactoring: Refactored high-frequency stream-based operations in ComputerUtil, ComputerUtilCard, and ComputerUtilMana into optimized iterative for loops. This reduces garbage collection (GC) pressure and overhead during hot simulation paths.
    • Tree Pruning: Implemented a semantic signature caching mechanism in SpellAbilityPicker. This identifies redundant decision branches in the AI's search tree, pruning them and speeding up the overall decision-making process.
    • Simulation Isolation: Fixed 'Simulation Bleed' (where AI-modified card states leaked into the live game) by ensuring full ability list cloning in CardCopyService.
      Performance Impact:
      In internal benchmarks, these changes resulted in a significant reduction in time-to-decision for complex board states, particularly those involving many available spells and abilities.

…and iterative loops

- Implemented CardCopyService.fastGameCopy for rapid card deep-cloning.
- Refactored ComputerUtil, ComputerUtilCard, and ComputerUtilMana to use iterative loops instead of stream pipelines in hot paths.
- Integrated fast in-memory copying into GameCopier to avoid PaperCard re-parsing.
- Implemented signature caching in SpellAbilityPicker to prune redundant simulation branches.
- Fixed a bug where simulation state leaked into the live game by ensuring abilities are correctly copied in fastGameCopy.
@MostCromulent
Copy link
Copy Markdown
Contributor

MostCromulent commented May 10, 2026

@suspectedspam should probably outline your benchmarks, performance profiling and methodology on this given the claims in the PR about significant performance improvements.

I've had AI agents recommend performance optimisations in the past based on code analysis alone which don't bear out when you actually profile the results.

@MostCromulent MostCromulent added AI General AI tag Performance labels May 10, 2026
.max(Comparator.comparing(Card::getCMC))
.orElse(null);
Card best = null;
for (Card c : list) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why these changes should make a huge difference 🤔
Aren't Streams specifically designed to be cheap and fast in usage...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI General AI tag Performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants