Skip to content

Commit 09f22d3

Browse files
author
merge-queue-bot
committed
Merge PR #688: perf: apply high-performance-go guidelines across 5 hot paths (3-round review)
2 parents 3d65431 + 3a45453 commit 09f22d3

9 files changed

Lines changed: 100 additions & 33 deletions

File tree

internal/export/export.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,18 @@ func inGeneratedRange(line int, ranges []lint.LineRange) bool {
244244
// because such PIs sit inside a pair's body range and are skipped
245245
// here.
246246
func stripDirectives(f *lint.File, directives []directiveStrip) []byte {
247-
stripLines := map[int]bool{}
248-
bodyLines := map[int]bool{}
247+
stripLines := map[int]struct{}{}
248+
bodyLines := map[int]struct{}{}
249249

250250
for _, d := range directives {
251251
pairs, _ := gensection.FindMarkerPairs(f, d.name, d.ruleID, d.ruleName)
252252
for _, p := range pairs {
253253
for line := p.StartLine; line < p.ContentFrom; line++ {
254-
stripLines[line] = true
254+
stripLines[line] = struct{}{}
255255
}
256-
stripLines[p.EndLine] = true
256+
stripLines[p.EndLine] = struct{}{}
257257
for line := p.ContentFrom; line <= p.ContentTo; line++ {
258-
bodyLines[line] = true
258+
bodyLines[line] = struct{}{}
259259
}
260260
}
261261
}
@@ -276,7 +276,7 @@ func stripDirectives(f *lint.File, directives []directiveStrip) []byte {
276276
continue
277277
}
278278
for line := startLine; line <= endLine; line++ {
279-
stripLines[line] = true
279+
stripLines[line] = struct{}{}
280280
}
281281
}
282282

@@ -316,20 +316,20 @@ func piLineRange(pi *piparser.ProcessingInstruction, f *lint.File) (int, int) {
316316
return start, f.LineOfOffset(pi.ClosureLine.Start)
317317
}
318318

319-
func overlapsAny(from, to int, set map[int]bool) bool {
319+
func overlapsAny(from, to int, set map[int]struct{}) bool {
320320
for line := from; line <= to; line++ {
321-
if set[line] {
321+
if _, ok := set[line]; ok {
322322
return true
323323
}
324324
}
325325
return false
326326
}
327327

328-
func emitLines(srcLines [][]byte, strip map[int]bool) []byte {
328+
func emitLines(srcLines [][]byte, strip map[int]struct{}) []byte {
329329
var b bytes.Buffer
330330
for i, line := range srcLines {
331331
lineNum := i + 1
332-
if strip[lineNum] {
332+
if _, ok := strip[lineNum]; ok {
333333
continue
334334
}
335335
b.Write(line)

internal/index/locate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,11 @@ func listItemValue(line string) (string, bool) {
462462
// Returns "" when none precedes the item.
463463
func enclosingListKey(lines [][]byte, line int) string {
464464
for i := line - 2; i >= 0; i-- {
465-
m := piArgRE.FindStringSubmatch(string(lines[i]))
466-
if len(m) >= 3 && strings.TrimSpace(m[2]) == "" {
467-
return m[1]
465+
// FindSubmatch accepts []byte directly, avoiding a string allocation
466+
// per scanned line when the caller passes bytes.
467+
m := piArgRE.FindSubmatch(lines[i])
468+
if len(m) >= 3 && len(m[2]) == 0 {
469+
return string(m[1])
468470
}
469471
// A populated `key: value` line or another list item does not
470472
// open a list block for our item; keep scanning past list items

internal/index/locate_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,15 @@ func TestLocateFrontMatterKindsListItemWithDifferentValues(t *testing.T) {
248248
assert.Equal(t, "kinds", res.FrontMatterKey)
249249
assert.Equal(t, "reference", res.FrontMatterValue)
250250
}
251+
252+
func TestEnclosingListKey_FindsParentKey(t *testing.T) {
253+
lines := [][]byte{
254+
[]byte("inputs:"),
255+
[]byte(" - alpha"),
256+
[]byte(" - beta"),
257+
[]byte(" - gamma"),
258+
}
259+
// Line 4 (1-based) is "gamma"; the enclosing key is "inputs".
260+
got := enclosingListKey(lines, 4)
261+
assert.Equal(t, "inputs", got)
262+
}

internal/lsp/rename.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func isValidRefDefLine(source []byte, line int) bool {
8080
if bodyLine < 1 {
8181
return false
8282
}
83-
return rename.ValidRefDefBodyLines(body)[bodyLine]
83+
_, ok := rename.ValidRefDefBodyLines(body)[bodyLine]
84+
return ok
8485
}
8586

8687
// headingPrepareRange builds the rename range for an ATX or setext

internal/rename/rename.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ func LinkRef(source []byte, oldLabel, newName string) ([]Edit, error) {
9191
// reference definition goldmark accepted (not a code-block
9292
// look-alike). The LSP prepare-rename gate consults it so the rename
9393
// UI never surfaces on a `[label]: url`-shaped code sample.
94-
func ValidRefDefBodyLines(body []byte) map[int]bool {
95-
out := map[int]bool{}
94+
func ValidRefDefBodyLines(body []byte) map[int]struct{} {
95+
out := map[int]struct{}{}
9696
for _, m := range validRefDefMatches(body) {
97-
out[m.bodyLine] = true
97+
out[m.bodyLine] = struct{}{}
9898
}
9999
return out
100100
}
@@ -169,7 +169,7 @@ func validRefDefMatches(body []byte) []validRefDefMatch {
169169
var out []validRefDefMatch
170170
for _, m := range index.RefDefRegexpMatches(body) {
171171
bodyLine := lineOfBodyOffset(body, m[2])
172-
if consumed[bodyLine] {
172+
if _, ok := consumed[bodyLine]; ok {
173173
continue
174174
}
175175
raw := body[m[2]:m[3]]
@@ -190,8 +190,8 @@ func validRefDefMatches(body []byte) []validRefDefMatch {
190190
// is by definition not a def. The Document root and
191191
// LinkReferenceDefinition nodes are skipped: the former spans the
192192
// whole buffer, the latter IS the line a real def lives on.
193-
func contentBlockLines(root ast.Node, body []byte) map[int]bool {
194-
out := map[int]bool{}
193+
func contentBlockLines(root ast.Node, body []byte) map[int]struct{} {
194+
out := map[int]struct{}{}
195195
_ = ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
196196
if !entering {
197197
return ast.WalkContinue, nil
@@ -206,7 +206,7 @@ func contentBlockLines(root ast.Node, body []byte) map[int]bool {
206206
ls := n.Lines()
207207
for i := 0; i < ls.Len(); i++ {
208208
seg := ls.At(i)
209-
out[lineOfBodyOffset(body, seg.Start)] = true
209+
out[lineOfBodyOffset(body, seg.Start)] = struct{}{}
210210
}
211211
return ast.WalkContinue, nil
212212
})

internal/rename/rename_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ func TestLinkRef_WithFrontMatterLineOffset(t *testing.T) {
8888

8989
func TestValidRefDefBodyLines(t *testing.T) {
9090
body := []byte("para\n\n[a]: u\n\n```\n[b]: v\n```\n")
91+
// ValidRefDefBodyLines returns map[int]struct{} — presence means valid def.
9192
got := ValidRefDefBodyLines(body)
92-
assert.True(t, got[3], "real def on body line 3")
93-
assert.False(t, got[6], "fenced def-shaped line is not a def")
93+
_, ok3 := got[3]
94+
assert.True(t, ok3, "real def on body line 3")
95+
_, ok6 := got[6]
96+
assert.False(t, ok6, "fenced def-shaped line is not a def")
9497
}
9598

9699
func TestBodyAndFMOffset(t *testing.T) {

internal/rules/concisenessscoring/rule.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,15 @@ func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
9797
}
9898

9999
line := astutil.ParagraphLine(para, f)
100-
message := fmt.Sprintf(
101-
"conciseness score too low (%.2f < %.2f); target >= %.2f",
102-
result.Conciseness, r.MinScore, r.MinScore,
103-
)
104100
examples := formatExamples(result.Cues)
101+
var cuesSuffix string
105102
if examples != "" {
106-
message += fmt.Sprintf(
107-
"; reduce verbose cues (e.g., %s)",
108-
examples,
109-
)
103+
cuesSuffix = "; reduce verbose cues (e.g., " + examples + ")"
110104
}
105+
message := fmt.Sprintf(
106+
"conciseness score too low (%.2f < %.2f); target >= %.2f%s",
107+
result.Conciseness, r.MinScore, r.MinScore, cuesSuffix,
108+
)
111109

112110
diags = append(diags, lint.Diagnostic{
113111
File: f.Path,

internal/rules/concisenessscoring/rule_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,53 @@ func TestNewScorer_Success(t *testing.T) {
296296
require.NoError(t, err)
297297
assert.NotNil(t, s)
298298
}
299+
300+
func TestCheck_MessageNoConcatenationWhenExamplesPresent(t *testing.T) {
301+
// When verbose cues are found, the diagnostic message must include
302+
// the cue examples in a single fmt.Sprintf rather than via `message +=`
303+
// concatenation. This test verifies the combined message format so the
304+
// implementation cannot silently drop the cue text.
305+
src := []byte(verboseParagraph() + "\n")
306+
f, err := lint.NewFile("test.md", src)
307+
require.NoError(t, err)
308+
309+
threshold := modelConciseness(t)
310+
r := &Rule{MinScore: threshold, MinWords: 20}
311+
diags := r.Check(f)
312+
if len(diags) == 0 {
313+
t.Skip("model threshold did not trigger on fixture")
314+
}
315+
msg := diags[0].Message
316+
// The message must contain both the score summary and the cue guidance.
317+
assert.Contains(t, msg, "conciseness score too low")
318+
assert.Contains(t, msg, "target >=")
319+
// verboseParagraph always triggers cue detection; both must be present.
320+
assert.Contains(t, msg, "reduce verbose cues")
321+
assert.Contains(t, msg, "e.g.,", "message with cues must include formatted examples")
322+
}
323+
324+
func TestCheck_NoCuesMessage(t *testing.T) {
325+
// Exercises the if examples == "" branch: a paragraph that scores as
326+
// verbose but produces no cue phrases yields the base message only.
327+
noCuePara := "When the configuration is set, the setting is used by the " +
328+
"configuration. The configuration uses the setting and the setting " +
329+
"configures the configuration."
330+
s, err := NewScorer()
331+
require.NoError(t, err)
332+
scored := s.Score(noCuePara)
333+
if len(scored.Cues) > 0 {
334+
t.Skip("scorer detected cues in fixture; model may have drifted")
335+
}
336+
337+
src := []byte(noCuePara + "\n")
338+
f, err := lint.NewFile("test.md", src)
339+
require.NoError(t, err)
340+
341+
r := &Rule{MinScore: scored.Conciseness + 0.10, MinWords: 1}
342+
diags := r.Check(f)
343+
require.Len(t, diags, 1, "expected diagnostic for low-scoring no-cue paragraph")
344+
msg := diags[0].Message
345+
assert.Contains(t, msg, "conciseness score too low")
346+
assert.Contains(t, msg, "target >=")
347+
assert.NotContains(t, msg, "reduce verbose cues", "no cues detected, so no cue guidance")
348+
}

internal/secreview/render.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"sort"
10+
"strconv"
1011
"time"
1112
)
1213

@@ -112,9 +113,9 @@ func locStr(loc *Location) string {
112113
s = "?"
113114
}
114115
if loc.StartLine != 0 {
115-
s += fmt.Sprintf(":%d", loc.StartLine)
116+
s += ":" + strconv.Itoa(loc.StartLine)
116117
if loc.EndLine != 0 && loc.EndLine != loc.StartLine {
117-
s += fmt.Sprintf("-%d", loc.EndLine)
118+
s += "-" + strconv.Itoa(loc.EndLine)
118119
}
119120
}
120121
return s

0 commit comments

Comments
 (0)