Skip to main content

DISP004: Optimization Hint

PropertyValue
Diagnostic IDDISP004
TitleOptimization hint
CategoryExcalibur.Dispatch.Performance
SeverityInfo
Enabled by defaultYes

Cause

The analyzer detected a potential performance improvement. Common suggestions include:

  • Seal the class - Handler classes that are not inherited can be sealed, enabling devirtualization
  • Use ValueTask - Methods returning Task that complete synchronously could return ValueTask to avoid allocation
  • Use readonly struct - Value types used as messages could be readonly struct to avoid defensive copies
  • Register as singleton - Stateless handlers can be registered as singletons to avoid per-request allocation

Examples

Unsealed handler class

// Info DISP004: Consider sealing 'CreateOrderHandler' for better performance
public class CreateOrderHandler : IActionHandler<CreateOrder>
{
public Task HandleAsync(CreateOrder action, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

Fix:

public sealed class CreateOrderHandler : IActionHandler<CreateOrder>
{
public Task HandleAsync(CreateOrder action, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

Synchronous handler returning Task

// Info DISP004: Consider returning ValueTask instead of Task
public sealed class PingHandler : IActionHandler<Ping>
{
public Task HandleAsync(Ping action, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

Fix:

public sealed class PingHandler : IActionHandler<Ping>
{
public ValueTask HandleAsync(Ping action, CancellationToken cancellationToken)
=> ValueTask.CompletedTask;
}

When to Suppress

Suppress this diagnostic if the optimization does not apply to your scenario or if readability is preferred over the minor performance gain.

See Also