Skip to content

Commit a553971

Browse files
author
merge-queue-bot
committed
Merge PR #512: perf: apply high-performance Go guidelines across five sites
2 parents e753eb0 + a39d52a commit a553971

28 files changed

Lines changed: 48 additions & 40 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ linters:
1111
- ineffassign
1212
- misspell
1313
- lll
14+
- prealloc
1415
- revive
1516
settings:
1617
lll:

cmd/mdsmith/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func runHelpPatterns(args []string) int {
6969
fmt.Fprintf(os.Stderr, "mdsmith: %v\n", err)
7070
return 2
7171
}
72-
items := make([]patternRec, 0)
72+
items := make([]patternRec, 0, len(rules))
7373
for _, r := range rules {
7474
if r.Maintainability == nil {
7575
continue

cmd/mdsmith/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func writeMetricsListJSON(defs []metricspkg.Definition) error {
326326
func writeMetricsRankText(rows []metricspkg.Row, defs []metricspkg.Definition) error {
327327
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
328328

329-
var headers []string
329+
headers := make([]string, 0, len(defs)+1)
330330
for _, def := range defs {
331331
headers = append(headers, strings.ToUpper(def.Name))
332332
}

internal/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ func TestYamlHasKeyRejectsAnchor(t *testing.T) {
13201320

13211321
// TestTopLevelKeySet_InvalidYAML covers the yaml.Unmarshal error
13221322
// branch of topLevelKeySet: a syntactically bad YAML payload
1323-
// returns an empty set (not a panic) so callers can degrade
1323+
// returns nil (not a panic) so callers can degrade
13241324
// gracefully.
13251325
func TestTopLevelKeySet_InvalidYAML(t *testing.T) {
13261326
assert.Empty(t, topLevelKeySet([]byte("{not: valid: yaml:")))

internal/config/convention_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestProvenance_ConventionLayerVisible(t *testing.T) {
175175
require.True(t, ok, "rule must appear in resolution")
176176
require.NotEmpty(t, rr.Layers)
177177

178-
var sources []string
178+
sources := make([]string, 0, len(rr.Layers))
179179
for _, l := range rr.Layers {
180180
sources = append(sources, l.Source)
181181
}
@@ -610,7 +610,7 @@ func TestProvenance_UserConventionLayerHasUserSuffix(t *testing.T) {
610610
rr, ok := res.Rules["markdown-flavor"]
611611
require.True(t, ok)
612612

613-
var sources []string
613+
sources := make([]string, 0, len(rr.Layers))
614614
for _, l := range rr.Layers {
615615
sources = append(sources, l.Source)
616616
}

internal/config/load.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,19 @@ func loadFromBytes(data []byte, sourcePath string, mergeKinds bool) (*Config, er
111111
}
112112

113113
// topLevelKeySet returns the set of top-level YAML mapping keys
114-
// present in data, or an empty set on parse error. It rejects
114+
// present in data, or nil on parse error. It rejects
115115
// anchor/alias usage for the same reason yamlHasKey does.
116116
func topLevelKeySet(data []byte) map[string]bool {
117117
node, err := yamlutil.UnmarshalNodeSafe(data)
118118
if err != nil {
119-
return map[string]bool{}
119+
return nil
120120
}
121121
if node.Kind != yaml.DocumentNode || len(node.Content) == 0 {
122-
return map[string]bool{}
122+
return nil
123123
}
124124
mapping := node.Content[0]
125125
if mapping.Kind != yaml.MappingNode {
126-
return map[string]bool{}
126+
return nil
127127
}
128128
result := make(map[string]bool, len(mapping.Content)/2)
129129
for i := 0; i < len(mapping.Content)-1; i += 2 {

internal/corpus/collect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Collect(cfg *Config, cacheDir string) ([]Record, error) {
1919

2020
allow := makeAllowset(cfg.LicenseAllowlist)
2121

22-
records := make([]Record, 0)
22+
records := make([]Record, 0, len(cfg.Sources))
2323
for idx, source := range cfg.Sources {
2424
sourceRecords, err := collectSource(
2525
cfg,

internal/corpus/qa.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func validateAnnotationsAgainstSample(
9090
annotations []QAAnnotation,
9191
) error {
9292
seenIDs := make(map[string]struct{}, len(annotations))
93-
extraIDs := make([]string, 0)
93+
extraIDs := make([]string, 0, len(annotations))
9494

9595
for _, annotation := range annotations {
9696
recordID := annotation.RecordID

internal/cuetemplate/cuetemplate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func buildSource(fm map[string]any, expr string) string {
173173
if err != nil {
174174
panic(fmt.Errorf("cuetemplate: encoding frontmatter: %w", err))
175175
}
176-
var src []byte
176+
var src []byte //nolint:prealloc
177177
src = append(src, []byte(
178178
"import \"strings\"\n\n"+
179179
"_strings_used: strings.Join([], \"\")\n")...)

internal/engine/check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func (r *mockMultiLineRule) ID() string { return r.id }
443443
func (r *mockMultiLineRule) Name() string { return r.name }
444444
func (r *mockMultiLineRule) Category() string { return "test" }
445445
func (r *mockMultiLineRule) Check(f *lint.File) []lint.Diagnostic {
446-
var diags []lint.Diagnostic
446+
diags := make([]lint.Diagnostic, 0, len(r.lines))
447447
for _, l := range r.lines {
448448
diags = append(diags, lint.Diagnostic{
449449
File: f.Path,

0 commit comments

Comments
 (0)