Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions src/Microsoft.Extensions.ML/PredictionEnginePoolPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.ObjectPool;
using Microsoft.ML;

namespace Microsoft.Extensions.ML
{
/// <summary>
/// <see cref="IPooledObjectPolicy{T}"/> for <see cref="PredictionEngine{TData, TPrediction}"/>
/// <see cref="PooledObjectPolicy{T}"/> for <see cref="PredictionEngine{TData, TPrediction}"/>
/// which is responsible for creating pooled objects, and when to return objects to the pool.
/// </summary>
internal class PredictionEnginePoolPolicy<TData, TPrediction>
: IPooledObjectPolicy<PredictionEngine<TData, TPrediction>>
: PooledObjectPolicy<PredictionEngine<TData, TPrediction>>
where TData : class
where TPrediction : class, new()
{
private readonly MLContext _mlContext;
private readonly ITransformer _model;
private readonly List<WeakReference> _references;

/// <summary>
/// Initializes a new instance of <see cref="PredictionEnginePoolPolicy{TData, TPrediction}"/>.
Expand All @@ -34,21 +30,13 @@ public PredictionEnginePoolPolicy(MLContext mlContext, ITransformer model)
{
_mlContext = mlContext;
_model = model;
_references = new List<WeakReference>();
}

/// <inheritdoc />
public PredictionEngine<TData, TPrediction> Create()
{
var engine = _mlContext.Model.CreatePredictionEngine<TData, TPrediction>(_model);
_references.Add(new WeakReference(engine));
return engine;
}
public override PredictionEngine<TData, TPrediction> Create() =>
_mlContext.Model.CreatePredictionEngine<TData, TPrediction>(_model);

/// <inheritdoc />
public bool Return(PredictionEngine<TData, TPrediction> obj)
{
return _references.Any(x => x.Target == obj);
}
public override bool Return(PredictionEngine<TData, TPrediction> obj) => true;
}
}