-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Cache the hash in compilation options #62289
Changes from 4 commits
f47dd37
14c6136
bfaf84b
b3292a9
4b6bb80
1bdfa3c
895a293
8831003
4630dda
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 |
---|---|---|
|
@@ -261,6 +261,8 @@ protected set | |
|
||
private readonly Lazy<ImmutableArray<Diagnostic>> _lazyErrors; | ||
|
||
private int _hashCode; | ||
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. Any particular reason you're using 0 as the sentinel, not making this 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. force of habit. it's just a pattern i got very accustomed to for some reason over my years :) happy to change to nullable if you'd like :) 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. it's also likely that i trust ints more (with nullable, not sure what the multithreading concerns may be) :) 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. It's fine to leave is as, was more for understanding why this approach was taken. 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 think an If we're concerned about multithreading, is there a need for some kind of memory barrier when we write to 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 don't believe that there's anything we need to be concerned about with 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. yup. i like int because it's just so safe. |
||
|
||
// Expects correct arguments. | ||
internal CompilationOptions( | ||
OutputKind outputKind, | ||
|
@@ -651,7 +653,18 @@ protected bool EqualsHelper([NotNullWhen(true)] CompilationOptions? other) | |
return equal; | ||
} | ||
|
||
public abstract override int GetHashCode(); | ||
public sealed override int GetHashCode() | ||
{ | ||
if (_hashCode == 0) | ||
{ | ||
var hashCode = ComputeHashCode(); | ||
_hashCode = hashCode == 0 ? 1 : hashCode; | ||
} | ||
|
||
return _hashCode; | ||
} | ||
|
||
protected abstract int ComputeHashCode(); | ||
|
||
protected int GetHashCodeHelper() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,25 @@ internal static int CombineValues<T>(IEnumerable<T>? values, int maxItemsToHash | |
return hashCode; | ||
} | ||
|
||
internal static int CombineValues<TKey, TValue>(ImmutableDictionary<TKey, TValue> values, int maxItemsToHash = int.MaxValue) | ||
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. these helpers ensure that when we do compute the hashcode we dont' incur allocations going through the 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. they are copied from teh IEnumerable version, just tweaked accordingly. we really need shapes :) |
||
where TKey : notnull | ||
{ | ||
if (values == null) | ||
return 0; | ||
|
||
var hashCode = 0; | ||
var count = 0; | ||
foreach (var value in values) | ||
{ | ||
if (count++ >= maxItemsToHash) | ||
break; | ||
|
||
hashCode = Hash.Combine(value.GetHashCode(), hashCode); | ||
} | ||
|
||
return hashCode; | ||
} | ||
|
||
internal static int CombineValues<T>(T[]? values, int maxItemsToHash = int.MaxValue) | ||
{ | ||
if (values == null) | ||
|
@@ -143,6 +162,31 @@ internal static int CombineValues(IEnumerable<string?>? values, StringComparer s | |
return hashCode; | ||
} | ||
|
||
internal static int CombineValues(ImmutableArray<string> values, StringComparer stringComparer, int maxItemsToHash = int.MaxValue) | ||
{ | ||
if (values == null) | ||
{ | ||
return 0; | ||
} | ||
|
||
var hashCode = 0; | ||
var count = 0; | ||
foreach (var value in values) | ||
{ | ||
if (count++ >= maxItemsToHash) | ||
{ | ||
break; | ||
} | ||
|
||
if (value != null) | ||
{ | ||
hashCode = Hash.Combine(stringComparer.GetHashCode(value), hashCode); | ||
} | ||
CyrusNajmabadi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return hashCode; | ||
} | ||
|
||
/// <summary> | ||
/// The offset bias value used in the FNV-1a algorithm | ||
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function | ||
|
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.
this is safe. it's jsut a removal of hte override.