Skip to content

Commit 6d89652

Browse files
committed
perf(config): replace chain := []string{} with var; add workspace test
Third code-review pass found three issues: 1. validate.go and kind_extends.go: `chain := []string{}` allocates an empty backing array on every call. `var chain []string` (nil slice) defers the first allocation to the initial append, saving one heap allocation on every call where the loop never appends (kinds without an extends: chain in validateKindExtends, or when the extends chain has no schemas in extendsChainSchemas). 2. provenance.go: `allRuleNames` was the last `map[string]bool` presence set in the file not converted by the previous commit. Changed to `map[string]struct{}` for consistency with the surrounding code. 3. workspace_test.go: Add TestMemFSDirEntriesIgnoresEmptySegment to document and pin the empty-name-segment guard in memFS.dirEntries. A key with a double-slash segment (e.g. "a//b.md") produces an empty first component after stripping the directory prefix; the nested-if guard introduced in 9b0fa79 must check `name != ""` before the seen-map probe. The test constructs memFS directly (bypassing NewMemWorkspace path.Clean) to exercise this path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QjbjBieiWMBKURdKVNnPvy
1 parent ae1338f commit 6d89652

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

internal/config/kind_extends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func extendsChainSchemas(
101101
kinds map[string]KindBody, name string,
102102
) ([]schemaChainEntry, error) {
103103
visited := make(map[string]struct{})
104-
chain := []string{}
104+
var chain []string
105105
current := name
106106
for current != "" {
107107
if _, ok := visited[current]; ok {

internal/config/provenance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ func splitRulesByExplicit(cfg *Config) (defaults, user map[string]RuleCfg) {
354354
}
355355

356356
func allRuleNames(layers []layerInfo) []string {
357-
seen := map[string]bool{}
357+
seen := make(map[string]struct{})
358358
for _, l := range layers {
359359
for name := range l.Rules {
360-
seen[name] = true
360+
seen[name] = struct{}{}
361361
}
362362
}
363363
names := make([]string, 0, len(seen))

internal/config/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func ValidateKinds(cfg *Config) error {
7676
// deterministic across runs.
7777
func validateKindExtends(kinds map[string]KindBody, name string) error {
7878
visited := make(map[string]struct{})
79-
chain := []string{}
79+
var chain []string
8080
current := name
8181
for current != "" {
8282
if _, ok := visited[current]; ok {

pkg/mdsmith/workspace_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,34 @@ func TestMemFileInfo_Mode(t *testing.T) {
290290
}
291291
}
292292

293+
// TestMemFSDirEntriesIgnoresEmptySegment verifies that a key whose first
294+
// segment after the directory prefix is empty (produced by a double-slash
295+
// in the raw key, bypassing NewMemWorkspace cleanup) does not produce an
296+
// empty-name directory entry. The guard `if name != ""` in dirEntries must
297+
// be checked before the seen-map probe to prevent probing with key "".
298+
func TestMemFSDirEntriesIgnoresEmptySegment(t *testing.T) {
299+
// Construct memFS directly to bypass NewMemWorkspace path.Clean so the
300+
// double-slash key is preserved; "a//b.md" after stripping prefix "a/"
301+
// leaves "/b.md", whose first segment is "".
302+
m := memFS{
303+
"a//b.md": []byte("b"),
304+
"a/c.md": []byte("c"),
305+
}
306+
ents := m.dirEntries("a")
307+
for _, e := range ents {
308+
if e.Name() == "" {
309+
t.Fatalf("dirEntries emitted an entry with empty name: %#v", e)
310+
}
311+
}
312+
if len(ents) != 1 || ents[0].Name() != "c.md" {
313+
names := make([]string, len(ents))
314+
for i, e := range ents {
315+
names[i] = e.Name()
316+
}
317+
t.Fatalf("dirEntries(\"a\") with double-slash key = %v, want [c.md]", names)
318+
}
319+
}
320+
293321
// --- indexSlash ---
294322

295323
// TestIndexSlash covers the four boundary cases: no slash, slash at

0 commit comments

Comments
 (0)