Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit f0b6829

Browse files
author
Jay Camp
authored
Fix selfdescribe ast cache (#703)
This brings down selfdescribe.json generation from 6s to 1.2s.
1 parent afa1e82 commit f0b6829

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

cmd/agent/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func doStatus() {
8585
// Print out agent self-description of config/metadata
8686
func doSelfDescribe() {
8787
log.SetOutput(os.Stderr)
88-
fmt.Print(selfdescribe.JSON())
88+
selfdescribe.JSON(os.Stdout)
8989
}
9090

9191
// glog is a transitive dependency of the agent and puts a bunch of flags in

internal/selfdescribe/selfdescribe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package selfdescribe
77

88
import (
99
"encoding/json"
10+
"io"
1011
"reflect"
1112

1213
"github.com/signalfx/signalfx-agent/internal/core/config"
@@ -17,7 +18,7 @@ import (
1718
// various components in the agent. It is meant to be used as an intermediate
1819
// form which serves as a data source for automatically generating docs about
1920
// the agent.
20-
func JSON() string {
21+
func JSON(writer io.Writer) {
2122
out, err := json.MarshalIndent(map[string]interface{}{
2223
"TopConfig": getStructMetadata(reflect.TypeOf(config.Config{})),
2324
"GenericMonitorConfig": getStructMetadata(reflect.TypeOf(config.MonitorConfig{})),
@@ -29,6 +30,5 @@ func JSON() string {
2930
if err != nil {
3031
panic(err)
3132
}
32-
33-
return string(out)
33+
writer.Write(out)
3434
}

internal/selfdescribe/sourcedocs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"strings"
1313
)
1414

15-
var astCache = make(map[string]struct {
15+
type astCacheEntry struct {
1616
fset *token.FileSet
1717
pkgs map[string]*ast.Package
18-
})
18+
}
19+
20+
var astCache = map[string]astCacheEntry{}
1921

2022
// Returns the ast node of the struct itself and the comment group on the
2123
// struct type.
@@ -34,6 +36,7 @@ func structNodes(packageDir, structName string) (*ast.TypeSpec, *ast.CommentGrou
3436
if err != nil {
3537
panic(err)
3638
}
39+
astCache[packageDir] = astCacheEntry{fset: fset, pkgs: pkgs}
3740
}
3841

3942
for _, p := range pkgs {
@@ -112,7 +115,7 @@ func notesFromDocs(docs []*doc.Package, noteType string) []*doc.Note {
112115

113116
func structFieldDocs(packageDir, structName string) map[string]string {
114117
configStruct, _ := structNodes(packageDir, structName)
115-
fieldDocs := make(map[string]string)
118+
fieldDocs := map[string]string{}
116119
for _, field := range configStruct.Type.(*ast.StructType).Fields.List {
117120
if field.Names != nil {
118121
fieldDocs[field.Names[0].Name] = commentTextToParagraphs(field.Doc.Text())

0 commit comments

Comments
 (0)