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

Frozen dictionary #1125

Closed
wants to merge 7 commits into from
Closed
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
6 changes: 4 additions & 2 deletions src/Verify.Tests/CultureToDateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ public Task BuildCultureToDate()
{
return Task.CompletedTask;
}

var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
var builder = new StringBuilder(
"""
// <auto-generated />
static partial class DateScrubber
{
static Dictionary<string, CultureDate> cultureDates = new()
static IReadOnlyDictionary<string, CultureDate> cultureDates = new Dictionary<string, CultureDate>()
{

""");
Expand All @@ -31,6 +32,7 @@ static partial class DateScrubber
continue;
}
}

builder.AppendLine(
$$"""
{
Expand All @@ -44,7 +46,7 @@ static partial class DateScrubber

builder.AppendLine(
"""
};
}.ToFrozenDictionary();
}
""");
var file = Path.Combine(AttributeReader.GetSolutionDirectory(), "Verify/Serialization/Scrubbers/DateScrubber_Generated.cs");
Expand Down
4 changes: 2 additions & 2 deletions src/Verify/Compare/SharedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public static partial class VerifierSettings
{
static Dictionary<string, StringCompare> stringComparers = [];
static Dictionary<string, StreamCompare> streamComparers = [];
static IDictionary<string, StringCompare> stringComparers = new Dictionary<string, StringCompare>();
static IDictionary<string, StreamCompare> streamComparers = new Dictionary<string, StreamCompare>();
static StringCompare? defaultStringComparer;

internal static bool TryGetStreamComparer(string extension, [NotNullWhen(true)] out StreamCompare? comparer) =>
Expand Down
11 changes: 11 additions & 0 deletions src/Verify/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

public partial class Counter
{
internal static void FirstRun()
{
#if NET6_0_OR_GREATER
globalNamedDates = globalNamedDates.ToFrozenDictionary();
globalNamedTimes = globalNamedTimes.ToFrozenDictionary();
#endif
globalNamedDateTimes = globalNamedDateTimes.ToFrozenDictionary();
globalNamedDateTimeOffsets = globalNamedDateTimeOffsets.ToFrozenDictionary();
globalNamedGuids = globalNamedGuids.ToFrozenDictionary();
}

#if NET6_0_OR_GREATER
Dictionary<Date, string> namedDates;
Dictionary<Time, string> namedTimes;
Expand Down
3 changes: 1 addition & 2 deletions src/Verify/Counter_Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ namespace VerifyTests;
public partial class Counter
{
ConcurrentDictionary<Date, (int intValue, string stringValue)> dateCache = [];
static Dictionary<Date, string> globalNamedDates = [];
static IDictionary<Date, string> globalNamedDates = new Dictionary<Date, string>();
int currentDate;

internal static void AddNamed(Date value, string name)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<DateTime, (int intValue, string stringValue)> dateTimeCache = new(new DateTimeComparer());
static Dictionary<DateTime, string> globalNamedDateTimes = [];
static IDictionary<DateTime, string> globalNamedDateTimes = new Dictionary<DateTime, string>();

class DateTimeComparer : IEqualityComparer<DateTime>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<DateTimeOffset, (int intValue, string stringValue)> dateTimeOffsetCache = new(new DateTimeOffsetComparer());
static Dictionary<DateTimeOffset, string> globalNamedDateTimeOffsets = [];
static IDictionary<DateTimeOffset, string> globalNamedDateTimeOffsets = new Dictionary<DateTimeOffset, string>();

class DateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<Guid, (int intValue, string stringValue)> guidCache = [];
static Dictionary<Guid, string> globalNamedGuids = [];
static IDictionary<Guid, string> globalNamedGuids = new Dictionary<Guid, string>();
int currentGuid;

public int Next(Guid input) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace VerifyTests;
public partial class Counter
{
ConcurrentDictionary<Time, (int intValue, string stringValue)> timeCache = [];
static Dictionary<Time, string> globalNamedTimes = [];
static IDictionary<Time, string> globalNamedTimes = new Dictionary<Time, string>();
int currentTime;

internal static void AddNamed(Time time, string name)
Expand Down
1 change: 1 addition & 0 deletions src/Verify/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
global using System.Runtime.InteropServices;
global using CharSpan = System.ReadOnlySpan<char>;
global using Culture = System.Globalization.CultureInfo;
global using System.Collections.Frozen;
2 changes: 1 addition & 1 deletion src/Verify/Naming/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class VerifierSettings
{
internal static Namer SharedNamer = new();

static Dictionary<Type, Func<object, string>> parameterToNameLookup = new()
static IDictionary<Type, Func<object, string>> parameterToNameLookup = new Dictionary<Type, Func<object, string>>
{
{
typeof(bool), _ => ((bool) _).ToString(Culture.InvariantCulture)
Expand Down
5 changes: 3 additions & 2 deletions src/Verify/Serialization/Scrubbers/DateScrubber_Generated.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <auto-generated />
static partial class DateScrubber
{
static Dictionary<string, CultureDate> cultureDates = new()
static IReadOnlyDictionary<string, CultureDate> cultureDates = new Dictionary<string, CultureDate>()
{
{
"",
Expand Down Expand Up @@ -1653,5 +1653,6 @@ static partial class DateScrubber
new(2023, 2, 22, 12, 10, 10, 10),
new(2023, 5, 7, 1, 0, 0))
},
};
}
.ToFrozenDictionary() ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
internal static Dictionary<string, List<Action<StringBuilder, Counter>>> ExtensionMappedGlobalScrubbers = [];
internal static IDictionary<string, List<Action<StringBuilder, Counter>>> ExtensionMappedGlobalScrubbers = new Dictionary<string, List<Action<StringBuilder, Counter>>>();

/// <summary>
/// Modify the resulting test content using custom code.
Expand Down
4 changes: 2 additions & 2 deletions src/Verify/Serialization/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static bool TryGetToString(
return typeToString.TryGetValue(target.GetType(), out toString);
}

static Dictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>> typeToString = new()
static IDictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>> typeToString = new Dictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>>
{
#region typeToStringMapping

Expand Down Expand Up @@ -135,7 +135,7 @@ public static void TreatAsString<T>(AsString<T>? toString = null)

internal static void Reset()
{
InnerVerifier.verifyHasBeenRun = false;
InnerVerifier.verifyHasBeenRun = 0;
DateCountingEnabled = true;
StrictJson = false;
scrubProjectDir = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
static Dictionary<Type, Dictionary<string, ConvertTargetMember>> membersConverters = [];
static IDictionary<Type, Dictionary<string, ConvertTargetMember>> membersConverters = new Dictionary<Type, Dictionary<string, ConvertTargetMember>>();

internal static ConvertTargetMember? GetMemberConverter(MemberInfo member) =>
GetMemberConverter(member.DeclaringType, member.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Splitters/Settings_Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
static Dictionary<string, AsyncConversion<Stream>> extensionConverters = [];
static IDictionary<string, AsyncConversion<Stream>> extensionConverters = new Dictionary<string, AsyncConversion<Stream>>();

internal static bool TryGetExtensionConverter(string extension, [NotNullWhen(true)] out AsyncConversion<Stream>? converter) =>
extensionConverters.TryGetValue(extension, out converter);
Expand Down
15 changes: 12 additions & 3 deletions src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public partial class InnerVerifier :
GetIndexedFileNames getIndexedFileNames = null!;
IEnumerable<string> verifiedFiles = null!;
Counter counter;
internal static bool verifyHasBeenRun;
internal static int verifyHasBeenRun;

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowIfVerifyHasBeenRun()
{
if (!verifyHasBeenRun)
if (verifyHasBeenRun == 0)
{
return;
}
Expand All @@ -38,7 +38,7 @@ public InnerVerifier(
Guard.AgainstEmpty(sourceFile);
Guard.AgainstEmpty(typeName);
Guard.AgainstEmpty(methodName);
verifyHasBeenRun = true;
FirstRun();
VerifierSettings.RunBeforeCallbacks();
this.settings = settings;

Expand All @@ -63,6 +63,15 @@ public InnerVerifier(
}
}

static void FirstRun()
{
if (Interlocked.Exchange(ref verifyHasBeenRun, 1) == 0)
{
VerifierSettings.FirstRun();
Counter.FirstRun();
}
}

/// <summary>
/// Initialize a new instance of the <see cref="InnerVerifier" /> class for verifying the entire file (not just a specific type)
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Verify/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ public static partial class VerifierSettings
public static void OmitContentFromException() =>
omitContentFromException = true;

internal static void FirstRun()
{
stringComparers = stringComparers.ToFrozenDictionary();
streamComparers = streamComparers.ToFrozenDictionary();
parameterToNameLookup = parameterToNameLookup.ToFrozenDictionary();
ExtensionMappedGlobalScrubbers = ExtensionMappedGlobalScrubbers.ToFrozenDictionary();
typeToString = typeToString.ToFrozenDictionary();
membersConverters = membersConverters.ToFrozenDictionary();
extensionConverters = extensionConverters.ToFrozenDictionary();
}

/// <summary>
/// Automatically accept the results of all tests.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Verify/Verify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<DefineConstants>$(DefineConstants);DiffEngine</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" Condition="'$(TargetFramework)' != 'net8.0'" />
<PackageReference Include="Polyfill" Version="2.2.0" PrivateAssets="all" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" Condition="$(TargetFrameworkIdentifier) == '.NETFramework'" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" Condition="$(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework) == 'net6.0' or $(TargetFramework) == 'net7.0' or $(TargetFramework) == 'net8.0'" />
Expand Down
Loading