-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d99644e
commit f058274
Showing
3 changed files
with
100 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Components/Components/src/Rendering/SimplifiedStringHashComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNetCore.Components.Rendering | ||
{ | ||
/// <summary> | ||
/// This comparer is optimized for use with dictionaries where the great majority of insertions/lookups | ||
/// don't match existing entries. For example, when building a dictionary of almost entirely unique keys. | ||
/// It's faster than the normal string comparer in this case because it doesn't use string.GetHashCode, | ||
/// and hence doesn't have to consider every character in the string. | ||
/// | ||
/// This primary scenario is <see cref="RenderTreeBuilder.ProcessDuplicateAttributes(int)"/>, which needs | ||
/// to detect when one attribute is overriding another, but in the vast majority of cases attributes don't | ||
/// actually override each other. | ||
/// </summary> | ||
internal class SimplifiedStringHashComparer : IEqualityComparer<string> | ||
{ | ||
public readonly static SimplifiedStringHashComparer Instance = new SimplifiedStringHashComparer(); | ||
|
||
public bool Equals(string? x, string? y) | ||
{ | ||
return string.Equals(x, y, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public int GetHashCode(string key) | ||
{ | ||
var keyLength = key.Length; | ||
if (keyLength > 0) | ||
{ | ||
// Consider just the length and middle and last characters. | ||
// This will produce a distinct result for a sufficiently large | ||
// proportion of attribute names. | ||
return unchecked( | ||
char.ToLowerInvariant(key[keyLength - 1]) | ||
+ 31 * char.ToLowerInvariant(key[keyLength / 2]) | ||
+ 961 * keyLength); | ||
} | ||
else | ||
{ | ||
return default; | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Components/Components/test/Rendering/SimplifiedStringHashComparerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Components.Rendering | ||
{ | ||
public class SimplifiedStringHashComparerTest | ||
{ | ||
[Fact] | ||
public void EqualityIsCaseInsensitive() | ||
{ | ||
Assert.True(SimplifiedStringHashComparer.Instance.Equals("abc", "ABC")); | ||
} | ||
|
||
[Fact] | ||
public void HashCodesAreCaseInsensitive() | ||
{ | ||
var hash1 = SimplifiedStringHashComparer.Instance.GetHashCode("abc"); | ||
var hash2 = SimplifiedStringHashComparer.Instance.GetHashCode("ABC"); | ||
Assert.Equal(hash1, hash2); | ||
} | ||
} | ||
} |