Context
Actions are executed in hot paths (potentially hundreds of NPCs per tick). Current implementation allocates a new List<IGameEvent> on every execution.
Current Implementation
public IReadOnlyList<IGameEvent> Execute(...)
{
var events = new List<IGameEvent>(); // Allocation on every call
// ...
return events;
}
Proposed Solution
Use C# 12 collection expressions for zero-allocation fixed-size collections:
// RestAction (2 events)
return [
new NeedChangedEvent(...),
new NeedChangedEvent(...)
];
// EatAction (1 event)
return [new NeedChangedEvent(...)];
Benefits
- Zero allocation for single-element collections
- Minimal allocation for fixed-size collections
- More readable and concise
- Better performance in hot paths
Files to Update
src/FantasyRpgWorld.Core/Actions/RestAction.cs
src/FantasyRpgWorld.Core/Actions/EatAction.cs
src/FantasyRpgWorld.Core/Actions/WorkAction.cs
Acceptance Criteria
Related
Identified in PR #112 review by .NET Specialist
Part of Epic 3: NPC Decision Making
Labels
- type:refactor
- priority:medium
- area:performance
Context
Actions are executed in hot paths (potentially hundreds of NPCs per tick). Current implementation allocates a new
List<IGameEvent>on every execution.Current Implementation
Proposed Solution
Use C# 12 collection expressions for zero-allocation fixed-size collections:
Benefits
Files to Update
src/FantasyRpgWorld.Core/Actions/RestAction.cssrc/FantasyRpgWorld.Core/Actions/EatAction.cssrc/FantasyRpgWorld.Core/Actions/WorkAction.csAcceptance Criteria
Related
Identified in PR #112 review by .NET Specialist
Part of Epic 3: NPC Decision Making
Labels