cmd/gc: reduce lexer allocs when parsing numeric constants

This reduces the number of allocs when
running the rotate.go tests by
about 20%, after applying CL 5700.

Combining

s = "const str"
s += <another string>

generally saves an alloc and might be a candidate for
rsc's grind tool. However, I'm sending this CL now
because this also reuses the result of calling lexbuf.String.

Change-Id: If3a7300b7da9612ab62bb910ee90349dca88dde3
Reviewed-on: https://go-review.googlesource.com/5821
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/cmd/internal/gc/lex.go b/src/cmd/internal/gc/lex.go
index 19b969e..55c8d76 100644
--- a/src/cmd/internal/gc/lex.go
+++ b/src/cmd/internal/gc/lex.go
@@ -853,6 +853,7 @@
 	var rune_ uint
 	var s *Sym
 	var h *Loophack
+	var str string
 
 	prevlineno = lineno
 
@@ -1405,8 +1406,9 @@
 	cp = nil
 	ungetc(c)
 
+	str = lexbuf.String()
 	yylval.val.U.Xval = new(Mpint)
-	mpatofix(yylval.val.U.Xval, lexbuf.String())
+	mpatofix(yylval.val.U.Xval, str)
 	if yylval.val.U.Xval.Ovf != 0 {
 		Yyerror("overflow in constant")
 		Mpmovecfix(yylval.val.U.Xval, 0)
@@ -1416,8 +1418,7 @@
 	if Debug['x'] != 0 {
 		fmt.Printf("lex: integer literal\n")
 	}
-	litbuf = "literal "
-	litbuf += lexbuf.String()
+	litbuf = "literal " + str
 	return LLITERAL
 
 casedot:
@@ -1461,9 +1462,10 @@
 casei:
 	cp = nil
 
+	str = lexbuf.String()
 	yylval.val.U.Cval = new(Mpcplx)
 	Mpmovecflt(&yylval.val.U.Cval.Real, 0.0)
-	mpatoflt(&yylval.val.U.Cval.Imag, lexbuf.String())
+	mpatoflt(&yylval.val.U.Cval.Imag, str)
 	if yylval.val.U.Cval.Imag.Val.Ovf != 0 {
 		Yyerror("overflow in imaginary constant")
 		Mpmovecflt(&yylval.val.U.Cval.Real, 0.0)
@@ -1473,16 +1475,16 @@
 	if Debug['x'] != 0 {
 		fmt.Printf("lex: imaginary literal\n")
 	}
-	litbuf = "literal "
-	litbuf += lexbuf.String()
+	litbuf = "literal " + str
 	return LLITERAL
 
 caseout:
 	cp = nil
 	ungetc(c)
 
+	str = lexbuf.String()
 	yylval.val.U.Fval = new(Mpflt)
-	mpatoflt(yylval.val.U.Fval, lexbuf.String())
+	mpatoflt(yylval.val.U.Fval, str)
 	if yylval.val.U.Fval.Val.Ovf != 0 {
 		Yyerror("overflow in float constant")
 		Mpmovecflt(yylval.val.U.Fval, 0.0)
@@ -1492,8 +1494,7 @@
 	if Debug['x'] != 0 {
 		fmt.Printf("lex: floating literal\n")
 	}
-	litbuf = "literal "
-	litbuf += lexbuf.String()
+	litbuf = "literal " + str
 	return LLITERAL
 
 strlit: