Skip to content

Commit 5b4e3f2

Browse files
authored
Merge pull request #14386 from xwu/no-mixing
[stdlib/Sema] Minor improvements to synthesized hashValue
2 parents 77e417f + 8014793 commit 5b4e3f2

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ deriveBodyHashable_enum_hashValue(AbstractFunctionDecl *hashValueDecl) {
838838
for (auto elt : enumDecl->getAllElements()) {
839839
// case .<elt>(let a0, let a1, ...):
840840
SmallVector<VarDecl*, 3> payloadVars;
841-
SmallVector<ASTNode, 3> mixExpressions;
841+
SmallVector<ASTNode, 3> combineExprs;
842842

843843
auto payloadPattern = enumElementPayloadSubpattern(elt, 'a', hashValueDecl,
844844
payloadVars);
@@ -863,7 +863,7 @@ deriveBodyHashable_enum_hashValue(AbstractFunctionDecl *hashValueDecl) {
863863
/*implicit*/ true);
864864
auto assignExpr = new (C) AssignExpr(resultRef, SourceLoc(),
865865
ordinalExpr, /*implicit*/ true);
866-
mixExpressions.emplace_back(ASTNode(assignExpr));
866+
combineExprs.emplace_back(ASTNode(assignExpr));
867867
}
868868

869869
if (!hasNoAssociatedValues) {
@@ -873,18 +873,14 @@ deriveBodyHashable_enum_hashValue(AbstractFunctionDecl *hashValueDecl) {
873873
auto payloadVarRef = new (C) DeclRefExpr(payloadVar, DeclNameLoc(),
874874
/*implicit*/ true);
875875
// result = _combineHashValues(result, <payloadVar>.hashValue)
876-
auto mixExpr = combineHashValuesAssignmentExpr(C, resultVar,
877-
payloadVarRef);
878-
mixExpressions.emplace_back(ASTNode(mixExpr));
876+
auto combineExpr = combineHashValuesAssignmentExpr(C, resultVar,
877+
payloadVarRef);
878+
combineExprs.emplace_back(ASTNode(combineExpr));
879879
}
880-
881-
// result = _mixInt(result)
882-
auto assignExpr = mixIntAssignmentExpr(C, resultVar);
883-
mixExpressions.emplace_back(ASTNode(assignExpr));
884880
}
885881

886882
auto hasBoundDecls = !payloadVars.empty();
887-
auto body = BraceStmt::create(C, SourceLoc(), mixExpressions, SourceLoc());
883+
auto body = BraceStmt::create(C, SourceLoc(), combineExprs, SourceLoc());
888884
cases.push_back(CaseStmt::create(C, SourceLoc(), labelItem, hasBoundDecls,
889885
SourceLoc(), body));
890886
}
@@ -963,16 +959,12 @@ deriveBodyHashable_struct_hashValue(AbstractFunctionDecl *hashValueDecl) {
963959
auto selfPropertyExpr = new (C) DotSyntaxCallExpr(propertyRef, SourceLoc(),
964960
selfRef);
965961
// result = _combineHashValues(result, <property>.hashValue)
966-
auto mixExpr = combineHashValuesAssignmentExpr(C, resultVar,
967-
selfPropertyExpr);
968-
statements.emplace_back(ASTNode(mixExpr));
962+
auto combineExpr = combineHashValuesAssignmentExpr(C, resultVar,
963+
selfPropertyExpr);
964+
statements.emplace_back(ASTNode(combineExpr));
969965
}
970966

971967
{
972-
// result = _mixInt(result)
973-
auto assignExpr = mixIntAssignmentExpr(C, resultVar);
974-
statements.push_back(assignExpr);
975-
976968
// return result
977969
auto resultRef = new (C) DeclRefExpr(resultVar, DeclNameLoc(),
978970
/*implicit*/ true,
@@ -1021,7 +1013,6 @@ deriveHashable_hashValue(TypeChecker &tc, Decl *parentDecl,
10211013
// result = _combineHashValues(result, a0.hashValue)
10221014
// result = _combineHashValues(result, a1.hashValue)
10231015
// }
1024-
// result = _mixInt(result)
10251016
// return result
10261017
// }
10271018
// }
@@ -1030,10 +1021,9 @@ deriveHashable_hashValue(TypeChecker &tc, Decl *parentDecl,
10301021
// var x: Int
10311022
// var y: String
10321023
// @derived var hashValue: Int {
1033-
// var result: Int = 0
1024+
// var result = 0
10341025
// result = _combineHashValues(result, x.hashValue)
10351026
// result = _combineHashValues(result, y.hashValue)
1036-
// result = _mixInt(result)
10371027
// return result
10381028
// }
10391029
// }

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ extension DoubleWidth : Hashable {
154154
var result = 0
155155
result = _combineHashValues(result, _storage.high.hashValue)
156156
result = _combineHashValues(result, _storage.low.hashValue)
157-
result = _mixInt(result)
158157
return result
159158
}
160159
}

stdlib/public/core/Hashing.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,13 @@ func _squeezeHashValue(_ hashValue: Int, _ upperBound: Int) -> Int {
203203
@_transparent
204204
public // @testable
205205
func _combineHashValues(_ firstValue: Int, _ secondValue: Int) -> Int {
206-
let magic = 0x9e3779b9 as UInt // Based on the golden ratio.
206+
// Use a magic number based on the golden ratio
207+
// (0x1.9e3779b97f4a7c15f39cc0605cedc8341082276bf3a27251f86c6a11d0c18e95p0).
208+
#if arch(i386) || arch(arm)
209+
let magic = 0x9e3779b9 as UInt
210+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
211+
let magic = 0x9e3779b97f4a7c15 as UInt
212+
#endif
207213
var x = UInt(bitPattern: firstValue)
208214
x ^= UInt(bitPattern: secondValue) &+ magic &+ (x &<< 6) &+ (x &>> 2)
209215
return Int(bitPattern: x)

0 commit comments

Comments
 (0)