Skip to content

Commit

Permalink
CachedRange<V> performance audit (#19)
Browse files Browse the repository at this point in the history
* Codegen fixes for CachedRange IEnumerable overload
- Sacrifice ordering by expiration in quicklist when inserting from IEnumerable to reduce loop body size
- Add benchmarks to track actual CachedRange perf
- Add PolySharp to work towards removing hand-rolled polyfills for older targets

* Fix typo

* Various minor adjustments:
- Add 'net7.0' TFM
- Use PolySharp with private assets instead of IndexRange
- Move some project attributes to build props
- Only use write barrier in quicklist when working with non-managed keys, make sure JIT constant folds it
- Some global constants were mutable, mark them as readonly
- Clean up benchmarks a little

* Fix build

* Remove net48 TFM from benchmarks
  • Loading branch information
neon-sunset committed Dec 10, 2022
1 parent 136d43a commit 806f7bc
Show file tree
Hide file tree
Showing 21 changed files with 1,412 additions and 1,512 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dotnet-releaser.yml
Expand Up @@ -22,6 +22,7 @@ jobs:
dotnet-version: |
3.1.x
6.0.x
7.0.x
- name: Build, Test, Pack, Publish
shell: bash
env:
Expand All @@ -47,6 +48,7 @@ jobs:
dotnet-version: |
3.1.x
6.0.x
7.0.x
- name: Build, Test, Pack, Publish
shell: pwsh
env:
Expand Down
18 changes: 18 additions & 0 deletions Directory.Build.props
@@ -0,0 +1,18 @@
<Project>
<PropertyGroup>
<Features>strict</Features>
<LangVersion>11</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<IsTrimmable>true</IsTrimmable>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PolySharp" Version="1.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
32 changes: 6 additions & 26 deletions src/FastCache.Benchmarks/Comparison.cs
Expand Up @@ -39,47 +39,27 @@ public void Initialize()
[Benchmark(Baseline = true)]
public string TryGetCached()
{
if (Cached<string>.TryGet(ItemKey, out var cached))
{
return cached;
}

return Unreachable<string>();
return Cached<string>.TryGet(ItemKey, out var cached) ? cached : Unreachable<string>();
}

[Benchmark]
public string TryGetMemoryCache()
{
var found = _memoryCache.TryGetValue(ItemKey, out var cacheItem);
if (found && cacheItem is string value)
{
return value;
}

return Unreachable<string>();
return _memoryCache.TryGetValue(ItemKey, out var result) && result is string value
? value
: Unreachable<string>();
}

[Benchmark]
public string TryGetCacheManager()
{
var value = _cacheManager.Get(ItemKey);
if (value is not null)
{
return value;
}

return Unreachable<string>();
return _cacheManager.Get(ItemKey) ?? Unreachable<string>();
}

[Benchmark]
public string TryGetLazyCache()
{
if (_lazyCache.TryGetValue<string>(ItemKey, out var value))
{
return value;
}

return Unreachable<string>();
return _lazyCache.TryGetValue<string>(ItemKey, out var value) ? value : Unreachable<string>();
}

[Benchmark]
Expand Down

0 comments on commit 806f7bc

Please sign in to comment.