未验证 提交 70a222a4 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #44367 from CyrusNajmabadi/experimentCache

Cache results of IsExperimentEnabled for performance
......@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Composition;
using System.Reflection;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
......@@ -21,6 +22,13 @@ internal class VisualStudioExperimentationService : ForegroundThreadAffinitizedO
private readonly MethodInfo _isCachedFlightEnabledInfo;
private readonly IVsFeatureFlags _featureFlags;
/// <summary>
/// Cache of values we've queried from the underlying VS service. These values are expected to last for the
/// lifetime of the session, so it's fine for us to cache things to avoid the heavy cost of querying for them
/// over and over.
/// </summary>
private readonly Dictionary<string, bool> _experimentEnabledMap = new Dictionary<string, bool>();
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioExperimentationService(IThreadingContext threadingContext, SVsServiceProvider serviceProvider)
......@@ -53,6 +61,29 @@ public VisualStudioExperimentationService(IThreadingContext threadingContext, SV
}
public bool IsExperimentEnabled(string experimentName)
{
ThisCanBeCalledOnAnyThread();
// First look in our cache to see if this has already been computed and cached.
lock (_experimentEnabledMap)
{
if (_experimentEnabledMap.TryGetValue(experimentName, out var result))
return result;
}
// Otherwise, compute and cache this ourselves. It's fine if multiple callers cause this to happen. We'll
// just let the last one win.
var enabled = IsExperimentEnabledWorker(experimentName);
lock (_experimentEnabledMap)
{
_experimentEnabledMap[experimentName] = enabled;
}
return enabled;
}
private bool IsExperimentEnabledWorker(string experimentName)
{
ThisCanBeCalledOnAnyThread();
if (_isCachedFlightEnabledInfo != null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册