Skip to content

Commit ef818c0

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 9b0fa79 commit ef818c0

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
@@ -266,6 +266,34 @@ func TestMemFSGlobDoublestar(t *testing.T) {
266266
}
267267
}
268268

269+
// TestMemFSDirEntriesIgnoresEmptySegment verifies that a key whose first
270+
// segment after the directory prefix is empty (produced by a double-slash
271+
// in the raw key, bypassing NewMemWorkspace cleanup) does not produce an
272+
// empty-name directory entry. The guard `if name != ""` in dirEntries must
273+
// be checked before the seen-map probe to prevent probing with key "".
274+
func TestMemFSDirEntriesIgnoresEmptySegment(t *testing.T) {
275+
// Construct memFS directly to bypass NewMemWorkspace path.Clean so the
276+
// double-slash key is preserved; "a//b.md" after stripping prefix "a/"
277+
// leaves "/b.md", whose first segment is "".
278+
m := memFS{
279+
"a//b.md": []byte("b"),
280+
"a/c.md": []byte("c"),
281+
}
282+
ents := m.dirEntries("a")
283+
for _, e := range ents {
284+
if e.Name() == "" {
285+
t.Fatalf("dirEntries emitted an entry with empty name: %#v", e)
286+
}
287+
}
288+
if len(ents) != 1 || ents[0].Name() != "c.md" {
289+
names := make([]string, len(ents))
290+
for i, e := range ents {
291+
names[i] = e.Name()
292+
}
293+
t.Fatalf("dirEntries(\"a\") with double-slash key = %v, want [c.md]", names)
294+
}
295+
}
296+
269297
// --- indexSlash ---
270298

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

0 commit comments

Comments
 (0)