Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
###############################
# VB Coding Conventions #
###############################
[*.vb]
# Modifier preferences
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion

# code analysis
dotnet_diagnostic.IDE0079.severity = none
dotnet_diagnostic.CA1805.severity = none
dotnet_diagnostic.CA1815.severity = none
5 changes: 1 addition & 4 deletions BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,7 @@ private void VerifyHits(int iterations, int minSamples)

// verify this doesn't block or throw
var b = cache.Scheduler as BackgroundThreadScheduler;
if (b is not null)
{
b.Dispose();
}
b?.Dispose();
}

private void LogLru()
Expand Down
2 changes: 2 additions & 0 deletions BitFaster.Caching/Atomic/AsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFactor
return await synchronizedTask.ConfigureAwait(false);
}

#pragma warning disable CA2002 // Do not lock on objects with weak identity
private Task<V> DoubleCheck(Task<V> value)
{
// Fast path
Expand All @@ -155,6 +156,7 @@ private Task<V> DoubleCheck(Task<V> value)

return valueTask;
}
#pragma warning restore CA2002 // Do not lock on objects with weak identity
}
}
}
2 changes: 2 additions & 0 deletions BitFaster.Caching/Atomic/AtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public override int GetHashCode()
return ValueIfCreated.GetHashCode();
}

#pragma warning disable CA2002 // Do not lock on objects with weak identity
private class Initializer
{
private bool isInitialized;
Expand All @@ -153,5 +154,6 @@ public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : st
}
}
}
#pragma warning restore CA2002 // Do not lock on objects with weak identity
}
}
4 changes: 4 additions & 0 deletions BitFaster.Caching/Atomic/AtomicFactoryScopedAsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public AtomicFactoryScopedAsyncCache(ICache<K, ScopedAsyncAtomicFactory<K, V>> c
///<inheritdoc/>
public ICollection<K> Keys => AtomicEx.FilterKeys<K, ScopedAsyncAtomicFactory<K, V>>(this.cache, v => v.IsScopeCreated);

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public void AddOrUpdate(K key, V value)
{
this.cache.AddOrUpdate(key, new ScopedAsyncAtomicFactory<K, V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public void Clear()
Expand Down Expand Up @@ -129,11 +131,13 @@ public bool TryRemove(K key)
return this.cache.TryRemove(key);
}

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public bool TryUpdate(K key, V value)
{
return this.cache.TryUpdate(key, new ScopedAsyncAtomicFactory<K, V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, Scoped<V>>> GetEnumerator()
Expand Down
4 changes: 4 additions & 0 deletions BitFaster.Caching/Atomic/AtomicFactoryScopedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public AtomicFactoryScopedCache(ICache<K, ScopedAtomicFactory<K, V>> cache)
///<inheritdoc/>
public ICollection<K> Keys => AtomicEx.FilterKeys<K, ScopedAtomicFactory<K, V>>(this.cache, v => v.IsScopeCreated);

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public void AddOrUpdate(K key, V value)
{
this.cache.AddOrUpdate(key, new ScopedAtomicFactory<K, V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public void Clear()
Expand Down Expand Up @@ -129,11 +131,13 @@ public bool TryRemove(K key)
return this.cache.TryRemove(key);
}

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public bool TryUpdate(K key, V value)
{
return this.cache.TryUpdate(key, new ScopedAtomicFactory<K, V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, Scoped<V>>> GetEnumerator()
Expand Down
2 changes: 2 additions & 0 deletions BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public async ValueTask<Scoped<V>> CreateScopeAsync<TFactory>(K key, TFactory val
return await synchronizedTask.ConfigureAwait(false);
}

#pragma warning disable CA2002 // Do not lock on objects with weak identity
private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)
{
// Fast path
Expand All @@ -201,6 +202,7 @@ private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)

return task;
}
#pragma warning restore CA2002 // Do not lock on objects with weak identity

// <remarks>
// Let's say there are 2 threads, A and B:
Expand Down
2 changes: 2 additions & 0 deletions BitFaster.Caching/Atomic/ScopedAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public void Dispose()
scope.Dispose();
}

#pragma warning disable CA2002 // Do not lock on objects with weak identity
private class Initializer
{
private bool isInitialized;
Expand Down Expand Up @@ -196,5 +197,6 @@ public Scoped<V> TryCreateDisposedScope()
}
}
}
#pragma warning restore CA2002 // Do not lock on objects with weak identity
}
}
7 changes: 7 additions & 0 deletions BitFaster.Caching/BitFaster.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<PropertyGroup>
<AnalysisModePerformance>All</AnalysisModePerformance>
<AnalysisModeDocumentation>All</AnalysisModeDocumentation>
<AnalysisModeInteroperability>All</AnalysisModeInteroperability>
<AnalysisModeReliability>All</AnalysisModeReliability>
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching/Lfu/CmSketchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace BitFaster.Caching.Lfu
/// https://github.com/ben-manes/caffeine
public class CmSketchCore<T, I> where I : struct, IsaProbe
{
private static readonly long ResetMask = 0x7777777777777777L;
private static readonly long OneMask = 0x1111111111111111L;
private const long ResetMask = 0x7777777777777777L;
private const long OneMask = 0x1111111111111111L;

private long[] table;
private int sampleSize;
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lfu/LfuCapacityPartition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void InitializeStepSize()
stepSize = HillClimberStepPercent;
}

private double Clamp(double input, double min, double max)
private static double Clamp(double input, double min, double max)
{
return Math.Max(min, Math.Min(input, max));
}
Expand Down
8 changes: 4 additions & 4 deletions BitFaster.Caching/Lru/ClassicLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ public async ValueTask<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
return value;
}

value = await valueFactory(key);
value = await valueFactory(key).ConfigureAwait(false);

if (TryAdd(key, value))
{
return value;
}

return await this.GetOrAddAsync(key, valueFactory);
return await this.GetOrAddAsync(key, valueFactory).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -228,14 +228,14 @@ public async ValueTask<V> GetOrAddAsync<TArg>(K key, Func<K, TArg, Task<V>> valu
return value;
}

value = await valueFactory(key, factoryArgument);
value = await valueFactory(key, factoryArgument).ConfigureAwait(false);

if (TryAdd(key, value))
{
return value;
}

return await this.GetOrAddAsync(key, valueFactory, factoryArgument);
return await this.GetOrAddAsync(key, valueFactory, factoryArgument).ConfigureAwait(false);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/ConcurrentLruCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void AddOrUpdate(K key, V value)
///<inheritdoc/>
public void Clear()
{
int count = this.Count();
int count = this.Count;

for (int i = 0; i < count; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching/Scheduler/BackgroundThreadScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class BackgroundThreadScheduler : IScheduler, IDisposable
public BackgroundThreadScheduler()
{
// dedicated thread
Task.Factory.StartNew(() => Background(), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => Background(), cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

///<inheritdoc/>
Expand Down Expand Up @@ -68,7 +68,7 @@ private async Task Background()
{
try
{
await semaphore.WaitAsync(cts.Token);
await semaphore.WaitAsync(cts.Token).ConfigureAwait(false);

BufferStatus s;
do
Expand Down
3 changes: 2 additions & 1 deletion BitFaster.Caching/Scheduler/ThreadPoolScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BitFaster.Caching.Scheduler
Expand All @@ -25,7 +26,7 @@ public void Run(Action action)
{
count++;
Task.Run(action)
.ContinueWith(t => lastException = new Optional<Exception>(t.Exception.Flatten().InnerException), TaskContinuationOptions.OnlyOnFaulted);
.ContinueWith(t => lastException = new Optional<Exception>(t.Exception.Flatten().InnerException), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
}
}
}
8 changes: 6 additions & 2 deletions BitFaster.Caching/ScopedAsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public ScopedAsyncCache(IAsyncCache<K, Scoped<V>> cache)
///<inheritdoc/>
public ICollection<K> Keys => this.cache.Keys;

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public void AddOrUpdate(K key, V value)
{
this.cache.AddOrUpdate(key, new Scoped<V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public void Clear()
Expand All @@ -67,7 +69,7 @@ public async ValueTask<Lifetime<V>> ScopedGetOrAddAsync(K key, Func<K, Task<Scop
var spinwait = new SpinWait();
while (true)
{
var scope = await cache.GetOrAddAsync(key, valueFactory);
var scope = await cache.GetOrAddAsync(key, valueFactory).ConfigureAwait(false);

if (scope.TryCreateLifetime(out var lifetime))
{
Expand Down Expand Up @@ -98,7 +100,7 @@ public async ValueTask<Lifetime<V>> ScopedGetOrAddAsync<TArg>(K key, Func<K, TAr
var spinwait = new SpinWait();
while (true)
{
var scope = await cache.GetOrAddAsync(key, valueFactory, factoryArgument);
var scope = await cache.GetOrAddAsync(key, valueFactory, factoryArgument).ConfigureAwait(false);

if (scope.TryCreateLifetime(out var lifetime))
{
Expand Down Expand Up @@ -134,11 +136,13 @@ public bool TryRemove(K key)
return this.cache.TryRemove(key);
}

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public bool TryUpdate(K key, V value)
{
return this.cache.TryUpdate(key, new Scoped<V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, Scoped<V>>> GetEnumerator()
Expand Down
4 changes: 4 additions & 0 deletions BitFaster.Caching/ScopedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public ScopedCache(ICache<K, Scoped<V>> cache)
///<inheritdoc/>
public ICollection<K> Keys => this.cache.Keys;

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public void AddOrUpdate(K key, V value)
{
this.cache.AddOrUpdate(key, new Scoped<V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public void Clear()
Expand Down Expand Up @@ -121,11 +123,13 @@ public bool TryRemove(K key)
return this.cache.TryRemove(key);
}

#pragma warning disable CA2000 // Dispose objects before losing scope
///<inheritdoc/>
public bool TryUpdate(K key, V value)
{
return this.cache.TryUpdate(key, new Scoped<V>(value));
}
#pragma warning restore CA2000 // Dispose objects before losing scope

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, Scoped<V>>> GetEnumerator()
Expand Down