From: Matthew Dempsky Date: Fri, 26 Feb 2016 02:17:31 +0000 (-0800) Subject: cmd/compile: simplify error sorting X-Git-Tag: go1.7beta1~1704 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a5b7a8d6dd143b5b0b9631b0281559938d542c78;p=gostls13.git cmd/compile: simplify error sorting 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go index 0c8b4cd57f..5287626aae 100644 --- a/src/cmd/compile/internal/gc/subr.go +++ b/src/cmd/compile/internal/gc/subr.go @@ -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)