tridecco / game-client-web

Tridecco Game Client is a frontend application designed to support Tridecco games. It provides user interface rendering, user interaction handling, dynamic content loading, and communication with the backend server.
https://play.tridecco.com
GNU Affero General Public License v3.0
1 stars 0 forks source link

Game Performance Degradation Over Time #22

Closed TKanX closed 1 month ago

TKanX commented 1 month ago

Description:

The game experiences significant performance issues during gameplay. Initially, the game runs smoothly, but after approximately the 10th turn, it begins to lag and eventually becomes unresponsive. The memory usage remains stable at around 100MB, so memory leakage is not suspected. The issue seems to be related to the rendering logic using the Canvas API.

Steps to Reproduce:

  1. Start a new game session.
  2. Play through approximately 10 turns.
  3. Observe the increasing lag and eventual freezing of the game.

Expected Behavior:

The game should maintain consistent performance throughout all turns, without noticeable lag or freezing.

Actual Behavior:

Performance deteriorates after several turns, leading to significant lag and eventual page unresponsiveness.

Possible Causes:

TKanX commented 1 month ago

Performance Recording Completed

I have completed a performance recording to analyze the issues with game performance. The recording includes details on where the performance degrades and potential bottlenecks.

Screenshot 2024-08-31 124159

Timeline Spikes: Look for any sudden spikes in activity on the timeline that might indicate performance issues. Frame Rate: Check the frame rate graph to see if there are significant drops. Main Thread Analysis: Review the main thread’s activities to identify any long tasks or inefficient operations. Rendering Issues: Analyze the rendering section for frequent or excessive painting operations. Memory Usage: Observe memory usage patterns to ensure there are no unexpected increases.

@tedhyu

tedhyu commented 1 month ago

Very impressive, Tony.

TKanX commented 1 month ago

Performance Analysis Update

image

I have recorded the performance data during the occurrence of the lagging issue. Here’s a summary of my findings:

Upon examining the first critical time frame where the game begins to experience severe lag, the following key observations were made:

image

  1. Frame Rate Drop and Freeze:

The frame rate starts to drop sharply just before the game completely freezes. At this point, the frame rate data disappears from the timeline, indicating a total halt in the rendering process.

image

Normal frame rate:

image

Drop frame rate:

image

  1. GPU Activity:

Multiple GPU tasks, each taking approximately 751ms, were identified during the problematic frame. The green blocks on the timeline represent these GPU operations, indicating that the GPU was heavily engaged in rendering or compositing tasks during this period.

image

Upon examining the fourth critical time frame the task lasted for 13.77 seconds, leading to a substantial lag and eventual freezing of the application. The detailed analysis of this task reveals insights into its origin and impact:

image

Detailed Analysis of Long JavaScript Task:

  1. Long Task Summary:

image

  1. Event Level:

image

The long task was triggered by a click event, consuming a substantial amount of time in scripting, specifically within its children functions.

  1. Function Call Level:

image

The function at game.js:676:46 was responsible for the majority of the time, indicating that it contains the core logic causing the delay.

  1. hideAvailablePositions Function:

image

This function, hideAvailablePositions, primarily contributed to the delay by delegating extensive tasks to its child functions.

  1. _restorePieces Function:

image

The _restorePieces function, which handles significant processing, was one of the major contributors to the task's total execution time.

  1. drawPiece Function:

image

The drawPiece function, while not the main contributor, still took a significant amount of time due to processing of drawing operations.

  1. restore Function:

The restore function was responsible for a substantial amount of the time, suggesting it is a critical point of performance issues.

  1. CPP GC (Garbage Collection):

Garbage Collection (GC) in C++ had minimal impact compared to the JavaScript tasks, indicating that the primary performance issues are related to the JavaScript execution.

The long JavaScript task that caused the game to freeze for 13.77 seconds was primarily due to extensive processing within functions like hideAvailablePositions, _restorePieces, and drawPiece. The performance issue appears to be rooted in inefficient handling of rendering and state management tasks.

Key Findings:

@tedhyu

TKanX commented 1 month ago

Optimized piece rendering and caching

https://github.com/tridecco/game-client-web/commit/16aed06d935f13b4f4d66df283efa8b7f67c9a64

Changes:

TKanX commented 1 month ago

Performance and rendering issues have been successfully resolved. After optimizing the piece rendering process and introducing a caching mechanism, the performance has significantly improved. I re-recorded the performance metrics, and there are no more signs of lag or unresponsiveness. The memory usage is now between 25-40 MB.

image

@tedhyu