Skip to content

Commit

Permalink
perf: replace some static getters to readonly fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Oct 21, 2023
1 parent c2580e5 commit 8ee74d3
Show file tree
Hide file tree
Showing 41 changed files with 113 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/Stl.Fusion.Blazor/BlazorModeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stl.Fusion.Blazor;

public class BlazorModeHelper(NavigationManager navigator)
{
public static bool IsBlazorServer { get; } = OSInfo.Kind != OSKind.WebAssembly;
public static readonly bool IsBlazorServer = OSInfo.Kind != OSKind.WebAssembly;

protected NavigationManager Navigator { get; } = navigator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DefaultParameterComparer : ParameterComparer
.GetInterfaces()
.Single(x => Equals(x.Name, "IEventCallback"));

public static ParameterComparer Instance { get; } = new DefaultParameterComparer();
public static ParameterComparer Instance { get; set; } = new DefaultParameterComparer();

// Mostly copied from Microsoft.AspNetCore.Components.ChangeDetection
public override bool AreEqual(object? oldValue, object? newValue)
Expand Down
12 changes: 6 additions & 6 deletions src/Stl.Fusion.EntityFramework/DbHints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ public abstract record DbHint(Symbol Value);

public record DbLockingHint(Symbol Value) : DbHint(Value)
{
public static DbLockingHint KeyShare { get; } = new(nameof(KeyShare));
public static DbLockingHint Share { get; } = new(nameof(Share));
public static DbLockingHint NoKeyUpdate { get; } = new(nameof(NoKeyUpdate));
public static DbLockingHint Update { get; } = new(nameof(Update));
public static readonly DbLockingHint KeyShare = new(nameof(KeyShare));
public static readonly DbLockingHint Share = new(nameof(Share));
public static readonly DbLockingHint NoKeyUpdate = new(nameof(NoKeyUpdate));
public static readonly DbLockingHint Update = new(nameof(Update));
}

public record DbWaitHint(Symbol Value) : DbHint(Value)
{
public static DbLockingHint NoWait { get; } = new(nameof(NoWait));
public static DbLockingHint SkipLocked { get; } = new(nameof(SkipLocked));
public static readonly DbLockingHint NoWait = new(nameof(NoWait));
public static readonly DbLockingHint SkipLocked = new(nameof(SkipLocked));
}

public record DbCustomHint(Symbol Value) : DbHint(Value);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Stl.Fusion.Authentication;
private static readonly ListFormat IdFormat = ListFormat.SlashSeparated;

public static readonly UserIdentity None;
public static string DefaultSchema { get; } = "Default";
public static string DefaultSchema { get; set; } = "Default";

[DataMember(Order = 0), MemoryPackOrder(0)]
public Symbol Id { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Fusion.Ext.Services/Extensions/KeyValueStoreExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Stl.Fusion.Extensions;

public static class KeyValueStoreExt
{
public static ListFormat ListFormat { get; } = ListFormat.SlashSeparated;
public static ListFormat ListFormat { get; set; } = ListFormat.SlashSeparated;
public static char Delimiter => ListFormat.Delimiter;

// Set
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Fusion/Session/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed partial class Session : IHasId<Symbol>, IRequirementTarget,
IEquatable<Session>, IConvertibleTo<string>, IConvertibleTo<Symbol>,
IHasJsonCompatibleToString
{
public static Session Default { get; } = new("~");
public static readonly Session Default = new("~");
public static SessionFactory Factory { get; set; } = DefaultSessionFactory.New();
public static SessionValidator Validator { get; set; } = session => !session.IsDefault();

Expand Down
4 changes: 2 additions & 2 deletions src/Stl.Interception/ArgumentList-Generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ArrangeConstructorOrDestructorBody
using Cysharp.Text;
Expand All @@ -10,7 +10,7 @@ namespace Stl.Interception;

public abstract partial record ArgumentList
{
public static ImmutableArray<Type> Types { get; } = ImmutableArray.Create(new [] {
public static readonly ImmutableArray<Type> Types = ImmutableArray.Create(new [] {
typeof(ArgumentList0),
typeof(ArgumentList<>),
typeof(ArgumentList<, >),
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Interception/ArgumentList-Generated.tt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Stl.Interception;

public abstract partial record ArgumentList
{
public static ImmutableArray<Type> Types { get; } = ImmutableArray.Create(new [] {
public static readonly ImmutableArray<Type> Types = ImmutableArray.Create(new [] {
typeof(ArgumentList0),
<# for (var itemCount = 1; itemCount <= maxItemCount; itemCount++) {
#>
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Interception/ArgumentList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract partial record ArgumentList
{
protected static readonly ConcurrentDictionary<(Type, MethodInfo), Func<object, ArgumentList, object?>> InvokerCache = new();

public static ArgumentList Empty { get; } = new ArgumentList0();
public static readonly ArgumentList Empty = new ArgumentList0();

[JsonIgnore, Newtonsoft.Json.JsonIgnore, IgnoreDataMember, MemoryPackIgnore]
public abstract int Length { get; }
Expand Down
9 changes: 3 additions & 6 deletions src/Stl.Interception/TypeViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ TypeViewFactory<TView> For<TView>()
where TView : class;
}

public class TypeViewFactory : ITypeViewFactory
public class TypeViewFactory(TypeViewInterceptor interceptor) : ITypeViewFactory
{
public static ITypeViewFactory Default { get; } =
public static ITypeViewFactory Default { get; set; } =
new TypeViewFactory(new TypeViewInterceptor(DependencyInjection.ServiceProviderExt.Empty));

protected Interceptor Interceptor { get; }

public TypeViewFactory(TypeViewInterceptor interceptor)
=> Interceptor = interceptor;
protected Interceptor Interceptor { get; } = interceptor;

public object CreateView(object implementation, Type viewType)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Stl.Plugins/Metadata/PluginSetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Stl.Plugins.Metadata;

public class PluginSetInfo
{
public static PluginSetInfo Empty { get; } = new(
public static readonly PluginSetInfo Empty = new(
ImmutableDictionary<TypeRef, PluginInfo>.Empty,
ImmutableDictionary<TypeRef, ImmutableHashSet<TypeRef>>.Empty,
ImmutableDictionary<TypeRef, ImmutableArray<TypeRef>>.Empty);
Expand Down Expand Up @@ -81,7 +81,7 @@ public override string ToString()

// Private methods

// This method is used instead of .ToHashSet to eliminate #if NETFRAMEWORK
// This method is used instead of .ToHashSet to eliminate #if NETFRAMEWORK
private static HashSet<TSource> ToHashSet<TSource>(IEnumerable<TSource> source)
=> new(source);

Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Plugins/PluginInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IPluginInfoProvider
/// </summary>
public class Query
{
public static Query Instance { get; } = new();
public static readonly Query Instance = new();
}

ImmutableHashSet<TypeRef> GetDependencies(Type pluginType);
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Rpc/WebSockets/WebSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class WebSocketChannel<T> : Channel<T>
{
public record Options
{
public static Options Default { get; } = new();
public static readonly Options Default = new();

public bool OwnsWebSocket { get; init; } = true;
public int WriteFrameSize { get; init; } = 4400;
Expand Down
10 changes: 5 additions & 5 deletions src/Stl/Async/ValueTaskExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Stl.Async;

public static class ValueTaskExt
{
public static ValueTask NeverEndingTask { get; } = TaskExt.NeverEndingTask.ToValueTask();
public static ValueTask<Unit> NeverEndingUnitTask { get; } = TaskExt.NeverEndingUnitTask.ToValueTask();
public static ValueTask CompletedTask { get; } = Task.CompletedTask.ToValueTask();
public static ValueTask<bool> TrueTask { get; } = FromResult(true);
public static ValueTask<bool> FalseTask { get; } = FromResult(false);
public static readonly ValueTask NeverEndingTask = TaskExt.NeverEndingTask.ToValueTask();
public static readonly ValueTask<Unit> NeverEndingUnitTask = TaskExt.NeverEndingUnitTask.ToValueTask();
public static readonly ValueTask CompletedTask = Task.CompletedTask.ToValueTask();
public static readonly ValueTask<bool> TrueTask = FromResult(true);
public static readonly ValueTask<bool> FalseTask = FromResult(false);

public static ValueTask<T> FromResult<T>(T value) => new(value);
public static ValueTask<T> FromException<T>(Exception error) => new(Task.FromException<T>(error));
Expand Down
3 changes: 2 additions & 1 deletion src/Stl/Collections/Slim/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace Stl.Collections.Slim;
public sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
public static ReferenceEqualityComparer<T> Instance { get; } = new();
public static readonly ReferenceEqualityComparer<T> Instance = new();

public bool Equals(T? x, T? y) => ReferenceEquals(x, y);
public int GetHashCode(T obj) => RuntimeHelpers.GetHashCode(obj);
}
2 changes: 1 addition & 1 deletion src/Stl/Concurrency/GCHandlePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public sealed class GCHandlePool(GCHandlePool.Options settings) : IDisposable
{
public record Options
{
public static Options Default { get; } = new();
public static readonly Options Default = new();

public int Capacity { get; init; } = 1024;
public GCHandleType HandleType { get; init; } = GCHandleType.Weak;
Expand Down
2 changes: 1 addition & 1 deletion src/Stl/Conversion/Internal/CastToBaseConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Stl.Conversion.Internal;
public class CastToBaseConverter<TSource, TTarget> : Converter<TSource, TTarget>
where TSource : TTarget
{
public static CastToBaseConverter<TSource, TTarget> Instance { get; } = new();
public static readonly CastToBaseConverter<TSource, TTarget> Instance = new();

public override TTarget Convert(TSource source)
=> source;
Expand Down
2 changes: 1 addition & 1 deletion src/Stl/Conversion/Internal/CastToDescendantConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Stl.Conversion.Internal;
public class CastToDescendantConverter<TSource, TTarget> : Converter<TSource, TTarget>
where TTarget : TSource
{
public static CastToDescendantConverter<TSource, TTarget> Instance { get; } = new();
public static readonly CastToDescendantConverter<TSource, TTarget> Instance = new();

public override TTarget Convert(TSource source)
=> (TTarget) source!;
Expand Down
12 changes: 6 additions & 6 deletions src/Stl/Conversion/Internal/DefaultSourceConverterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ protected virtual Converter<TSource, TTarget> GetConverter<TTarget>()

// 0. Can we simply cast to base / descendant?
if (tTarget.IsAssignableFrom(tSource)) {
var pInstance =
var fInstance =
typeof(CastToBaseConverter<,>)
.MakeGenericType(tSource, tTarget)
.GetProperty(
.GetField(
nameof(CastToBaseConverter<TTarget, TTarget>.Instance),
BindingFlags.Static | BindingFlags.Public);
return (Converter<TSource, TTarget>) pInstance!.GetValue(null)!;
return (Converter<TSource, TTarget>) fInstance!.GetValue(null)!;
}
if (tSource.IsAssignableFrom(tTarget)) {
var pInstance =
var fInstance =
typeof(CastToDescendantConverter<,>)
.MakeGenericType(tSource, tTarget)
.GetProperty(
.GetField(
nameof(CastToDescendantConverter<TTarget, TTarget>.Instance),
BindingFlags.Static | BindingFlags.Public);
return (Converter<TSource, TTarget>) pInstance!.GetValue(null)!;
return (Converter<TSource, TTarget>) fInstance!.GetValue(null)!;
}

// 1. Is there IConverter<,> service?
Expand Down
2 changes: 1 addition & 1 deletion src/Stl/DependencyInjection/ServiceProviderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Stl.DependencyInjection;

public static class ServiceProviderExt
{
public static IServiceProvider Empty { get; } = new ServiceCollection().BuildServiceProvider();
public static readonly IServiceProvider Empty = new ServiceCollection().BuildServiceProvider();

// Logging extensions

Expand Down
2 changes: 1 addition & 1 deletion src/Stl/Diagnostics/ActivitySourceExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stl.Diagnostics;

public static class ActivitySourceExt
{
public static ActivitySource Unknown { get; } = new("<Unknown>");
public static readonly ActivitySource Unknown = new("<Unknown>");

public static Activity? StartActivity(
this ActivitySource activitySource,
Expand Down
2 changes: 1 addition & 1 deletion src/Stl/Diagnostics/MeterExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Stl.Diagnostics;

public static class MeterExt
{
public static Meter Unknown { get; } = new("<Unknown>");
public static readonly Meter Unknown = new("<Unknown>");
}
4 changes: 2 additions & 2 deletions src/Stl/Diagnostics/Samplers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public sealed record Sampler(
Func<Sampler> Duplicate)
{
#pragma warning disable CS8603
public static Sampler Always { get; } =
public static readonly Sampler Always =
new(nameof(Always), 1, static () => true, () => Always);
public static Sampler Never { get; } =
public static readonly Sampler Never =
new(nameof(Never), 0, static () => false, () => Never);
#pragma warning restore CS8603

Expand Down
4 changes: 2 additions & 2 deletions src/Stl/IO/FileExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Stl.IO;

public static class FileExt
{
public static Encoding DefaultReadEncoding { get; } = Encoding.UTF8;
public static Encoding DefaultWriteEncoding { get; } = new UTF8Encoding(false, true);
public static readonly Encoding DefaultReadEncoding = Encoding.UTF8;
public static readonly Encoding DefaultWriteEncoding = new UTF8Encoding(false, true);

public static Task WriteText(string path, string? contents, CancellationToken cancellationToken = default)
=> WriteText(path, contents, null, cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions src/Stl/Multitenancy/Tenant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace Stl.Multitenancy;
[DataContract, MemoryPackable(GenerateType.VersionTolerant)]
public partial record Tenant : IHasId<Symbol>
{
public static Tenant Default { get; } = new(Symbol.Empty, "The only tenant", "");
public static Tenant Dummy { get; } = new("*", "Example tenant", "__example");
public static readonly Tenant Default = new(Symbol.Empty, "The only tenant", "");
public static readonly Tenant Dummy = new("*", "Example tenant", "__example");

[DataMember, MemoryPackOrder(0)]
public Symbol Id { get; init; } = Symbol.Empty;
Expand Down
41 changes: 0 additions & 41 deletions src/Stl/OS/DisplayInfo.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Stl/OS/HardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class HardwareInfo
// Environment.TickCount is negative in WebAssembly @ startup
Environment.TickCount - (RefreshIntervalTicks << 1);

public static bool IsSingleThreaded { get; } = OSInfo.IsWebAssembly;
public static readonly bool IsSingleThreaded = OSInfo.IsWebAssembly;

public static int ProcessorCount {
get {
Expand Down
10 changes: 5 additions & 5 deletions src/Stl/Requirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public abstract record Requirement
public abstract object CheckUntyped([NotNull] object? value);
#endif

public static FuncRequirement<T> New<T>(ExceptionBuilder exceptionBuilder, Func<T?, bool> validator)
public static FuncRequirement<T> New<T>(ExceptionBuilder exceptionBuilder, Func<T?, bool> validator)
=> new(exceptionBuilder, validator);
public static FuncRequirement<T> New<T>(Func<T?, bool> validator)
public static FuncRequirement<T> New<T>(Func<T?, bool> validator)
=> new(validator);
}

public abstract record Requirement<T> : Requirement
{
private const string MustExistPropertyName = "MustExist";
private const string MustExistFieldOrPropertyName = "MustExist";
// ReSharper disable once StaticMemberInGenericType
private static readonly object MustExistLock = new();
private static volatile Requirement<T>? _mustExist;
Expand All @@ -36,10 +36,10 @@ public static Requirement<T> MustExist {

var type = typeof(T);
var result = type
.GetProperty(MustExistPropertyName, BindingFlags.Public | BindingFlags.Static)
.GetField(MustExistFieldOrPropertyName, BindingFlags.Public | BindingFlags.Static)
?.GetValue(null) as Requirement<T>;
result ??= type
.GetField(MustExistPropertyName, BindingFlags.Public | BindingFlags.Static)
.GetProperty(MustExistFieldOrPropertyName, BindingFlags.Public | BindingFlags.Static)
?.GetValue(null) as Requirement<T>;
result ??= MustExistRequirement<T>.Default;
return _mustExist = result;
Expand Down
Loading

0 comments on commit 8ee74d3

Please sign in to comment.