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

SceneObject.Update() performance hit #16

Open
11 tasks
aneteanetes opened this issue May 9, 2023 · 0 comments
Open
11 tasks

SceneObject.Update() performance hit #16

aneteanetes opened this issue May 9, 2023 · 0 comments

Comments

@aneteanetes
Copy link
Owner

SceneObject Update system

ISceneObject have Updatable bool property and ComponentUpdateChainCall(GameTimeLoop gameTime) method. IGameClient implementation can call ComponentUpdateChainCall when game Update part (separated from drawing) is active. IGameClient implementation can check component update is possible by Updatable property (which default is 'true').

ComponentUpdateCompatibility

DungeonGlobal have ComponentUpdateCompatibility property. This property can be used by IGameClient implementation and target game for controlling Updatable check. It should be removed.

ComponentUpdateChainCall implementation

  1. SceneObject call component Update() method
  2. Elapsed calculated
  3. Default frame-based animation calculated
  4. AfterUpdate invoked

It's ok for now not use Update name of method at top of abstraction.

Dungeon.Monogame implementation

When Update part is active, implementation follows:

  1. Controls update events (check block and scene switch)
  2. For each layer.objects call UpdateComponent
  3. If object is updatable, it calls ComponentUpdateChainCall
  4. For each children calls UpdateComponent

Issue

Dungeon.Monogame implementation 2 and 4 - harm for performance. Its clearly seen when there are more than 10.000 components, and even on 1.5k components: Update method takes so long, that the FPS drops to 0.

Solution

All of this for Dungeon.Monogame:

  • Remove DungeonGlobal.ComponentUpdateCompatibility
  • Rename ComponentUpdateChainCall to ComponentUpdate
  • Add ISceneLayer property ISceneObject[] Updatables {get;} (name optional)
  • Add ISceneLayer void AddUpdatable()
  • New update logic (below)
  • New add updatable logic (below)
  • In GameClient.UpdateLoop:24 iterate by SceneLayer.Updatables
  • Remove GameClient.UpdateComponent recursive call for children

New update logic

When ISceneLayer.AddObject called, check for Updatable and if it is, add to array. (perfer way is resize array, not as SceneLayer.Objects prop)

New add updatable logic

For composited objects in composition tree (e.g. AddObject inside ISceneObject) do like SceneControl.AddControl works:

  1. When AddObject call
  2. If SceneObject is Updatable
  3. Call ISceneLayer.AddUpdatable
  4. Inside SceneLayer check for duplicates etc, resize array

Optional:

  • Add ISceneLayer.Updatable property
  • Add parameter for Scene.AddLayer(..., bool updatable=false)
  • Add GameClient.Update check for scene is updatable

Important

  • Avoid hardcopy of elements
  • Avoid foreach
  • Avoid recursive calls
  • Avoid unnecessary iterations
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

No branches or pull requests

1 participant