A source-level audit found several avoidable managed allocations in ML.NET inference, training, and shared data-pipeline hot paths. These occur per row, example, class, or training iteration rather than only during model or pipeline initialisation.
| Area |
Evidence |
Concern |
| TensorFlow inference |
src/Microsoft.ML.TensorFlow/TensorflowTransform.cs:756,903, TensorflowUtils.cs:447 |
Creates a Runner, input/output arrays, shape array, and full input buffer per row. |
| ONNX inference |
src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs:689, OnnxUtils.cs:559,570 |
Copies each input and shape into new arrays and creates a new input list per row. |
| SDCA training |
src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs:847, SdcaMulticlass.cs:265 |
Capturing lambdas allocate inside the innermost compare-and-swap loop. Multiclass multiplies this by class count and retry count. |
| Shared vector processing |
src/Microsoft.ML.DataView/VBuffer.cs:342,471 |
VBuffer.Items() uses a yield iterator, producing an allocation for each enumeration. Hot callers include FFM, FastTree and prediction materialisation. |
| Multiclass inference |
src/Microsoft.ML.StandardTrainers/Standard/MulticlassClassification/OneVersusAllTrainer.cs:636,729,885,897 |
Invokes Parallel.For with a captured delegate per row. Softmax also creates a double[classCount] array per prediction. |
| Prediction output |
src/Microsoft.ML.Data/DataView/TypedCursor.cs:459-469 |
Sparse vector outputs allocate a new array even when using the reusable PredictionEngine.Predict(..., ref prediction) overload. |
| Text processing |
src/Microsoft.ML.Transforms/Text/NgramTransform.cs:698,715 |
TF-IDF and IDF getters construct capturing delegates per row. |
| Cursor consolidation |
src/Microsoft.ML.Data/Data/DataViewUtils.cs:406,728 |
Creates a Batch and one BatchColumn wrapper per active column at every batch boundary. |
| Enumerable ingestion |
src/Microsoft.ML.Data/DataView/DataViewConstructionUtils.cs:395,400 |
Key conversion through Convert.ChangeType boxes values twice per row. |
The existing performance suite does not adequately detect these regressions:
RecommendedConfig does not enable BenchmarkDotNet's MemoryDiagnoser.
BenchmarksTest checks only that benchmarks build and execute, not allocation thresholds.
- The batch inference benchmarks at
StochasticDualCoordinateAscentClassifierBench.cs:166-172 call the lazy Transform method without enumerating its output, so they do not measure scoring.
Describe the solution you'd like
Add allocation-focused benchmarks and remove the highest-frequency allocations while preserving numerical behaviour and existing concurrency guarantees.
Acceptance criteria:
- Allocation and throughput results are recorded before and after each change.
- Fixed-shape ONNX and TensorFlow inference no longer copies full input vectors into new managed arrays on every row where backend lifetime rules permit reuse.
- SDCA performs no delegate or closure allocation per dual update.
- Targeted FFM, FastTree and prediction materialisation paths do not allocate iterator objects per row.
- Reused sparse prediction outputs do not allocate a replacement array when their length is unchanged.
- Numerical output and deterministic single-threaded training behaviour remain unchanged.
- Buffers remain scoped per mapper, cursor, or prediction engine where required for thread safety.
Describe alternatives you've considered
Relying on garbage collection or GC tuning would only mitigate the symptoms and would not remove allocation volume from the hottest loops.
Advising callers to use PredictionEngine.Predict(..., ref prediction) is useful, but it addresses only the result-object allocation. It does not fix tensor copies, sparse output arrays, multiclass scoring temporaries, or training allocations.
Global shared buffer pools were also considered, but per-engine or per-cursor reuse is safer because PredictionEngine and several mapper caches are intentionally not thread-safe.
Additional context
ML.NET already has suitable allocation-conscious patterns, particularly VBufferEditor.Create(ref destination, ...), reusable getter buffers, and preallocated trainer scratch arrays. The proposed changes should extend those existing patterns rather than introduce broadly shared mutable state.
This issue is based on static source inspection. Runtime profiling and allocation baselines should be added before implementation to quantify each finding and prioritise the work.
A source-level audit found several avoidable managed allocations in ML.NET inference, training, and shared data-pipeline hot paths. These occur per row, example, class, or training iteration rather than only during model or pipeline initialisation.
src/Microsoft.ML.TensorFlow/TensorflowTransform.cs:756,903,TensorflowUtils.cs:447Runner, input/output arrays, shape array, and full input buffer per row.src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs:689,OnnxUtils.cs:559,570src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs:847,SdcaMulticlass.cs:265src/Microsoft.ML.DataView/VBuffer.cs:342,471VBuffer.Items()uses ayielditerator, producing an allocation for each enumeration. Hot callers include FFM, FastTree and prediction materialisation.src/Microsoft.ML.StandardTrainers/Standard/MulticlassClassification/OneVersusAllTrainer.cs:636,729,885,897Parallel.Forwith a captured delegate per row. Softmax also creates adouble[classCount]array per prediction.src/Microsoft.ML.Data/DataView/TypedCursor.cs:459-469PredictionEngine.Predict(..., ref prediction)overload.src/Microsoft.ML.Transforms/Text/NgramTransform.cs:698,715src/Microsoft.ML.Data/Data/DataViewUtils.cs:406,728Batchand oneBatchColumnwrapper per active column at every batch boundary.src/Microsoft.ML.Data/DataView/DataViewConstructionUtils.cs:395,400Convert.ChangeTypeboxes values twice per row.The existing performance suite does not adequately detect these regressions:
RecommendedConfigdoes not enable BenchmarkDotNet'sMemoryDiagnoser.BenchmarksTestchecks only that benchmarks build and execute, not allocation thresholds.StochasticDualCoordinateAscentClassifierBench.cs:166-172call the lazyTransformmethod without enumerating its output, so they do not measure scoring.Describe the solution you'd like
Add allocation-focused benchmarks and remove the highest-frequency allocations while preserving numerical behaviour and existing concurrency guarantees.
PredictionEngineoverloads, including dense and sparse vector outputs.IDataView.VBuffer.Items()calls with span/index-based loops or an allocation-free enumerator.Parallel.Forbelow a measured class-count threshold.VBuffer.CopyTo(Span<T>).Convert.ChangeTypecalls with converters selected during getter construction.Acceptance criteria:
Describe alternatives you've considered
Relying on garbage collection or GC tuning would only mitigate the symptoms and would not remove allocation volume from the hottest loops.
Advising callers to use
PredictionEngine.Predict(..., ref prediction)is useful, but it addresses only the result-object allocation. It does not fix tensor copies, sparse output arrays, multiclass scoring temporaries, or training allocations.Global shared buffer pools were also considered, but per-engine or per-cursor reuse is safer because
PredictionEngineand several mapper caches are intentionally not thread-safe.Additional context
ML.NET already has suitable allocation-conscious patterns, particularly
VBufferEditor.Create(ref destination, ...), reusable getter buffers, and preallocated trainer scratch arrays. The proposed changes should extend those existing patterns rather than introduce broadly shared mutable state.This issue is based on static source inspection. Runtime profiling and allocation baselines should be added before implementation to quantify each finding and prioritise the work.