]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify error sorting
authorMatthew Dempsky <mdempsky@google.com>
Fri, 26 Feb 2016 02:17:31 +0000 (18:17 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 26 Feb 2016 03:10:13 +0000 (03:10 +0000)
Errors have unique seq values (their index within the errors slice),
so errcmp never needs to fallback to sorting by message text.
Moreover, comparing by original index is exactly the purpose of using
a stable sort algorithm (and sort.Stable was added in Go 1.2), so we
really only need to compare by lineno.

Change-Id: I7f534b72a05d899ae9788dc7ef0541dd92a8b578
Reviewed-on: https://go-review.googlesource.com/19929
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/subr.go

index 0c8b4cd57f6484a6b4c5b837d91fc754b7978996..5287626aae5092a112a5db7718ea1cf8652b6c61 100644 (file)
@@ -19,7 +19,6 @@ import (
 
 type Error struct {
        lineno int
-       seq    int
        msg    string
 }
 
@@ -49,35 +48,24 @@ func adderrorname(n *Node) {
 
 func adderr(line int, format string, args ...interface{}) {
        errors = append(errors, Error{
-               seq:    len(errors),
                lineno: line,
                msg:    fmt.Sprintf("%v: %s\n", Ctxt.Line(line), fmt.Sprintf(format, args...)),
        })
 }
 
-// errcmp sorts errors by line, then seq, then message.
-type errcmp []Error
+// byLineno sorts errors by lineno.
+type byLineno []Error
 
-func (x errcmp) Len() int      { return len(x) }
-func (x errcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
-func (x errcmp) Less(i, j int) bool {
-       a := &x[i]
-       b := &x[j]
-       if a.lineno != b.lineno {
-               return a.lineno < b.lineno
-       }
-       if a.seq != b.seq {
-               return a.seq < b.seq
-       }
-       return a.msg < b.msg
-}
+func (x byLineno) Len() int           { return len(x) }
+func (x byLineno) Less(i, j int) bool { return x[i].lineno < x[j].lineno }
+func (x byLineno) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
 
 func Flusherrors() {
        bstdout.Flush()
        if len(errors) == 0 {
                return
        }
-       sort.Sort(errcmp(errors))
+       sort.Stable(byLineno(errors))
        for i := 0; i < len(errors); i++ {
                if i == 0 || errors[i].msg != errors[i-1].msg {
                        fmt.Printf("%s", errors[i].msg)