Reference

Benchmarks

Measurements of what a train costs over plain code, taken with BenchmarkDotNet. The suite is Trax.Effect/tests/Trax.Effect.Tests.Benchmarks/. What the numbers mean for choosing where to use a train is in Train overhead.

Test Environment

BenchmarkDotNet v0.14.0, CachyOS
AMD Ryzen 7 7840U w/ Radeon 780M Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 10.0.103
  [Host] : .NET 10.0.3, X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

What's Being Measured

Three execution modes are compared for identical workloads. The suite also has a fourth, EffectTrain_InMemory_* (a ServiceTrain with the in-memory data provider), which the tables below do not report.

ModeDescription
SerialPlain function calls, no framework, no abstractions
BaseTrainTrain<TIn, TOut>, the core Chain/Resolve pipeline with Either error handling
ServiceTrain (no effects)ServiceTrain<TIn, TOut>, full DI-resolved train with effect runner lifecycle, but no effect providers registered

Train Overhead

MethodMeanAllocated
Serial - Add 10.23 ns-
BaseTrain - Add 11,564 ns3,688 B
ServiceTrain - Add 17,061 ns7,176 B
Serial - Add 3 (3 junctions)0.23 ns-
BaseTrain - Add 31,966 ns4,536 B
ServiceTrain - Add 37,696 ns8,024 B
Serial - Transform (DTO → entity)19.7 ns152 B
BaseTrain - Transform1,340 ns1,648 B
ServiceTrain - Transform6,889 ns5,232 B
Serial - Simulated I/O (3× Task.Yield)1,053 ns112 B
BaseTrain - Simulated I/O3,747 ns4,992 B
ServiceTrain - Simulated I/O9,516 ns8,479 B

Scaling with Junction Count

JunctionsSerialBaseTrainServiceTrainBase Overhead/JunctionEffect Overhead/Junction
14.7 ns1,630 ns7,186 ns--
34.7 ns1,987 ns7,622 ns~179 ns~218 ns
55.1 ns2,468 ns8,079 ns~210 ns~223 ns
1010.7 ns3,440 ns9,016 ns~201 ns~203 ns

Each additional junction adds roughly 200 ns in both train modes: junction instantiation, type mapping, and Either propagation through the chain.

Memory Scaling

JunctionsBaseTrainServiceTrain
13,688 B7,176 B
34,536 B8,024 B
55,384 B8,872 B
107,720 B11,352 B

Each additional junction allocates roughly 424 B (BaseTrain) or 464 B (ServiceTrain).

Where the Overhead Comes From

SourceApproximate Cost
Train base class instantiation + Either wrapping~1.3 μs
Per-junction: type resolution, Chain<T> dispatch, Either bind~200 ns/junction
DI scope creation (CreateScope)~1 μs
Effect runner lifecycle (initialize + save, no providers)~4.5 μs

Running the Benchmarks Yourself

From the repository root:

cd Trax.Effect/tests/Trax.Effect.Tests.Benchmarks
 
# Run all benchmarks
dotnet run -c Release -- --filter '*'
 
# Run a specific suite
dotnet run -c Release -- --filter '*TrainOverhead*'
dotnet run -c Release -- --filter '*Scaling*'
 
# List available benchmarks
dotnet run -c Release -- --list flat

Results vary by hardware; the ratios between the serial, base and service train modes vary less than the absolute numbers.