Skip to content

Commit

Permalink
perf: Only subscribe to OnUpdate if it is overriden
Browse files Browse the repository at this point in the history
perf: Micro optimizations that probably wont matter
  • Loading branch information
thiagomvas committed May 29, 2024
1 parent 48ae99f commit b59e460
Show file tree
Hide file tree
Showing 22 changed files with 23 additions and 188 deletions.
15 changes: 0 additions & 15 deletions Basalt.Raylib/Components/BoxRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ public BoxRenderer(Entity entity) : base(entity)
{
}

/// <summary>
/// Called when the component starts.
/// </summary>
public override void OnStart()
{

}

/// <summary>
/// Called every frame to update the component.
/// </summary>
public override void OnUpdate()
{

}

/// <summary>
/// Called every frame to render the component.
Expand Down
11 changes: 0 additions & 11 deletions Basalt.Raylib/Components/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ public Button(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
Expand Down
7 changes: 0 additions & 7 deletions Basalt.Raylib/Components/EntityLineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ public override void OnStart()
}
}

/// <summary>
/// Called every frame to update the component.
/// </summary>
public override void OnUpdate()
{
}

/// <summary>
/// Called every frame to render the component.
/// </summary>
Expand Down
11 changes: 0 additions & 11 deletions Basalt.Raylib/Components/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ public Image(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
Expand Down
14 changes: 1 addition & 13 deletions Basalt.Raylib/Components/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ public Label(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
{
Expand All @@ -58,7 +46,7 @@ public override void OnUIRender()
Raylib_cs.Raylib.DrawTextPro(Raylib_cs.Raylib.GetFontDefault(),
Text,
position,
Raylib_cs.Raylib.MeasureTextEx(Raylib_cs.Raylib.GetFontDefault(), Text, FontSize, Spacing) / 2,
Raylib_cs.Raylib.MeasureTextEx(Raylib_cs.Raylib.GetFontDefault(), Text, FontSize, Spacing) * 0.5f,
Rotation,
FontSize,
Spacing,
Expand Down
13 changes: 0 additions & 13 deletions Basalt.Raylib/Components/LineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ public LineRenderer(Entity entity) : base(entity)
{
}

/// <summary>
/// Called when the component is started.
/// </summary>
public override void OnStart()
{
}

/// <summary>
/// Called every frame to update the component.
/// </summary>
public override void OnUpdate()
{
}

/// <summary>
/// Called every frame to render the component.
Expand Down
9 changes: 0 additions & 9 deletions Basalt.Raylib/Components/ModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ public ModelRenderer(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{
}

/// <inheritdoc/>
public override void OnUpdate()
{
}

/// <inheritdoc/>
public override unsafe void OnRender()
Expand Down
9 changes: 0 additions & 9 deletions Basalt.Raylib/Components/Panel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ public Panel(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{
}

/// <inheritdoc/>
public override void OnUpdate()
{
}

/// <inheritdoc/>
public override void OnUIRender()
Expand Down
11 changes: 0 additions & 11 deletions Basalt.Raylib/Components/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ public ProgressBar(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
Expand Down
9 changes: 0 additions & 9 deletions Basalt.Raylib/Components/SphereRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public SphereRenderer(Entity entity) : base(entity)

}

/// <inheritdoc/>
public override void OnStart()
{
}

/// <inheritdoc/>
public override void OnUpdate()
{
}

/// <inheritdoc/>
public override unsafe void OnRender()
Expand Down
10 changes: 5 additions & 5 deletions Basalt.TestField/Components/DebugInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public override void OnPhysicsUpdate()
public override void OnUIRender()
{
DrawFPS(10, 10);
DrawText($"Entities: {Engine.Instance.EntityManager.EntityCount}", 10, 30, 18, Color.DarkGreen);
DrawText($"Threads Used: {Process.GetCurrentProcess().Threads.Count}", 10, 50, 18, Color.DarkGreen);
DrawText($"RAM Used: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB", 10, 70,18, Color.DarkGreen);
DrawText($"Average Physics DeltaTime: {pastPhysicsDeltaTime.Average()} ms", 10, 90, 18, Color.DarkGreen);
DrawText($"Average FPS: {pastFps.Average()}", 10, 110, 18, Color.DarkGreen);
//DrawText($"Entities: {Engine.Instance.EntityManager.EntityCount}", 10, 30, 18, Color.DarkGreen);
//DrawText($"Threads Used: {Process.GetCurrentProcess().Threads.Count}", 10, 50, 18, Color.DarkGreen);
//DrawText($"RAM Used: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB", 10, 70,18, Color.DarkGreen);
//DrawText($"Average Physics DeltaTime: {pastPhysicsDeltaTime.Average()} ms", 10, 90, 18, Color.DarkGreen);
//DrawText($"Average FPS: {pastFps.Average()}", 10, 110, 18, Color.DarkGreen);
}
}
}
2 changes: 1 addition & 1 deletion Basalt.TestField/Components/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Basalt.TestField.Components
{
public class PlayerController : Component
{
public float MoveSpeed = 25;
public float MoveSpeed = 10;
private IInputSystem inputSystem;
public PlayerController(Entity entity) : base(entity)
{
Expand Down
2 changes: 1 addition & 1 deletion Basalt.TestField/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@



TestingUtils.SetupTestingScene();
TestingUtils.SetupTestingScene(250);
TestingUtils.SetupDebugInfo();
6 changes: 3 additions & 3 deletions Basalt.TestField/TestingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Basalt.TestField
{
public static class TestingUtils
{
public static void SetupTestingScene(int boxCount = 10, int ropeLength = 20)
public static void SetupTestingScene(int boxCount = 50, int ropeLength = 20)
{
var ground = new Entity();
ground.Transform.Position = new Vector3(0, -1, 0);
Expand All @@ -24,8 +24,8 @@ public static void SetupTestingScene(int boxCount = 10, int ropeLength = 20)
{
var box = new Entity();
box.Transform.Position = new Vector3(Random.Shared.Next(-30, 30), 25 + i, Random.Shared.Next(-30, 30));
box.AddComponent(new ModelRenderer(box) { ModelCacheKey = "robot", Size = new Vector3((i + 1) / 5f), Offset = -Vector3.UnitY * (i + 1) / 2.5f });
box.AddComponent(new BoxCollider(box) { Size = new Vector3(i) });
box.AddComponent(new ModelRenderer(box) { ModelCacheKey = "knot", Size = Vector3.One });
box.AddComponent(new BoxCollider(box) { Size = Vector3.One });
box.AddComponent(new Rigidbody(box) { IsKinematic = false, Mass = 1 });

Engine.CreateEntity(box);
Expand Down
20 changes: 0 additions & 20 deletions Basalt/Common/Components/BoxCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ public BoxCollider(Entity entity) : base(entity)
{

}

/// <summary>
/// Called when the component starts.
/// </summary>
public override void OnStart()
{
}

/// <summary>
/// Called every frame to update the component.
/// </summary>
public override void OnUpdate() { }

/// <summary>
/// Called when the component needs to be rendered.
/// </summary>
public override void OnRender()
{

}
}

}
8 changes: 0 additions & 8 deletions Basalt/Common/Components/ChainLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ public ChainLink(Entity entity) : base(entity)
{
}

public override void OnStart()
{

}
public override void OnUpdate()
{

}
public override void OnPhysicsUpdate()
{
// Calculate the vector between the two entities
Expand Down
12 changes: 7 additions & 5 deletions Basalt/Common/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public virtual void OnRender()
/// <summary>
/// Called when the component is started.
/// </summary>
public abstract void OnStart();
public virtual void OnStart() { }

/// <summary>
/// Called every frame to update the component.
/// </summary>
public abstract void OnUpdate();
public virtual void OnUpdate() { }

/// <summary>
/// Called every physics update to update the component's physics.
Expand Down Expand Up @@ -110,14 +110,16 @@ private virtual protected void SubscribeToEvents()
var eventbus = Engine.Instance.GetEngineComponent<IEventBus>()!;
Type type = this.GetType();

eventbus.Subscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent);
// Check if OnUpdate was overriden
if (type.GetMethod(nameof(OnUpdate))!.DeclaringType != typeof(Component))
eventbus.Subscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent);

// Check if OnRender was overriden
if (type.GetMethod(nameof(OnRender)).DeclaringType != typeof(Component))
if (type.GetMethod(nameof(OnRender))!.DeclaringType != typeof(Component))
eventbus.Subscribe(BasaltConstants.RenderEventKey, OnRenderEvent);

// Check if OnPhysicsUpdate was overriden
if (type.GetMethod(nameof(OnPhysicsUpdate)).DeclaringType != typeof(Component))
if (type.GetMethod(nameof(OnPhysicsUpdate))!.DeclaringType != typeof(Component))
eventbus.Subscribe(BasaltConstants.PhysicsUpdateEventKey, OnPhysicsUpdateEvent);
}

Expand Down
9 changes: 0 additions & 9 deletions Basalt/Common/Components/FixedLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ public FixedLink(Entity entity) : base(entity)
{
}

public override void OnStart()
{

}

public override void OnUpdate()
{
// Optional: Add any behavior you want to occur during regular updates
}

public override void OnPhysicsUpdate()
{
Expand Down
7 changes: 0 additions & 7 deletions Basalt/Common/Components/Rigidbody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ public override void OnStart()

}

/// <summary>
/// Called on each frame update.
/// </summary>
public override void OnUpdate()
{
}

public void AddForce(Vector3 force, ForceType type = ForceType.Force)
{
if (IsKinematic)
Expand Down
15 changes: 0 additions & 15 deletions Basalt/Common/Components/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,5 @@ public Transform(Entity entity) : base(entity)
Position = new Vector3();
}

/// <summary>
/// Called when the component starts.
/// </summary>
public override void OnStart()
{

}

/// <summary>
/// Called every frame to update the component.
/// </summary>
public override void OnUpdate()
{

}
}
}
8 changes: 4 additions & 4 deletions Basalt/Common/Physics/CollisionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ private static void BoxBoxCollision(Collider col1, Collider col2)
return; // Cannot collide with itself
}

Vector3 extents1 = box1.Size / 2f;
Vector3 extents2 = box2.Size / 2f;
Vector3 extents1 = box1.Size * 0.5f;
Vector3 extents2 = box2.Size * 0.5f;

// Calculate the min and max points of the two colliders along each axis
Vector3 min1 = box1.Position - extents1;
Expand Down Expand Up @@ -140,8 +140,8 @@ private static void BoxBoxCollision(Collider col1, Collider col2)
rb2.Velocity -= impulse / rb2.Mass;
}

box1.Entity.Transform.Position += separationDirection * separationDistance / 2f;
box2.Entity.Transform.Position -= separationDirection * separationDistance / 2f;
box1.Entity.Transform.Position += separationDirection * separationDistance * 0.5f;
box2.Entity.Transform.Position -= separationDirection * separationDistance * 0.5f;
}
}
}
Expand Down
Loading

0 comments on commit b59e460

Please sign in to comment.