From a1bb3030c8d8256c5c45c0caad25f171bd15031a Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Thu, 12 Mar 2015 15:20:33 -0700 Subject: [PATCH] cmd/yacc: generate arrays instead of slices where possible Yacc generates a bunch of global variables of the form var yyFoo = []int{...} where yyFoo is never subsequently modified to point to a different slice. Since these variables are implicitly compiled as var yyFoo = ([...]int{...})[:] anyway, by simply converting them all to var yyFoo = [...]int{...} we save sizeof(sliceStruct) bytes of data memory for each variable and also make len(yyFoo) into compile-time constant expressions, which shaves some bytes off text size: $ size 6g.before 6g.after text data bss dec hex filename 4598019 605968 342700 5546687 54a2bf 6g.before 4597810 605552 342700 5546062 54a04e 6g.after Change-Id: I53c7aa6efdb2d52738013e9d337a59afbfcb2494 Reviewed-on: https://go-review.googlesource.com/7520 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot Reviewed-by: Dave Cheney Reviewed-by: Josh Bleecher Snyder Reviewed-by: Russ Cox --- src/cmd/internal/gc/go.y | 2 -- src/cmd/internal/gc/y.go | 26 +++++++++++++------------- src/cmd/yacc/yacc.go | 10 +++++----- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/cmd/internal/gc/go.y b/src/cmd/internal/gc/go.y index 62d2556116..31bebf32b9 100644 --- a/src/cmd/internal/gc/go.y +++ b/src/cmd/internal/gc/go.y @@ -2240,7 +2240,6 @@ hidden_interfacedcl_list: } %% - func fixlbrace(lbr int) { // If the opening brace was an LBODY, // set up for another one now that we're done. @@ -2249,4 +2248,3 @@ func fixlbrace(lbr int) { loophack = 1 } } - diff --git a/src/cmd/internal/gc/y.go b/src/cmd/internal/gc/y.go index 8b272ec83f..f63bc01bc0 100644 --- a/src/cmd/internal/gc/y.go +++ b/src/cmd/internal/gc/y.go @@ -69,7 +69,7 @@ const NotPackage = 57392 const NotParen = 57393 const PreferToRightParen = 57394 -var yyToknames = []string{ +var yyToknames = [...]string{ "LLITERAL", "LASOP", "LCOLAS", @@ -130,7 +130,7 @@ var yyToknames = []string{ "')'", "PreferToRightParen", } -var yyStatenames = []string{} +var yyStatenames = [...]string{} const yyEofCode = 1 const yyErrCode = 2 @@ -147,7 +147,7 @@ func fixlbrace(lbr int) { } //line yacctab:1 -var yyExca = []int{ +var yyExca = [...]int{ -1, 1, 1, -1, -2, 0, @@ -272,7 +272,7 @@ var yyStates []string const yyLast = 2282 -var yyAct = []int{ +var yyAct = [...]int{ 74, 381, 304, 285, 291, 486, 610, 398, 545, 478, 549, 296, 186, 75, 400, 229, 302, 401, 103, 389, @@ -504,7 +504,7 @@ var yyAct = []int{ 137, 136, 0, 151, 141, 142, 143, 144, 145, 146, 147, 148, } -var yyPact = []int{ +var yyPact = [...]int{ -1000, -1000, 542, 536, -1000, 164, -1000, 550, 555, 318, -1000, -1000, -1000, 588, -1000, -1000, 549, 1340, 316, 155, @@ -574,7 +574,7 @@ var yyPact = []int{ 110, -1000, 37, 2017, -1000, -1000, 2017, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 1689, 518, -1000, } -var yyPgo = []int{ +var yyPgo = [...]int{ 0, 57, 771, 774, 45, 150, 26, 540, 29, 770, 768, 2, 28, 61, 322, 766, 17, 4, 765, 761, @@ -592,7 +592,7 @@ var yyPgo = []int{ 648, 647, 642, 639, 637, 634, 631, 627, 38, 623, 596, 593, } -var yyR1 = []int{ +var yyR1 = [...]int{ 0, 112, 114, 114, 116, 113, 115, 115, 119, 119, 119, 120, 120, 121, 121, 2, 2, 2, 117, 123, @@ -631,7 +631,7 @@ var yyR1 = []int{ 94, 94, 93, 93, 125, 125, 100, 100, 104, 104, 102, 102, } -var yyR2 = []int{ +var yyR2 = [...]int{ 0, 4, 0, 3, 0, 3, 0, 3, 2, 5, 3, 3, 2, 1, 3, 1, 2, 2, 4, 0, @@ -670,7 +670,7 @@ var yyR2 = []int{ 2, 1, 1, 5, 0, 2, 1, 3, 1, 3, 1, 3, } -var yyChk = []int{ +var yyChk = [...]int{ -1000, -112, -113, -116, -114, 26, -117, 26, -115, -3, 25, -91, 74, 75, -118, -124, 25, -60, -119, 22, @@ -740,7 +740,7 @@ var yyChk = []int{ 15, 73, -58, 61, 61, 61, 61, -52, 67, 67, 21, -11, 69, -99, -99, -134, -24, -53, } -var yyDef = []int{ +var yyDef = [...]int{ 4, -2, 2, 0, 6, 0, 21, 0, 218, 0, 157, 158, 159, 0, 5, 344, 19, -2, 0, 0, @@ -810,7 +810,7 @@ var yyDef = []int{ 0, 133, 0, 335, 343, 337, 335, 206, 56, 57, 82, 87, 145, 333, 208, 294, 0, 83, } -var yyTok1 = []int{ +var yyTok1 = [...]int{ 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, @@ -826,7 +826,7 @@ var yyTok1 = []int{ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 68, 52, 69, 71, } -var yyTok2 = []int{ +var yyTok2 = [...]int{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, @@ -835,7 +835,7 @@ var yyTok2 = []int{ 42, 43, 44, 45, 46, 47, 48, 49, 58, 59, 62, } -var yyTok3 = []int{ +var yyTok3 = [...]int{ 0, } diff --git a/src/cmd/yacc/yacc.go b/src/cmd/yacc/yacc.go index 68e59e5b45..c6d79e5143 100644 --- a/src/cmd/yacc/yacc.go +++ b/src/cmd/yacc/yacc.go @@ -517,14 +517,14 @@ outer: // put out names of token names ftable.WriteRune('\n') - fmt.Fprintf(ftable, "var %sToknames = []string{\n", prefix) + fmt.Fprintf(ftable, "var %sToknames = [...]string{\n", prefix) for i := TOKSTART; i <= ntokens; i++ { fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name) } fmt.Fprintf(ftable, "}\n") // put out names of state names - fmt.Fprintf(ftable, "var %sStatenames = []string{", prefix) + fmt.Fprintf(ftable, "var %sStatenames = [...]string{", prefix) // for i:=TOKSTART; i<=ntokens; i++ { // fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); // } @@ -2156,7 +2156,7 @@ func output() { if !lflag { fmt.Fprintf(ftable, "\n//line yacctab:1") } - fmt.Fprintf(ftable, "\nvar %sExca = []int{\n", prefix) + fmt.Fprintf(ftable, "\nvar %sExca = [...]int{\n", prefix) noset := mkset() @@ -2892,7 +2892,7 @@ func others() { arout("Tok2", temp1, c+1) // table 3 has everything else - fmt.Fprintf(ftable, "var %sTok3 = []int{\n\t", prefix) + fmt.Fprintf(ftable, "var %sTok3 = [...]int{\n\t", prefix) c = 0 for i = 1; i <= ntokens; i++ { j = tokset[i].value @@ -2937,7 +2937,7 @@ func others() { func arout(s string, v []int, n int) { s = prefix + s - fmt.Fprintf(ftable, "var %v = []int{\n", s) + fmt.Fprintf(ftable, "var %v = [...]int{\n", s) for i := 0; i < n; i++ { if i%10 == 0 { fmt.Fprintf(ftable, "\n\t")