Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bringing commits over from internal/release/8.0 #108716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ede7c7b
Merge commit 'b764692d18e66561c7cff2101d642b0f789dae0c'
Jul 26, 2024
9e3c4a9
Merge commit 'd72b1cbd9f476aa9f43539223198794d58c63cff'
Aug 6, 2024
0a68963
Merge commit '79c619d6e809bc2648d6c2bbf37dba3a899ab80a'
Aug 7, 2024
05e0f2d
Merge commit '62f69f1e86f062c3b86f61c2fef3070781e4ff4f'
Aug 8, 2024
41657dc
Merge commit 'b379c7bb20415a30dc2684795828580543809f22'
Aug 13, 2024
778f78e
Merge commit 'e4ec9428d3262060fb5928b61632b35c144848ed'
Aug 13, 2024
25a72c3
Merge commit 'ed13b35174ac5b282adf0aaade335276a762159b'
Aug 13, 2024
3c8202d
Merge commit '3bcfd7e445eb3cc2652de4bf6a63a07f98cb9420'
Aug 15, 2024
b236305
Merge commit 'd40f32d2dc635e4b4c9d27dbe2673e185e6aa7ea'
Aug 19, 2024
be46d16
Fix performance issue when deserializing large payloads in JsonObject…
Aug 29, 2024
64d1397
Merge commit 'aa17c1b076d8a6c02243cb19514d8e8b2a61d8e1'
Aug 29, 2024
4426046
Merge commit 'ca415e95171f3fa58bd142c40f9a14fa30308671'
Aug 31, 2024
c2891d3
ZipArchive: Improve performance of removing extra fields
ericstj Sep 9, 2024
132fb31
Packaging: Port fix for GetParts from .NETFramework
ericstj Sep 9, 2024
7dc1560
Merged PR 41316: Use Marvin32 instead of xxHash32 in COSE hash codes
bartonjs Sep 9, 2024
378b27e
Microsoft.Extensions.Caching.Memory: use Marvin for keys on down-leve…
Sep 10, 2024
a8108c9
Merge commit 'cb77171f85a1431e67525922a53621d7475c5ebd'
Sep 10, 2024
81cabf2
Merge commit 'b5f53494d6541a1ef9b7d0e13214d5e5e5cec594'
Sep 16, 2024
8e2404d
Merge commit '81cabf2857a01351e5ab578947c7403a5b128ad1' into internal…
vseanreesermsft Oct 8, 2024
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
49 changes: 23 additions & 26 deletions src/libraries/Common/src/System/HashCodeRandomization.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace System
{
// Contains helpers for calculating randomized hash codes of common types.
Expand All @@ -14,39 +17,33 @@ namespace System
// rather than a global seed for the entire AppDomain.
internal static class HashCodeRandomization
{
#if !NET
private static readonly ulong s_seed = GenerateSeed();

private static ulong GenerateSeed()
{
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
byte[] rand = new byte[sizeof(ulong)];
rng.GetBytes(rand);

return BitConverter.ToUInt64(rand, 0);
}
}
#endif

public static int GetRandomizedOrdinalHashCode(this string value)
{
#if NETCOREAPP
// In .NET Core, string hash codes are already randomized.

return value.GetHashCode();
#else
// Downlevel, we need to perform randomization ourselves. There's still
// the potential for limited collisions ("Hello!" and "Hello!\0"), but
// this shouldn't be a problem in practice. If we need to address it,
// we can mix the string length into the accumulator before running the
// string contents through.
//
// We'll pull out pairs of chars and write 32 bits at a time.

HashCode hashCode = default;
int pair = 0;
for (int i = 0; i < value.Length; i++)
{
int ch = value[i];
if ((i & 1) == 0)
{
pair = ch << 16; // first member of pair
}
else
{
pair |= ch; // second member of pair
hashCode.Add(pair); // write pair as single unit
pair = 0;
}
}
hashCode.Add(pair); // flush any leftover data (could be 0 or 1 chars)
return hashCode.ToHashCode();
// Downlevel, we need to perform randomization ourselves.

ReadOnlySpan<char> charSpan = value.AsSpan();
ReadOnlySpan<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
return Marvin.ComputeHash32(byteSpan, s_seed);
#endif
}

Expand Down
107 changes: 89 additions & 18 deletions src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -126,7 +128,7 @@ internal void SetEntry(CacheEntry entry)
entry.LastAccessed = utcNow;

CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
if (coherentState._entries.TryGetValue(entry.Key, out CacheEntry? priorEntry))
if (coherentState.TryGetValue(entry.Key, out CacheEntry? priorEntry))
{
priorEntry.SetExpired(EvictionReason.Replaced);
}
Expand All @@ -145,12 +147,12 @@ internal void SetEntry(CacheEntry entry)
if (priorEntry == null)
{
// Try to add the new entry if no previous entries exist.
entryAdded = coherentState._entries.TryAdd(entry.Key, entry);
entryAdded = coherentState.TryAdd(entry.Key, entry);
}
else
{
// Try to update with the new entry if a previous entries exist.
entryAdded = coherentState._entries.TryUpdate(entry.Key, entry, priorEntry);
entryAdded = coherentState.TryUpdate(entry.Key, entry, priorEntry);

if (entryAdded)
{
Expand All @@ -165,7 +167,7 @@ internal void SetEntry(CacheEntry entry)
// The update will fail if the previous entry was removed after retrieval.
// Adding the new entry will succeed only if no entry has been added since.
// This guarantees removing an old entry does not prevent adding a new entry.
entryAdded = coherentState._entries.TryAdd(entry.Key, entry);
entryAdded = coherentState.TryAdd(entry.Key, entry);
}
}

Expand Down Expand Up @@ -210,7 +212,7 @@ public bool TryGetValue(object key, out object? result)
DateTime utcNow = UtcNow;

CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
if (coherentState._entries.TryGetValue(key, out CacheEntry? tmp))
if (coherentState.TryGetValue(key, out CacheEntry? tmp))
{
CacheEntry entry = tmp;
// Check if expired due to expiration tokens, timers, etc. and if so, remove it.
Expand Down Expand Up @@ -269,7 +271,8 @@ public void Remove(object key)
CheckDisposed();

CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
if (coherentState._entries.TryRemove(key, out CacheEntry? entry))

if (coherentState.TryRemove(key, out CacheEntry? entry))
{
if (_options.HasSizeLimit)
{
Expand All @@ -291,10 +294,10 @@ public void Clear()
CheckDisposed();

CoherentState oldState = Interlocked.Exchange(ref _coherentState, new CoherentState());
foreach (KeyValuePair<object, CacheEntry> entry in oldState._entries)
foreach (CacheEntry entry in oldState.GetAllValues())
{
entry.Value.SetExpired(EvictionReason.Removed);
entry.Value.InvokeEvictionCallbacks();
entry.SetExpired(EvictionReason.Removed);
entry.InvokeEvictionCallbacks();
}
}

Expand Down Expand Up @@ -415,10 +418,9 @@ private void ScanForExpiredItems()
DateTime utcNow = _lastExpirationScan = UtcNow;

CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
foreach (KeyValuePair<object, CacheEntry> item in coherentState._entries)
{
CacheEntry entry = item.Value;

foreach (CacheEntry entry in coherentState.GetAllValues())
{
if (entry.CheckExpired(utcNow))
{
coherentState.RemoveEntry(entry, _options);
Expand Down Expand Up @@ -516,9 +518,8 @@ private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntry

// Sort items by expired & priority status
DateTime utcNow = UtcNow;
foreach (KeyValuePair<object, CacheEntry> item in coherentState._entries)
foreach (CacheEntry entry in coherentState.GetAllValues())
{
CacheEntry entry = item.Value;
if (entry.CheckExpired(utcNow))
{
entriesToRemove.Add(entry);
Expand Down Expand Up @@ -645,18 +646,59 @@ private static void ValidateCacheKey(object key)
/// </summary>
private sealed class CoherentState
{
internal ConcurrentDictionary<object, CacheEntry> _entries = new ConcurrentDictionary<object, CacheEntry>();
private readonly ConcurrentDictionary<string, CacheEntry> _stringEntries = new ConcurrentDictionary<string, CacheEntry>(StringKeyComparer.Instance);
private readonly ConcurrentDictionary<object, CacheEntry> _nonStringEntries = new ConcurrentDictionary<object, CacheEntry>();
internal long _cacheSize;

private ICollection<KeyValuePair<object, CacheEntry>> EntriesCollection => _entries;
internal bool TryGetValue(object key, [NotNullWhen(true)] out CacheEntry? entry)
=> key is string s ? _stringEntries.TryGetValue(s, out entry) : _nonStringEntries.TryGetValue(key, out entry);

internal bool TryRemove(object key, [NotNullWhen(true)] out CacheEntry? entry)
=> key is string s ? _stringEntries.TryRemove(s, out entry) : _nonStringEntries.TryRemove(key, out entry);

internal bool TryAdd(object key, CacheEntry entry)
=> key is string s ? _stringEntries.TryAdd(s, entry) : _nonStringEntries.TryAdd(key, entry);

internal bool TryUpdate(object key, CacheEntry entry, CacheEntry comparison)
=> key is string s ? _stringEntries.TryUpdate(s, entry, comparison) : _nonStringEntries.TryUpdate(key, entry, comparison);

public IEnumerable<CacheEntry> GetAllValues()
{
// note this mimics the outgoing code in that we don't just access
// .Values, which has additional overheads; this is only used for rare
// calls - compaction, clear, etc - so the additional overhead of a
// generated enumerator is not alarming
foreach (KeyValuePair<string, CacheEntry> entry in _stringEntries)
{
yield return entry.Value;
}
foreach (KeyValuePair<object, CacheEntry> entry in _nonStringEntries)
{
yield return entry.Value;
}
}

private ICollection<KeyValuePair<string, CacheEntry>> StringEntriesCollection => _stringEntries;
private ICollection<KeyValuePair<object, CacheEntry>> NonStringEntriesCollection => _nonStringEntries;

internal int Count => _entries.Count;
internal int Count => _stringEntries.Count + _nonStringEntries.Count;

internal long Size => Volatile.Read(ref _cacheSize);

internal void RemoveEntry(CacheEntry entry, MemoryCacheOptions options)
{
if (EntriesCollection.Remove(new KeyValuePair<object, CacheEntry>(entry.Key, entry)))
if (entry.Key is string s)
{
if (StringEntriesCollection.Remove(new KeyValuePair<string, CacheEntry>(s, entry)))
{
if (options.SizeLimit.HasValue)
{
Interlocked.Add(ref _cacheSize, -entry.Size);
}
entry.InvokeEvictionCallbacks();
}
}
else if (NonStringEntriesCollection.Remove(new KeyValuePair<object, CacheEntry>(entry.Key, entry)))
{
if (options.SizeLimit.HasValue)
{
Expand All @@ -665,6 +707,35 @@ internal void RemoveEntry(CacheEntry entry, MemoryCacheOptions options)
entry.InvokeEvictionCallbacks();
}
}

#if NETCOREAPP
// on .NET Core, the inbuilt comparer has Marvin built in; no need to intercept
private static class StringKeyComparer
{
internal static IEqualityComparer<string> Instance => EqualityComparer<string>.Default;
}
#else
// otherwise, we need a custom comparer that manually implements Marvin
private sealed class StringKeyComparer : IEqualityComparer<string>, IEqualityComparer
{
private StringKeyComparer() { }

internal static readonly IEqualityComparer<string> Instance = new StringKeyComparer();

// special-case string keys and use Marvin hashing
public int GetHashCode(string? s) => s is null ? 0
: Marvin.ComputeHash32(MemoryMarshal.AsBytes(s.AsSpan()), Marvin.DefaultSeed);

public bool Equals(string? x, string? y)
=> string.Equals(x, y);

bool IEqualityComparer.Equals(object x, object y)
=> object.Equals(x, y);

int IEqualityComparer.GetHashCode(object obj)
=> obj is string s ? GetHashCode(s) : 0;
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>In-memory cache implementation of Microsoft.Extensions.Caching.Memory.IMemoryCache.</PackageDescription>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,4 +26,8 @@
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="$(CoreLibSharedDir)System\Marvin.cs" Link="Common\System\Marvin.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,21 @@ public async Task GetOrCreateAsyncFromCacheWithNullKeyThrows()
await Assert.ThrowsAsync<ArgumentNullException>(async () => await cache.GetOrCreateAsync<object>(null, null));
}

[Fact]
public void MixedKeysUsage()
{
// keys are split internally into 2 separate chunks
var cache = CreateCache();
var typed = Assert.IsType<MemoryCache>(cache);
object key0 = 123.45M, key1 = "123.45";
cache.Set(key0, "string value");
cache.Set(key1, "decimal value");

Assert.Equal(2, typed.Count);
Assert.Equal("string value", cache.Get(key0));
Assert.Equal("decimal value", cache.Get(key1));
}

private class TestKey
{
public override bool Equals(object obj) => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,12 @@ public static Zip64ExtraField GetAndRemoveZip64Block(List<ZipGenericExtraField>
zip64Field._localHeaderOffset = null;
zip64Field._startDiskNumber = null;

List<ZipGenericExtraField> markedForDelete = new List<ZipGenericExtraField>();
bool zip64FieldFound = false;

foreach (ZipGenericExtraField ef in extraFields)
extraFields.RemoveAll(ef =>
{
if (ef.Tag == TagConstant)
{
markedForDelete.Add(ef);
if (!zip64FieldFound)
{
if (TryGetZip64BlockFromGenericExtraField(ef, readUncompressedSize, readCompressedSize,
Expand All @@ -285,24 +283,18 @@ public static Zip64ExtraField GetAndRemoveZip64Block(List<ZipGenericExtraField>
zip64FieldFound = true;
}
}
return true;
}
}

foreach (ZipGenericExtraField ef in markedForDelete)
extraFields.Remove(ef);
return false;
});

return zip64Field;
}

public static void RemoveZip64Blocks(List<ZipGenericExtraField> extraFields)
{
List<ZipGenericExtraField> markedForDelete = new List<ZipGenericExtraField>();
foreach (ZipGenericExtraField field in extraFields)
if (field.Tag == TagConstant)
markedForDelete.Add(field);

foreach (ZipGenericExtraField field in markedForDelete)
extraFields.Remove(field);
extraFields.RemoveAll(field => field.Tag == TagConstant);
}

public void WriteBlock(Stream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>Provides classes that support storage of multiple data objects in a single container.</PackageDescription>
</PropertyGroup>

Expand Down
Loading
Loading