Skip to content

Instantly share code, notes, and snippets.

View controlflow's full-sized avatar

Alexander Shvedov controlflow

View GitHub Profile
using System;
using System.Threading;
using JetBrains.Annotations;
namespace JetBrains.Util.Concurrency.Threading;
/// <summary>
/// Reader-writer lock for reader-heavy scenarios.
/// </summary>
public sealed class StripedReaderWriterLock
#nullable enable
using System;
using JetBrains.Application.Progress;
using JetBrains.DocumentManagers.Transactions;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Feature.Services.Daemon;
using JetBrains.ReSharper.Feature.Services.Intentions.Scoped.Scopes;
using JetBrains.TextControl;
@controlflow
controlflow / Denormals.cs
Created October 5, 2022 20:09
Denormal floating numbers playground
using System.Text;
PrintNumber(0f);
PrintNumber(-0f);
PrintNumber(1f);
PrintNumber(-1f);
PrintNumber(float.PositiveInfinity);
PrintNumber(float.NegativeInfinity);
PrintNumber(float.NaN);
PrintNumber(float.Epsilon);
@controlflow
controlflow / foo.cs
Last active October 5, 2022 20:10
Parallel processing pattern
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);
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 July 14, 2021 19:19
Null check benchmarks
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>();
case IProperty {IsAuto: false, IsStatic: false} autoProperty
when !autoProperty.CanBeOverridden()
&& (autoProperty.Setter == null || autoProperty.Setter.IsInitOnly):
private static bool IsPossibleCodeBehind(IFile file)
{
return file is IFileImpl {SecondaryRangeTranslator: { } translator} && !(translator is IInjectedRangeTranslator);
}
if (pattern is IPatternWithDesignation withDesignation)
{
var existingDesignation = withDesignation.Designation;
if (existingDesignation != null && !(existingDesignation is IDiscardDesignation)) return false;
}
if (declaredElement is ITypeElement && !(declaredElement is IDelegate))
return false;
if (declaredElement is IFunction && !(declaredElement is IMethod) || declaredElement is ILabel || declaredElement is INamespace)
return false;