private static ImmutableArray <(string analyzerId, SerializableDiagnosticMap diagnosticMap)> Dehydrate( ImmutableDictionary <DiagnosticAnalyzer, DiagnosticAnalysisResultBuilder> builderMap, BidirectionalMap <string, DiagnosticAnalyzer> analyzerToIdMap ) { using var _ = ArrayBuilder <(string analyzerId, SerializableDiagnosticMap diagnosticMap)> .GetInstance( out var diagnostics ); foreach (var(analyzer, analyzerResults) in builderMap) { var analyzerId = GetAnalyzerId(analyzerToIdMap, analyzer); diagnostics.Add( ( analyzerId, new SerializableDiagnosticMap( analyzerResults.SyntaxLocals.SelectAsArray( entry => (entry.Key, entry.Value) ), analyzerResults.SemanticLocals.SelectAsArray( entry => (entry.Key, entry.Value) ), analyzerResults.NonLocals.SelectAsArray( entry => (entry.Key, entry.Value) ), analyzerResults.Others ) ) ); } return(diagnostics.ToImmutable()); }
static ImmutableDictionary <string, AnalyzerTelemetryInfo> GetTelemetryInfo( AnalysisResult analysisResult, ImmutableArray <DiagnosticAnalyzer> analyzers, BidirectionalMap <string, DiagnosticAnalyzer> analyzerToIdMap) { Func <DiagnosticAnalyzer, bool> shouldInclude; if (analyzers.Length < analysisResult.AnalyzerTelemetryInfo.Count) { // Filter the telemetry info to the executed analyzers. using var _ = PooledHashSet <DiagnosticAnalyzer> .GetInstance(out var analyzersSet); analyzersSet.AddRange(analyzers); shouldInclude = analyzer => analyzersSet.Contains(analyzer); } else { shouldInclude = _ => true; } var telemetryBuilder = ImmutableDictionary.CreateBuilder <string, AnalyzerTelemetryInfo>(); foreach (var(analyzer, analyzerTelemetry) in analysisResult.AnalyzerTelemetryInfo) { if (shouldInclude(analyzer)) { var analyzerId = GetAnalyzerId(analyzerToIdMap, analyzer); telemetryBuilder.Add(analyzerId, analyzerTelemetry); } } return(telemetryBuilder.ToImmutable()); }
private static async Task <CompilationWithAnalyzersCacheEntry> CreateCompilationWithAnalyzersCacheEntryAsync(Project project, CancellationToken cancellationToken) { // We could consider creating a service so that we don't do this repeatedly if this shows up as perf cost using var pooledObject = SharedPools.Default <HashSet <object> >().GetPooledObject(); using var pooledMap = SharedPools.Default <Dictionary <string, DiagnosticAnalyzer> >().GetPooledObject(); var referenceSet = pooledObject.Object; var analyzerMapBuilder = pooledMap.Object; // This follows what we do in DiagnosticAnalyzerInfoCache.CheckAnalyzerReferenceIdentity using var _ = ArrayBuilder <DiagnosticAnalyzer> .GetInstance(out var analyzerBuilder); foreach (var reference in project.Solution.AnalyzerReferences.Concat(project.AnalyzerReferences)) { if (!referenceSet.Add(reference.Id)) { continue; } var analyzers = reference.GetAnalyzers(project.Language); analyzerBuilder.AddRange(analyzers); analyzerMapBuilder.AppendAnalyzerMap(analyzers); } var compilationWithAnalyzers = await CreateCompilationWithAnalyzerAsync( project, analyzerBuilder.ToImmutable(), cancellationToken).ConfigureAwait(false); var analyzerToIdMap = new BidirectionalMap <string, DiagnosticAnalyzer>(analyzerMapBuilder); return(new CompilationWithAnalyzersCacheEntry(compilationWithAnalyzers, analyzerToIdMap));
public static void Test() { //This line compiles var possiblyBad = new BidirectionalMap <string, string>(); //This causes the compiler to fail with an ambiguous invocation possiblyBad.Remove("Something"); }
static LanguageServer() { VsCodeLanguageToLanguageNameMap = new BidirectionalMap <string, string>(new[] { new KeyValuePair <string, string>("hlsl", LanguageNames.Hlsl), new KeyValuePair <string, string>("shaderlab", LanguageNames.ShaderLab) }); }
public CompilationWithAnalyzersCacheEntry( CompilationWithAnalyzers compilationWithAnalyzers, BidirectionalMap <string, DiagnosticAnalyzer> analyzerToIdMap ) { CompilationWithAnalyzers = compilationWithAnalyzers; AnalyzerToIdMap = analyzerToIdMap; }
private string GetAnalyzerId(BidirectionalMap <string, DiagnosticAnalyzer> analyzerMap, DiagnosticAnalyzer analyzer) { var analyzerId = analyzerMap.GetKeyOrDefault(analyzer); Contract.ThrowIfNull(analyzerId); return(analyzerId); }
private async Task <DiagnosticAnalysisResultMap <string, DiagnosticAnalysisResultBuilder> > AnalyzeAsync( BidirectionalMap <string, DiagnosticAnalyzer> analyzerMap, ImmutableArray <DiagnosticAnalyzer> analyzers, OptionSet options, bool reportSuppressedDiagnostics, bool logAnalyzerExecutionTime, CancellationToken cancellationToken) { // flag that controls concurrency var useConcurrent = true; // get original compilation var compilation = await _project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); // fork compilation with concurrent build. this is okay since WithAnalyzers will fork compilation // anyway to attach event queue. this should make compiling compilation concurrent and make things // faster compilation = compilation.WithOptions(compilation.Options.WithConcurrentBuild(useConcurrent)); // We need this to fork soluton, otherwise, option is cached at document. // all this can go away once we do this - https://github.com/dotnet/roslyn/issues/19284 var temporaryWorksapce = new TemporaryWorkspace(_project.Solution); // TODO: can we support analyzerExceptionFilter in remote host? // right now, host doesn't support watson, we might try to use new NonFatal watson API? var analyzerOptions = new CompilationWithAnalyzersOptions( options: new WorkspaceAnalyzerOptions(_project.AnalyzerOptions, MergeOptions(_project.Solution.Options, options), temporaryWorksapce.CurrentSolution), onAnalyzerException: OnAnalyzerException, analyzerExceptionFilter: null, concurrentAnalysis: useConcurrent, logAnalyzerExecutionTime: logAnalyzerExecutionTime, reportSuppressedDiagnostics: reportSuppressedDiagnostics); var analyzerDriver = compilation.WithAnalyzers(analyzers, analyzerOptions); // PERF: Run all analyzers at once using the new GetAnalysisResultAsync API. var analysisResult = await analyzerDriver.GetAnalysisResultAsync(cancellationToken).ConfigureAwait(false); // REVIEW: the design of current analyzer engine is that, information/states in CompilationWithAnalyzer (more specifically AnalyzerManager singleton) // will live forever until analyzer references (analyzers), which is given to CompilationWithAnalyzer, go away. // that is not suitable for OOP since OOP will create new workspace // for each analysis but share all resources including analyzer references. // until, we address this issue, OOP will clear state every time analysis is done. // // * NOTE * this only works for now since we don't run analysis on multiple threads. // // best way to fix this is doing this - https://github.com/dotnet/roslyn/issues/2830 // host should control lifetime of all information related to analyzer reference explicitly CompilationWithAnalyzers.ClearAnalyzerState(analyzers); var builderMap = analysisResult.ToResultBuilderMap(_project, VersionStamp.Default, compilation, analysisResult.Analyzers, cancellationToken); return(DiagnosticAnalysisResultMap.Create( builderMap.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), analysisResult.AnalyzerTelemetryInfo.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), _exceptions.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value.ToImmutableArray()))); }
/// <summary> /// Creates a data reader that reads from the underlying stream and returns only the columns specified in the transform. /// </summary> /// <param name="reader"></param> /// <param name="resultColumnsByColumnName"></param> public ProjectingDataReader(TDataReader reader, Dictionary <string, RowProjection <object> > resultColumnsByColumnName) : base(reader) { _columnsByName = resultColumnsByColumnName; // _columns = resultColumnsByColumnName.Select(n => n.Value).ToArray(); _fieldCount = _columns.Length; // _nameToOrdinalMapping = resultColumnsByColumnName.Select((o, i) => new { o, i }).ToBidirectionalMap(p => p.o.Key, p => p.i); }
public ProjectingDataReader(TDataReader dataReader, RowProjection <object>[] resultColumns) : base(dataReader) { _columns = resultColumns; // _columnsByName = _columns.Select((i, o) => new { Index = i, Projection = o }) .ToDictionary(p => "COLUMN" + p.Index, p => p.Index); _fieldCount = _columns.Length; // _nameToOrdinalMapping = _columnsByName.Select((o, i) => new { o, i }).ToBidirectionalMap(p => p.o.Key, p => p.i); }
private async Task <DiagnosticAnalysisResultMap <string, DiagnosticAnalysisResultBuilder> > AnalyzeAsync( BidirectionalMap <string, DiagnosticAnalyzer> analyzerMap, ImmutableArray <DiagnosticAnalyzer> analyzers, OptionSet options, bool reportSuppressedDiagnostics, bool logAnalyzerExecutionTime, CancellationToken cancellationToken) { // flag that controls concurrency var useConcurrent = true; // get original compilation var compilation = await _project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); // fork compilation with concurrent build. this is okay since WithAnalyzers will fork compilation // anyway to attach event queue. this should make compiling compilation concurrent and make things // faster compilation = compilation.WithOptions(compilation.Options.WithConcurrentBuild(useConcurrent)); // We need this to fork soluton, otherwise, option is cached at document. // all this can go away once we do this - https://github.com/dotnet/roslyn/issues/19284 using (var temporaryWorksapce = new TemporaryWorkspace(_project.Solution)) { // TODO: can we support analyzerExceptionFilter in remote host? // right now, host doesn't support watson, we might try to use new NonFatal watson API? var analyzerOptions = new CompilationWithAnalyzersOptions( options: new WorkspaceAnalyzerOptions(_project.AnalyzerOptions, MergeOptions(_project.Solution.Options, options), temporaryWorksapce.CurrentSolution), onAnalyzerException: OnAnalyzerException, analyzerExceptionFilter: null, concurrentAnalysis: useConcurrent, logAnalyzerExecutionTime: logAnalyzerExecutionTime, reportSuppressedDiagnostics: reportSuppressedDiagnostics); var analyzerDriver = compilation.WithAnalyzers(analyzers, analyzerOptions); // PERF: Run all analyzers at once using the new GetAnalysisResultAsync API. var analysisResult = await analyzerDriver.GetAnalysisResultAsync(cancellationToken).ConfigureAwait(false); // record performance if tracker is available if (_performanceTracker != null) { // +1 to include project itself _performanceTracker.AddSnapshot(analysisResult.AnalyzerTelemetryInfo.ToAnalyzerPerformanceInfo(), _project.DocumentIds.Count + 1); } var builderMap = analysisResult.ToResultBuilderMap(_project, VersionStamp.Default, compilation, analysisResult.Analyzers, cancellationToken); return(DiagnosticAnalysisResultMap.Create( builderMap.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), analysisResult.AnalyzerTelemetryInfo.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), _exceptions.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value.ToImmutableArray()))); } }
public void Test3() { var d = new BidirectionalMap <string, int>(); d.Add(new Tuple <string, int>("Col1", 1)); d.Add(new Tuple <string, int>("Col2", 2)); d.Add(new Tuple <string, int>("Col3", 3)); Assert.ThrowsException <ItemsNotOneToOneException>(() => { d.Add(new Tuple <string, int>("Col3", 1)); }); }
public void Test4() { var d = new BidirectionalMap <int, int>(); d.Add(new Tuple <int, int>(1, 5)); d.Add(new Tuple <int, int>(2, 6)); d.Add(new Tuple <int, int>(3, 7)); Assert.ThrowsException <ItemsNotOneToOneException>(() => { d.Add(new Tuple <int, int>(4, 7)); }); }
void UpdateRenderables <T>( List <T> renderables, GameObject newObject, List <GameObject> objectPool, BidirectionalMap <T, GameObject> renderableObjectMap, Transform uiContainer, System.Action <GameObject> onCreateCallback = null) where T : IRenderable { // Put all GameObjects in a set, and remove the objects that are still in used HashSet <GameObject> unusedObjects = new HashSet <GameObject>(renderableObjectMap.Values); foreach (T renderable in renderables) { GameObject uiObject; // Spawn new UI person if (!renderableObjectMap.ContainsKey(renderable)) { if (objectPool.Count > 0) { uiObject = objectPool[objectPool.Count - 1]; objectPool.RemoveAt(objectPool.Count - 1); uiObject.SetActive(true); } else { uiObject = Instantiate(newObject); } uiObject.transform.SetParent(uiContainer, false); renderableObjectMap.Add(renderable, uiObject); if (onCreateCallback != null) { onCreateCallback(uiObject); } } else { uiObject = renderableObjectMap.GetValue(renderable); unusedObjects.Remove(uiObject); } renderable.RenderTo(uiObject); } foreach (GameObject uiObject in unusedObjects) { renderableObjectMap.RemoveValue(uiObject); objectPool.Add(uiObject); uiObject.SetActive(false); uiObject.transform.SetParent(null, false); } }
private ImmutableArray <DiagnosticAnalyzer> GetAnalyzers(BidirectionalMap <string, DiagnosticAnalyzer> analyzerMap, IEnumerable <string> analyzerIds) { // TODO: this probably need to be cached as well in analyzer service? var builder = ImmutableArray.CreateBuilder <DiagnosticAnalyzer>(); foreach (var analyzerId in analyzerIds) { if (analyzerMap.TryGetValue(analyzerId, out var analyzer)) { builder.Add(analyzer); } } return(builder.ToImmutable()); }
private async Task <DiagnosticAnalysisResultMap <string, DiagnosticAnalysisResultBuilder> > AnalyzeAsync( BidirectionalMap <string, DiagnosticAnalyzer> analyzerMap, ImmutableArray <DiagnosticAnalyzer> analyzers, SkippedHostAnalyzersInfo skippedAnalyzersInfo, bool reportSuppressedDiagnostics, bool logAnalyzerExecutionTime, CancellationToken cancellationToken) { // flag that controls concurrency var useConcurrent = true; // get original compilation var compilation = await _project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false); // fork compilation with concurrent build. this is okay since WithAnalyzers will fork compilation // anyway to attach event queue. this should make compiling compilation concurrent and make things // faster compilation = compilation.WithOptions(compilation.Options.WithConcurrentBuild(useConcurrent)); // TODO: can we support analyzerExceptionFilter in remote host? // right now, host doesn't support watson, we might try to use new NonFatal watson API? var analyzerOptions = new CompilationWithAnalyzersOptions( options: new WorkspaceAnalyzerOptions(_project.AnalyzerOptions, _project.Solution), onAnalyzerException: null, analyzerExceptionFilter: null, concurrentAnalysis: useConcurrent, logAnalyzerExecutionTime: logAnalyzerExecutionTime, reportSuppressedDiagnostics: reportSuppressedDiagnostics); var analyzerDriver = compilation.WithAnalyzers(analyzers, analyzerOptions); // PERF: Run all analyzers at once using the new GetAnalysisResultAsync API. var(analysisResult, additionalPragmaSuppressionDiagnostics) = await analyzerDriver.GetAnalysisResultAsync(_project, _analyzerInfoCache, cancellationToken).ConfigureAwait(false); // record performance if tracker is available if (_performanceTracker != null) { // +1 to include project itself _performanceTracker.AddSnapshot(analysisResult.AnalyzerTelemetryInfo.ToAnalyzerPerformanceInfo(_analyzerInfoCache), _project.DocumentIds.Count + 1); } var builderMap = analysisResult.ToResultBuilderMap(additionalPragmaSuppressionDiagnostics, _project, VersionStamp.Default, compilation, analysisResult.Analyzers, skippedAnalyzersInfo, cancellationToken); return(DiagnosticAnalysisResultMap.Create( builderMap.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), analysisResult.AnalyzerTelemetryInfo.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value))); }
private async Task<DiagnosticAnalysisResultMap<string, DiagnosticAnalysisResultBuilder>> AnalyzeAsync( BidirectionalMap<string, DiagnosticAnalyzer> analyzerMap, ImmutableArray<DiagnosticAnalyzer> analyzers, bool reportSuppressedDiagnostics, bool logAnalyzerExecutionTime, CancellationToken cancellationToken) { var compilation = await _project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); // TODO: can we support analyzerExceptionFilter in remote host? // right now, host doesn't support watson, we might try to use new NonFatal watson API? var analyzerOptions = new CompilationWithAnalyzersOptions( options: new WorkspaceAnalyzerOptions(_project.AnalyzerOptions, _project.Solution.Workspace), onAnalyzerException: OnAnalyzerException, analyzerExceptionFilter: null, concurrentAnalysis: true, logAnalyzerExecutionTime: logAnalyzerExecutionTime, reportSuppressedDiagnostics: reportSuppressedDiagnostics); var analyzerDriver = compilation.WithAnalyzers(analyzers, analyzerOptions); // PERF: Run all analyzers at once using the new GetAnalysisResultAsync API. var analysisResult = await analyzerDriver.GetAnalysisResultAsync(cancellationToken).ConfigureAwait(false); // REVIEW: the design of current analyzer engine is that, information/states in CompilationWithAnalyzer (more specifically AnalyzerManager singleton) // will live forever until analyzer references (analyzers), which is given to CompilationWithAnalyzer, go away. // that is not suitable for OOP since OOP will create new workspace // for each analysis but share all resources including analyzer references. // until, we address this issue, OOP will clear state every time analysis is done. // // * NOTE * this only works for now since we don't run analysis on multiple threads. // // best way to fix this is doing this - https://github.com/dotnet/roslyn/issues/2830 // host should control lifetime of all information related to analyzer reference explicitly CompilationWithAnalyzers.ClearAnalyzerState(analyzers); var builderMap = analysisResult.ToResultBuilderMap(_project, VersionStamp.Default, compilation, analysisResult.Analyzers, cancellationToken); return DiagnosticAnalysisResultMap.Create( builderMap.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), analysisResult.AnalyzerTelemetryInfo.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value), _exceptions.ToImmutableDictionary(kv => GetAnalyzerId(analyzerMap, kv.Key), kv => kv.Value.ToImmutableArray())); }
public void Test6() { var d = new BidirectionalMap <string, int>(new Dictionary <string, int>() { { "Col1", 1 }, { "Col2", 2 }, { "Col3", 3 } }); Assert.AreEqual(d.Count, 3); Assert.AreEqual(d.GetLeft(1), "Col1"); Assert.AreEqual(d.GetRight("Col3"), 3); Assert.ThrowsException <ItemsNotOneToOneException>(() => { d.Add(new Tuple <string, int>("Col1", 3)); }); }
public void Test5() { var d = new BidirectionalMap <int, int>(); d.Add(new Tuple <int, int>(1, 5)); d.Add(new Tuple <int, int>(2, 6)); d.Add(new Tuple <int, int>(3, 7)); Assert.AreEqual(d.Count, 3); Assert.AreEqual(d.GetLeft(7), 3); Assert.AreEqual(d.GetRight(3), 7); Assert.ThrowsException <Exception>(() => { var e = d.GetLeft(11); }); Assert.ThrowsException <Exception>(() => { var e = d.GetRight(11); }); }
private async Task <DiagnosticAnalysisResultMap <string, DiagnosticAnalysisResultBuilder> > AnalyzeAsync( CompilationWithAnalyzers compilationWithAnalyzers, BidirectionalMap <string, DiagnosticAnalyzer> analyzerToIdMap, ImmutableArray <DiagnosticAnalyzer> analyzers, SkippedHostAnalyzersInfo skippedAnalyzersInfo, bool reportSuppressedDiagnostics, bool logPerformanceInfo, bool getTelemetryInfo, CancellationToken cancellationToken) { var documentAnalysisScope = _document != null ? new DocumentAnalysisScope(_document, _span, analyzers, _analysisKind !.Value) : null; var(analysisResult, additionalPragmaSuppressionDiagnostics) = await compilationWithAnalyzers.GetAnalysisResultAsync( documentAnalysisScope, _project, _analyzerInfoCache, cancellationToken).ConfigureAwait(false); // Record performance if tracker is available if (logPerformanceInfo && _performanceTracker != null) { // +1 to include project itself var unitCount = documentAnalysisScope != null ? 1 : _project.DocumentIds.Count + 1; _performanceTracker.AddSnapshot(analysisResult.AnalyzerTelemetryInfo.ToAnalyzerPerformanceInfo(_analyzerInfoCache), unitCount); } var builderMap = await analysisResult.ToResultBuilderMapAsync( additionalPragmaSuppressionDiagnostics, documentAnalysisScope, _project, VersionStamp.Default, compilationWithAnalyzers.Compilation, analyzers, skippedAnalyzersInfo, reportSuppressedDiagnostics, cancellationToken).ConfigureAwait(false); var result = builderMap.ToImmutableDictionary(kv => GetAnalyzerId(analyzerToIdMap, kv.Key), kv => kv.Value); var telemetry = getTelemetryInfo ? GetTelemetryInfo(analysisResult, analyzers, analyzerToIdMap) : ImmutableDictionary <string, AnalyzerTelemetryInfo> .Empty; return(DiagnosticAnalysisResultMap.Create(result, telemetry));
public HeaderDataReader(IDataReader dataReader, HeaderReaderConfiguration configuration) : base(dataReader) { this._configuration = configuration; //build col info _columnsLazy = new Lazy <SimpleColumnInfo[]>(() => { var d = HeaderInformation(); return(d); }); //build col names index _columnNamesToIndex = new Lazy <BidirectionalMap <string, int> >(() => { var cols = _columnsLazy.Value .Select(c => new KeyValuePair <string, int>(c.ColumnName, c.Index)) .ToArray(); var r = new BidirectionalMap <string, int>(cols); return(r); }); }
/*************************/ // Constructor /*************************/ // Basic constructor public LinearRepresentativeList() { _map = new BidirectionalMap<BitSet, BitSet>(); }
/*************************/ // Constructor /*************************/ // Basic constructor public RepresentativeList() { Map = new BidirectionalMap<BitSet, dNeighborhood>(); }
private string GetAnalyzerId(BidirectionalMap<string, DiagnosticAnalyzer> analyzerMap, DiagnosticAnalyzer analyzer) { var analyzerId = analyzerMap.GetKeyOrDefault(analyzer); Contract.ThrowIfNull(analyzerId); return analyzerId; }
public void Setup() { map = new BidirectionalMap <int, string>(); }
private ImmutableArray<DiagnosticAnalyzer> GetAnalyzers(BidirectionalMap<string, DiagnosticAnalyzer> analyzerMap, IEnumerable<string> analyzerIds) { // TODO: this probably need to be cached as well in analyzer service? var builder = ImmutableArray.CreateBuilder<DiagnosticAnalyzer>(); foreach (var analyzerId in analyzerIds) { DiagnosticAnalyzer analyzer; if (analyzerMap.TryGetValue(analyzerId, out analyzer)) { builder.Add(analyzer); } } return builder.ToImmutable(); }