Skip to content

Commit 9833fdf

Browse files
authored
Merge pull request #33 from knz/20200520-is-shortcut
2 parents afcd467 + 4668c03 commit 9833fdf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

markers/markers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func Is(err, reference error) bool {
4343
}
4444
}
4545

46+
if err == nil {
47+
// Err is nil and reference is non-nil, so it cannot match. We
48+
// want to short-circuit the loop below in this case, otherwise
49+
// we're paying the expense of getMark() without need.
50+
return false
51+
}
52+
4653
// Not directly equal. Try harder, using error marks. We don't this
4754
// during the loop above as it may be more expensive.
4855
//
@@ -124,6 +131,14 @@ func IsAny(err error, references ...error) bool {
124131
}
125132
}
126133

134+
if err == nil {
135+
// The mark-based comparison below will never match anything if
136+
// the error is nil, so don't bother with computing the marks in
137+
// that case. This avoids the computational expense of computing
138+
// the reference marks upfront.
139+
return false
140+
}
141+
127142
// Try harder with marks.
128143
// Note: there is a more effective recursive algorithm that ensures
129144
// that any pair of string only gets compared once. Should this

0 commit comments

Comments
 (0)