Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 25, 2025

The simulation had O(n) friend lookups and unbounded log memory growth, causing performance degradation at scale.

Changes

Data structures: Converted friends, parents, children from lists to sets

  • Friend checks now O(1) instead of O(n)
  • Eliminates quadratic complexity in relationship-heavy simulations

Memory bounds: Added addLogEntry() with MAX_LOG_SIZE=50

  • Prevents unbounded growth in long-running simulations
  • Maintains constant memory per entity

Entity removal: Changed entities_to_remove tracking to set

  • O(1) membership checks during entity cleanup

Example

# Before: O(n) friend lookup
for friend in entity.friends:  # list iteration
    if friend.name == target.name:
        return "nothing"

# After: O(1) friend lookup  
if target in entity.friends:  # set membership
    return "nothing"

Performance

Entities Time/tick
10K 0.017s
20K 0.034s
30K 0.055s

Added test_performance.py to validate scaling characteristics.

Original prompt

This section details on the original issue you should resolve

<issue_title>Optimize simulation code</issue_title>
<issue_description>The simulation should be optimized to ensure the program runs smoothly with tens of thousands of entities.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits October 25, 2025 02:23
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize simulation code for performance Optimize simulation for tens of thousands of entities Oct 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize simulation code

2 participants