diff --git a/godoc/godoc.go b/godoc/godoc.go
index 8bda89ad..1063244a 100644
--- a/godoc/godoc.go
+++ b/godoc/godoc.go
@@ -233,24 +233,28 @@ func addStructFieldIDAttributes(buf *bytes.Buffer, name string, st *ast.StructTy
if st.Fields == nil {
return
}
- var scratch bytes.Buffer
+ // needsLink is a set of identifiers that still need to be
+ // linked, where value == key, to avoid an allocation in func
+ // linkedField.
+ needsLink := make(map[string]string)
+
for _, f := range st.Fields.List {
if len(f.Names) == 0 {
continue
}
fieldName := f.Names[0].Name
- scratch.Reset()
- var added bool
- foreachLine(buf.Bytes(), func(line []byte) {
- if !added && isLineForStructFieldID(line, fieldName) {
- added = true
- fmt.Fprintf(&scratch, ``, name, fieldName)
- }
- scratch.Write(line)
- })
- buf.Reset()
- buf.Write(scratch.Bytes())
+ needsLink[fieldName] = fieldName
}
+ var newBuf bytes.Buffer
+ foreachLine(buf.Bytes(), func(line []byte) {
+ if fieldName := linkedField(line, needsLink); fieldName != "" {
+ fmt.Fprintf(&newBuf, ``, name, fieldName)
+ delete(needsLink, fieldName)
+ }
+ newBuf.Write(line)
+ })
+ buf.Reset()
+ buf.Write(newBuf.Bytes())
}
// foreachLine calls fn for each line of in, where a line includes
@@ -270,9 +274,12 @@ func foreachLine(in []byte, fn func(line []byte)) {
// commentPrefix is the line prefix for comments after they've been HTMLified.
var commentPrefix = []byte(`