From: Josh Bleecher Snyder Date: Tue, 24 Feb 2015 20:54:57 +0000 (+0000) Subject: cmd/gc: reduce lexer allocs when parsing numeric constants X-Git-Tag: go1.5beta1~1857 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9f4c25e2236617c27e5b430c774b4d9c4b86ba1b;p=gostls13.git 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 += 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 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/internal/gc/lex.go b/src/cmd/internal/gc/lex.go index 19b969e57f..55c8d76bc2 100644 --- a/src/cmd/internal/gc/lex.go +++ b/src/cmd/internal/gc/lex.go @@ -853,6 +853,7 @@ func _yylex(yylval *yySymType) int32 { var rune_ uint var s *Sym var h *Loophack + var str string prevlineno = lineno @@ -1405,8 +1406,9 @@ ncu: 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 @@ ncu: 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 @@ caseep: 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 @@ casei: 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 @@ caseout: if Debug['x'] != 0 { fmt.Printf("lex: floating literal\n") } - litbuf = "literal " - litbuf += lexbuf.String() + litbuf = "literal " + str return LLITERAL strlit: