Skip to content

fix issue 4528, use thread safe ConcurrentDictionary instead of Dictionary #4570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 13, 2019
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
11 changes: 6 additions & 5 deletions src/Microsoft.Extensions.ML/PredictionEnginePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Microsoft.ML;
Expand All @@ -21,7 +22,7 @@ public class PredictionEnginePool<TData, TPrediction>
private readonly IOptionsFactory<PredictionEnginePoolOptions<TData, TPrediction>> _predictionEngineOptions;
private readonly IServiceProvider _serviceProvider;
private readonly PoolLoader<TData,TPrediction> _defaultEnginePool;
private readonly Dictionary<string, PoolLoader<TData, TPrediction>> _namedPools;
private readonly ConcurrentDictionary<string, PoolLoader<TData, TPrediction>> _namedPools;

public PredictionEnginePool(IServiceProvider serviceProvider,
IOptions<MLOptions> mlContextOptions,
Expand All @@ -38,7 +39,7 @@ public PredictionEnginePool(IServiceProvider serviceProvider,
_defaultEnginePool = new PoolLoader<TData, TPrediction>(_serviceProvider, defaultOptions);
}

_namedPools = new Dictionary<string, PoolLoader<TData, TPrediction>>();
_namedPools = new ConcurrentDictionary<string, PoolLoader<TData, TPrediction>>();
}

/// <summary>
Expand Down Expand Up @@ -79,9 +80,9 @@ public PredictionEngine<TData, TPrediction> GetPredictionEngine()
/// </param>
public PredictionEngine<TData, TPrediction> GetPredictionEngine(string modelName)
{
if (_namedPools.ContainsKey(modelName))
if (_namedPools.TryGetValue(modelName, out var existingPool))
{
return _namedPools[modelName].PredictionEnginePool.Get();
return existingPool.PredictionEnginePool.Get();
}

//This is the case where someone has used string.Empty to get the default model.
Expand All @@ -100,7 +101,7 @@ public PredictionEngine<TData, TPrediction> GetPredictionEngine(string modelName
//Here we are in the world of named models where the model hasn't been built yet.
var options = _predictionEngineOptions.Create(modelName);
var pool = new PoolLoader<TData, TPrediction>(_serviceProvider, options);
_namedPools.Add(modelName, pool);
pool = _namedPools.GetOrAdd(modelName, pool);
return pool.PredictionEnginePool.Get();
}

Expand Down