Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Shvedov controlflow

View GitHub Profile
View foo.cs
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
// the task is to Analyze() files in parallel and Process() them on the main thread in batches
var files = Enumerable.Range(0, 1000).Select(x => $"File{x:0000}.cs").ToList();
var degreeOfParallelism = Math.Min(Environment.ProcessorCount, 8);
View process.cs
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
// the task is to Analyze() files in parallel and Process() them on the main thread in batches
var files = Enumerable.Range(0, 1000).Select(x => $"File{x:0000}.cs").ToList();
var degreeOfParallelism = Math.Min(Environment.ProcessorCount, 8);
@controlflow
controlflow / main.cs
Created Jul 14, 2021
Null check benchmarks
View main.cs
using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
// ReSharper disable RedundantToStringCall
// ReSharper disable NotAccessedField.Local
#pragma warning disable 169
BenchmarkRunner.Run<NullChecksBenchmark>();
View foo1.cs
case IProperty {IsAuto: false, IsStatic: false} autoProperty
when !autoProperty.CanBeOverridden()
&& (autoProperty.Setter == null || autoProperty.Setter.IsInitOnly):
View foo1.cs
private static bool IsPossibleCodeBehind(IFile file)
{
return file is IFileImpl {SecondaryRangeTranslator: { } translator} && !(translator is IInjectedRangeTranslator);
}
View foo1.cs
if (pattern is IPatternWithDesignation withDesignation)
{
var existingDesignation = withDesignation.Designation;
if (existingDesignation != null && !(existingDesignation is IDiscardDesignation)) return false;
}
View foo1.cs
if (declaredElement is ITypeElement && !(declaredElement is IDelegate))
return false;
if (declaredElement is IFunction && !(declaredElement is IMethod) || declaredElement is ILabel || declaredElement is INamespace)
return false;
View foo1.cs
var parameter = parameterInstance.Element;
if (parameter.Kind != ParameterKind.VALUE
&& parameter.Kind != ParameterKind.REFERENCE) return;
View foo1.cs
if (arguments[currentIndex].MatchingParameter?.Element.IndexOf() is {} index && index >= 0)
// vs.
if (arguments[currentIndex].MatchingParameter?.Element.IndexOf() is >= 0 and var index)
View foo1.cs
case IPredefinedTypeUsage predefinedTypeUsage:
return predefinedTypeUsage.Parent is IEnumBase
? null
: InspectPredefinedTypeUsage(predefinedTypeUsage, predefinedTypeUsage.ScalarPredefinedTypeName, boundSettingsStore);