-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Speed up multiple attributes overwrite detection. Fixes #24467 #24561
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we assign the coefficients here to constants? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally I'd prefer not to add the extra abstraction unless it's a case where we think there are multiple uses that need to be kept in sync. Let me know if you want to discuss it - maybe there are other reasons I'm missing! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I posted the feedback in the interest of readability. I totally hear you about it not being technically necessary. Maybe the middleware group is an inline comment (e.g. |
||
+ 961 * keyLength); | ||
} | ||
else | ||
{ | ||
return default; | ||
} | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍