11namespace Microsoft . ComponentDetection . Orchestrator . Experiments ;
22
33using System ;
4+ using System . Collections . Concurrent ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Threading . Tasks ;
1213/// <inheritdoc />
1314public class ExperimentService : IExperimentService
1415{
15- private readonly List < ( IExperimentConfiguration Config , ExperimentResults ExperimentResults ) > experiments ;
16+ private readonly ConcurrentDictionary < IExperimentConfiguration , ExperimentResults > experiments ;
1617 private readonly IEnumerable < IExperimentProcessor > experimentProcessors ;
1718 private readonly ILogger < ExperimentService > logger ;
1819
@@ -27,7 +28,8 @@ public ExperimentService(
2728 IEnumerable < IExperimentProcessor > experimentProcessors ,
2829 ILogger < ExperimentService > logger )
2930 {
30- this . experiments = configs . Select ( x => ( x , new ExperimentResults ( ) ) ) . ToList ( ) ;
31+ this . experiments = new ConcurrentDictionary < IExperimentConfiguration , ExperimentResults > (
32+ configs . ToDictionary ( config => config , _ => new ExperimentResults ( ) ) ) ;
3133 this . experimentProcessors = experimentProcessors ;
3234 this . logger = logger ;
3335 }
@@ -61,18 +63,18 @@ public void RecordDetectorRun(IComponentDetector detector, IEnumerable<DetectedC
6163 }
6264 }
6365
64- private void FilterExperiments ( IComponentDetector detector , int count ) =>
65- this . experiments . RemoveAll ( experiment =>
66- {
67- var shouldRemove = ! experiment . Config . ShouldRecord ( detector , count ) ;
68-
69- if ( shouldRemove )
70- {
71- this . logger . LogDebug ( "Removing {Experiment} from active experiments" , experiment . Config . Name ) ;
72- }
66+ private void FilterExperiments ( IComponentDetector detector , int count )
67+ {
68+ var experimentsToRemove = this . experiments
69+ . Where ( x => ! x . Key . ShouldRecord ( detector , count ) )
70+ . Select ( x => x . Key )
71+ . ToList ( ) ;
7372
74- return shouldRemove ;
75- } ) ;
73+ foreach ( var config in experimentsToRemove . Where ( config => this . experiments . TryRemove ( config , out _ ) ) )
74+ {
75+ this . logger . LogDebug ( "Removing {Experiment} from active experiments" , config . Name ) ;
76+ }
77+ }
7678
7779 /// <inheritdoc />
7880 public async Task FinishAsync ( )
0 commit comments