]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: reduce lexer allocs when parsing numeric constants
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 24 Feb 2015 20:54:57 +0000 (20:54 +0000)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 26 Feb 2015 01:46:16 +0000 (01:46 +0000)
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>

src/cmd/internal/gc/lex.go

index 19b969e57f9d0316681cf86964d19db67efedb2f..55c8d76bc246fe8b99970b0be95ed1d77d19e846 100644 (file)
@@ -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: