Skip to content

Commit 575b88b

Browse files
committed
stringer: avoid if's in the generated code
Suggestion by dsymonds: Save code not data. Add an extra element to the index array and an if can be eliminated. Old generated code: const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" var _Day_index = [...]uint8{6, 13, 22, 30, 36, 44, 50} func (i Day) String() string { if i < 0 || i >= Day(len(_Day_index)) { return fmt.Sprintf("Day(%d)", i) } hi := _Day_index[i] lo := uint8(0) if i > 0 { lo = _Day_index[i-1] } return _Day_name[lo:hi] } New generated code: const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50} func (i Day) String() string { if i < 0 || i+1 >= Day(len(_Day_index)) { return fmt.Sprintf("Day(%d)", i) } return _Day_name[_Day_index[i]:_Day_index[i+1]] } Change-Id: I6f46a4892d5813a12ec1ad01738c6a21c7e45172 Reviewed-on: https://go-review.googlesource.com/1990 Reviewed-by: David Symonds <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 4d81e11 commit 575b88b

File tree

2 files changed

+25
-69
lines changed

2 files changed

+25
-69
lines changed

cmd/stringer/golden_test.go

+18-49
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,13 @@ const (
4848
const day_out = `
4949
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
5050
51-
var _Day_index = [...]uint8{6, 13, 22, 30, 36, 44, 50}
51+
var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
5252
5353
func (i Day) String() string {
54-
if i < 0 || i >= Day(len(_Day_index)) {
54+
if i < 0 || i+1 >= Day(len(_Day_index)) {
5555
return fmt.Sprintf("Day(%d)", i)
5656
}
57-
hi := _Day_index[i]
58-
lo := uint8(0)
59-
if i > 0 {
60-
lo = _Day_index[i-1]
61-
}
62-
return _Day_name[lo:hi]
57+
return _Day_name[_Day_index[i]:_Day_index[i+1]]
6358
}
6459
`
6560

@@ -78,19 +73,14 @@ const (
7873
const offset_out = `
7974
const _Number_name = "OneTwoThree"
8075
81-
var _Number_index = [...]uint8{3, 6, 11}
76+
var _Number_index = [...]uint8{0, 3, 6, 11}
8277
8378
func (i Number) String() string {
8479
i -= 1
85-
if i < 0 || i >= Number(len(_Number_index)) {
80+
if i < 0 || i+1 >= Number(len(_Number_index)) {
8681
return fmt.Sprintf("Number(%d)", i+1)
8782
}
88-
hi := _Number_index[i]
89-
lo := uint8(0)
90-
if i > 0 {
91-
lo = _Number_index[i-1]
92-
}
93-
return _Number_name[lo:hi]
83+
return _Number_name[_Number_index[i]:_Number_index[i+1]]
9484
}
9585
`
9686

@@ -116,27 +106,19 @@ const (
116106
)
117107
118108
var (
119-
_Gap_index_0 = [...]uint8{3, 8}
120-
_Gap_index_1 = [...]uint8{4, 7, 12, 17, 21}
121-
_Gap_index_2 = [...]uint8{6}
109+
_Gap_index_0 = [...]uint8{0, 3, 8}
110+
_Gap_index_1 = [...]uint8{0, 4, 7, 12, 17, 21}
111+
_Gap_index_2 = [...]uint8{0, 6}
122112
)
123113
124114
func (i Gap) String() string {
125115
switch {
126116
case 2 <= i && i <= 3:
127117
i -= 2
128-
lo := uint8(0)
129-
if i > 0 {
130-
lo = _Gap_index_0[i-1]
131-
}
132-
return _Gap_name_0[lo:_Gap_index_0[i]]
118+
return _Gap_name_0[_Gap_index_0[i]:_Gap_index_0[i+1]]
133119
case 5 <= i && i <= 9:
134120
i -= 5
135-
lo := uint8(0)
136-
if i > 0 {
137-
lo = _Gap_index_1[i-1]
138-
}
139-
return _Gap_name_1[lo:_Gap_index_1[i]]
121+
return _Gap_name_1[_Gap_index_1[i]:_Gap_index_1[i+1]]
140122
case i == 11:
141123
return _Gap_name_2
142124
default:
@@ -159,19 +141,14 @@ const (
159141
const num_out = `
160142
const _Num_name = "m_2m_1m0m1m2"
161143
162-
var _Num_index = [...]uint8{3, 6, 8, 10, 12}
144+
var _Num_index = [...]uint8{0, 3, 6, 8, 10, 12}
163145
164146
func (i Num) String() string {
165147
i -= -2
166-
if i < 0 || i >= Num(len(_Num_index)) {
148+
if i < 0 || i+1 >= Num(len(_Num_index)) {
167149
return fmt.Sprintf("Num(%d)", i+-2)
168150
}
169-
hi := _Num_index[i]
170-
lo := uint8(0)
171-
if i > 0 {
172-
lo = _Num_index[i-1]
173-
}
174-
return _Num_name[lo:hi]
151+
return _Num_name[_Num_index[i]:_Num_index[i+1]]
175152
}
176153
`
177154

@@ -196,25 +173,17 @@ const (
196173
)
197174
198175
var (
199-
_Unum_index_0 = [...]uint8{2, 4, 6}
200-
_Unum_index_1 = [...]uint8{3, 6}
176+
_Unum_index_0 = [...]uint8{0, 2, 4, 6}
177+
_Unum_index_1 = [...]uint8{0, 3, 6}
201178
)
202179
203180
func (i Unum) String() string {
204181
switch {
205182
case 0 <= i && i <= 2:
206-
lo := uint8(0)
207-
if i > 0 {
208-
lo = _Unum_index_0[i-1]
209-
}
210-
return _Unum_name_0[lo:_Unum_index_0[i]]
183+
return _Unum_name_0[_Unum_index_0[i]:_Unum_index_0[i+1]]
211184
case 253 <= i && i <= 254:
212185
i -= 253
213-
lo := uint8(0)
214-
if i > 0 {
215-
lo = _Unum_index_1[i-1]
216-
}
217-
return _Unum_name_1[lo:_Unum_index_1[i]]
186+
return _Unum_name_1[_Unum_index_1[i]:_Unum_index_1[i+1]]
218187
default:
219188
return fmt.Sprintf("Unum(%d)", i)
220189
}

cmd/stringer/stringer.go

+7-20
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix
519519
nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String())
520520
nameLen := b.Len()
521521
b.Reset()
522-
fmt.Fprintf(b, "_%s_index%s = [...]uint%d{", typeName, suffix, usize(nameLen))
522+
fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen))
523523
for i, v := range indexes {
524524
if i > 0 {
525525
fmt.Fprintf(b, ", ")
@@ -563,15 +563,10 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
563563
// [2]: size of index element (8 for uint8 etc.)
564564
// [3]: less than zero check (for signed types)
565565
const stringOneRun = `func (i %[1]s) String() string {
566-
if %[3]si >= %[1]s(len(_%[1]s_index)) {
566+
if %[3]si+1 >= %[1]s(len(_%[1]s_index)) {
567567
return fmt.Sprintf("%[1]s(%%d)", i)
568568
}
569-
hi := _%[1]s_index[i]
570-
lo := uint%[2]d(0)
571-
if i > 0 {
572-
lo = _%[1]s_index[i-1]
573-
}
574-
return _%[1]s_name[lo:hi]
569+
return _%[1]s_name[_%[1]s_index[i]:_%[1]s_index[i+1]]
575570
}
576571
`
577572

@@ -584,15 +579,10 @@ const stringOneRun = `func (i %[1]s) String() string {
584579
*/
585580
const stringOneRunWithOffset = `func (i %[1]s) String() string {
586581
i -= %[2]s
587-
if %[4]si >= %[1]s(len(_%[1]s_index)) {
582+
if %[4]si+1 >= %[1]s(len(_%[1]s_index)) {
588583
return fmt.Sprintf("%[1]s(%%d)", i + %[2]s)
589584
}
590-
hi := _%[1]s_index[i]
591-
lo := uint%[3]d(0)
592-
if i > 0 {
593-
lo = _%[1]s_index[i-1]
594-
}
595-
return _%[1]s_name[lo : hi]
585+
return _%[1]s_name[_%[1]s_index[i] : _%[1]s_index[i+1]]
596586
}
597587
`
598588

@@ -613,11 +603,8 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) {
613603
if values[0].value != 0 {
614604
g.Printf("\t\ti -= %s\n", &values[0])
615605
}
616-
g.Printf("\t\tlo := uint%d(0)\n", usize(len(values)))
617-
g.Printf("\t\tif i > 0 {\n")
618-
g.Printf("\t\t\tlo = _%s_index_%d[i-1]\n", typeName, i)
619-
g.Printf("\t\t}\n")
620-
g.Printf("\t\treturn _%s_name_%d[lo:_%s_index_%d[i]]\n", typeName, i, typeName, i)
606+
g.Printf("\t\treturn _%s_name_%d[_%s_index_%d[i]:_%s_index_%d[i+1]]\n",
607+
typeName, i, typeName, i, typeName, i)
621608
}
622609
g.Printf("\tdefault:\n")
623610
g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName)

0 commit comments

Comments
 (0)