����λ�ã���ҳ > �����̳� > �̳� > �����²���Ϊʲô��ôɵ����Դ����������string.GetHashCode, ��Ҫ��һ�������Ż���hash����
�����и��õ�hash���� ��λ���Ѷ����²�ǰ�������оٵ���Դ������������dbӳ��ʵ����Ż��� ��ǰ���ɲ��� hashcode ���бȽ� ��ʾ���� public static void GenerateReadTokens(this IDataReader reader, Span<int>
��λ���Ѷ����²�ǰ�������оٵ���Դ������������dbӳ��ʵ����Ż��� ��ǰ���ɲ��� hashcode ���бȽ�
��ʾ����
public static void GenerateReadTokens(this IDataReader reader, Span s)
{
for (int i = 0; i < reader.FieldCount; i++)
{
var name = reader.GetName(i);
var type = reader.GetFieldType(i);
switch (EntitiesGenerator.SlowNonRandomizedHash(name))
{
case 742476188U:
s[i] = type == typeof(int) ? 1 : 2;
break;
case 2369371622U:
s[i] = type == typeof(string) ? 3 : 4;
break;
case 1352703673U:
s[i] = type == typeof(float) ? 5 : 6;
break;
default:
break;
}
}
}
����Ϊʲô����
string.GetHashCode
, ��Ҫ��
SlowNonRandomizedHash(name)
�� �и��õķ������ã�����ɵ
��ʱ��Ҳֻ�� �� ������ta����
string.GetHashCode
���û�취�ã�
��ϧ��ͷ��������ٶ࣬һʱҲ�޷�����ta���ӵ�Ŀ��
ֻ���ڴ˶�д���䡰�Ʊ硱
������ʵ
NormalizedHash
���ܺ�ǿ�ģ���ʵ������
public static uint SlowNonRandomizedHash(this string? value)
{
uint hash = 0;
if (!string.IsNullOrEmpty(value))
{
hash = 2166136261u;
foreach (char c in value!)
{
hash = (char.ToLowerInvariant(c) ^ hash) * 16777619;
}
}
return hash;
}
���Dz�������ǿ��ǿ��Ҳ����ֻ�������������Ô��
��ʵ��ʵÔ��ܶ��˶�֪�������Ǵ�ҵ�Ĭ�ϳ�ʶ�ˣ�net code
string.GetHashCode
������ģ�������г���ͬһ���ַ������ܻ���ÿ�����ж��в�ͬ�Ĺ�ϣֵ
���� 18������� Why is string.GetHashCode() different each time I run my program in .NET Core?
����򵥸���һ��������
������dz��򵥵ij���Ϊ�������������ε���һ���ַ���GetHashCode()
using System;
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!".GetHashCode());
Console.WriteLine("Hello World!".GetHashCode());
}
}
����� .NET Framework �����д˳�����ÿ�����иó���ʱ����������ͬ��ֵ��
> dotnet run -c Release -f net471
-1989043627
-1989043627
> dotnet run -c Release -f net471
-1989043627
-1989043627
> dotnet run -c Release -f net471
-1989043627
-1989043627
�෴�����Ϊ .NET Core ����ͬһ��������ͬһ����ִ����ÿ�ε��ö�������ͬ��ֵ�������ڲ�ͬ�ij���ִ�У�����ò�ͬ��ֵ��GetHashCode()
> dotnet run -c Release -f netcoreapp2.1
-1105880285
-1105880285
> dotnet run -c Release -f netcoreapp2.1
1569543669
1569543669
> dotnet run -c Release -f netcoreapp2.1
-1477343390
-1477343390
Ŭ������֮���� ΢���ٷ��ĵ�������ʹ��GetHashCode()�����Ľ��� ������ȷ��ʾ����Ӧ��GetHashCode()����������hashֵ����Ϊ��ͬ�ܳ־û���ֵʹ�á�
The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.
Stephen Toub ��һ�� issue ���ᵽ���������Ĵ�:
Q: Why .NET Core utilize randomized string hashing?
�ʣ�Ϊʲô .NET Core ʹ������ַ�����ϣ��
A: Security, prevention against DoS attacks, etc.
A����ȫ�ԡ���ֹ DoS �����ȡ�
Ô�ĺ���ϸ�Ľ����йذ�ȫ�����ݣ�����Ͳ�����ϸ������
��Ȼ�϶����еģ�string ���ڲ���ʵ���У�
����Ȥ��ͯЬ�����Ķ�Դ�� https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs#L923
���� ��Сд���кͲ����ж���ʵ�֣� ����������18�������оٵķ������и��������Ż�
�����ڲ�����������û�а취����ֱ��ʹ��
�����أ� �����к�ħ������ֱ��ʹ��
public static partial class StringHashing
{
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetNonRandomizedHashCodeOrdinalIgnoreCase")]
public static extern int Hash(this string c);
}
���Ƕ�д�������ˣ�����һ�����ܣ���ҿ϶�������
��һ�μ򵥵ıȽ�
[ShortRunJob, MemoryDiagnoser, Orderer(summaryOrderPolicy: SummaryOrderPolicy.FastestToSlowest), GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory), CategoriesColumn]
public class StringHashingBenchmarks
{
[Params(0, 1, 10, 100)]
public int Count { get; set; }
public string Str { get; set; }
[GlobalSetup]
public void Setup()
{
var s = string.Join("", Enumerable.Repeat("_", Count));
var b = Encoding.UTF8.GetBytes(s);
Random.Shared.NextBytes(b);
Str = Encoding.UTF8.GetString(b);
}
[Benchmark(Baseline = true)]
public int GetHashCode()
{
return Str.GetHashCode();
}
[Benchmark]
public uint SlowNonRandomizedHash()
{
return Str.SlowNonRandomizedHash();
}
[Benchmark]
public int NonRandomizedHash()
{
return Str.Hash();
}
}
���
BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3880/23H2/2023Update/SunValley3)
13th Gen Intel Core i9-13900KF, 1 CPU, 32 logical and 24 physical cores
.NET SDK 9.0.100-preview.6.24328.19
[Host] : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2
ShortRun : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2
Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3
Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|
SlowNonRandomizedHash | 0 | 0.3286 ns | 0.0727 ns | 0.0040 ns | 0.69 | 0.01 | - | NA |
GetHashCode | 0 | 0.4751 ns | 0.1093 ns | 0.0060 ns | 1.00 | 0.00 | - | NA |
NonRandomizedHash | 0 | 0.6614 ns | 0.0339 ns | 0.0019 ns | 1.39 | 0.02 | - | NA |
GetHashCode | 1 | 0.5686 ns | 0.0881 ns | 0.0048 ns | 1.00 | 0.00 | - | NA |
NonRandomizedHash | 1 | 0.6559 ns | 0.0254 ns | 0.0014 ns | 1.15 | 0.01 | - | NA |
SlowNonRandomizedHash | 1 | 7.3752 ns | 0.2379 ns | 0.0130 ns | 12.97 | 0.11 | - | NA |
GetHashCode | 10 | 3.1627 ns | 0.2081 ns | 0.0114 ns | 1.00 | 0.00 | - | NA |
NonRandomizedHash | 10 | 16.1921 ns | 1.1773 ns | 0.0645 ns | 5.12 | 0.02 | - | NA |
SlowNonRandomizedHash | 10 | 44.4825 ns | 2.8742 ns | 0.1575 ns | 14.06 | 0.01 | - | NA |
GetHashCode | 100 | 40.4233 ns | 0.7217 ns | 0.0396 ns | 1.00 | 0.00 | - | NA |
NonRandomizedHash | 100 | 110.2494 ns | 13.1581 ns | 0.7212 ns | 2.73 | 0.02 | - | NA |
SlowNonRandomizedHash | 100 | 362.0329 ns | 11.0681 ns | 0.6067 ns | 8.96 | 0.02 | - | NA |
��Ȼ�����DZȽϵ� hash code �Ǵ�Сд���еģ� �����������Ǵ�Сд�����еģ�
�������඼�dz�С�����Կ���˵���Ǻ�ǿ�ķ�����
��ϧ
UnsafeAccessor
��Щ��ħ���޷���Դ��������ʹ��
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������