]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: use strings.Split instead of specialized code
authorRobert Griesemer <gri@golang.org>
Thu, 28 Mar 2013 22:47:39 +0000 (15:47 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 28 Mar 2013 22:47:39 +0000 (15:47 -0700)
With the faster strings package, the difference between
the specialized code and strings.Split is in the noise:

benchmark         old ns/op    new ns/op    delta
BenchmarkPrint     16724291     16686729   -0.22%

(Measured on a Mac Pro, 2.8GHz Quad-core Intel Xeon,
4GB 800 MHz DDR2, Mac OS X 10.8.3)

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/8100044

src/pkg/go/printer/printer.go

index 3c8d23e6553fb7b1cb7787645f4686aefebf69c2..e06d2edfb21c2e8da51d07c2c9f572c21a99c3e3 100644 (file)
@@ -395,35 +395,6 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as
        }
 }
 
-// Split comment text into lines
-// (using strings.Split(text, "\n") is significantly slower for
-// this specific purpose, as measured with: go test -bench=Print)
-//
-func split(text string) []string {
-       // count lines (comment text never ends in a newline)
-       n := 1
-       for i := 0; i < len(text); i++ {
-               if text[i] == '\n' {
-                       n++
-               }
-       }
-
-       // split
-       lines := make([]string, n)
-       n = 0
-       i := 0
-       for j := 0; j < len(text); j++ {
-               if text[j] == '\n' {
-                       lines[n] = text[i:j] // exclude newline
-                       i = j + 1            // discard newline
-                       n++
-               }
-       }
-       lines[n] = text[i:]
-
-       return lines
-}
-
 // Returns true if s contains only white space
 // (only tabs and blanks can appear in the printer's context).
 //
@@ -616,7 +587,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
 
        // for /*-style comments, print line by line and let the
        // write function take care of the proper indentation
-       lines := split(text)
+       lines := strings.Split(text, "\n")
 
        // The comment started in the first column but is going
        // to be indented. For an idempotent result, add indentation