Skip to content

Commit 23a32df

Browse files
committed
RequirementMachine: Exit early in Atom::compare() if the atoms are equal
1 parent 08cc2a6 commit 23a32df

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/AST/RewriteSystem.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,26 @@ Atom Atom::forLayout(LayoutConstraint layout,
173173
///
174174
/// * For layout atoms, we use LayoutConstraint::compare().
175175
int Atom::compare(Atom other, const ProtocolGraph &graph) const {
176+
// Exit early if the atoms are equal.
177+
if (Ptr == other.Ptr)
178+
return 0;
179+
176180
auto kind = getKind();
177181
auto otherKind = other.getKind();
178182

179183
if (kind != otherKind)
180184
return int(kind) < int(otherKind) ? -1 : 1;
181185

186+
int result = 0;
187+
182188
switch (kind) {
183189
case Kind::Name:
184-
return getName().compare(other.getName());
190+
result = getName().compare(other.getName());
191+
break;
185192

186193
case Kind::Protocol:
187-
return graph.compareProtocols(getProtocol(), other.getProtocol());
194+
result = graph.compareProtocols(getProtocol(), other.getProtocol());
195+
break;
188196

189197
case Kind::AssociatedType: {
190198
auto protos = getProtocols();
@@ -200,7 +208,8 @@ int Atom::compare(Atom other, const ProtocolGraph &graph) const {
200208
return result;
201209
}
202210

203-
return getName().compare(other.getName());
211+
result = getName().compare(other.getName());
212+
break;
204213
}
205214

206215
case Kind::GenericParam: {
@@ -213,15 +222,16 @@ int Atom::compare(Atom other, const ProtocolGraph &graph) const {
213222
if (param->getIndex() != otherParam->getIndex())
214223
return param->getIndex() < otherParam->getIndex() ? -1 : 1;
215224

216-
return 0;
225+
break;
217226
}
218227

219-
case Kind::Layout: {
220-
return getLayoutConstraint().compare(other.getLayoutConstraint());
221-
}
228+
case Kind::Layout:
229+
result = getLayoutConstraint().compare(other.getLayoutConstraint());
230+
break;
222231
}
223232

224-
llvm_unreachable("Bad atom kind");
233+
assert(result != 0 && "Two distinct atoms should not compare equal");
234+
return result;
225235
}
226236

227237
/// Print the atom using our mnemonic representation.

0 commit comments

Comments
 (0)