Cache source directories during configuration normalization
This commit is contained in:
@@ -70,8 +70,8 @@ func initializeDocumentMainDirectory(resolution *configkit.Resolution, state *do
|
||||
}
|
||||
info, ok := resolution.EntryInfo(configkit.Keys("version"))
|
||||
if ok && len(info.Origin.Locations) == 1 {
|
||||
if source, sourceOK := resolution.Source(info.Origin.Locations[0].SourceID); sourceOK && source.Path != "" {
|
||||
state.mainDirectory = filepath.Dir(source.Path)
|
||||
if directory := state.sourceDirectory(info.Origin.Locations[0].SourceID, resolution.Source); directory != "" {
|
||||
state.mainDirectory = directory
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -166,14 +166,35 @@ func documentPathBase(resolution *configkit.Resolution, state *documentLoadState
|
||||
if len(info.Origin.Locations) != 1 {
|
||||
return "", false
|
||||
}
|
||||
source, ok := resolution.Source(info.Origin.Locations[0].SourceID)
|
||||
if !ok || source.Path == "" {
|
||||
directory := state.sourceDirectory(info.Origin.Locations[0].SourceID, resolution.Source)
|
||||
if directory == "" {
|
||||
if state != nil && state.mainDirectory != "" {
|
||||
return state.mainDirectory, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
return filepath.Dir(source.Path), true
|
||||
return directory, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Source evidence is fixed during resolution. Cache only the directory needed
|
||||
// by paths, avoiding repeated copies of unrelated import-selection evidence.
|
||||
func (state *documentLoadState) sourceDirectory(id string, lookup func(string) (configkit.SourceInfo, bool)) string {
|
||||
if state != nil {
|
||||
if directory, ok := state.sourceDirectories[id]; ok {
|
||||
return directory
|
||||
}
|
||||
}
|
||||
directory := ""
|
||||
if source, ok := lookup(id); ok && source.Path != "" {
|
||||
directory = filepath.Dir(source.Path)
|
||||
}
|
||||
if state != nil {
|
||||
if state.sourceDirectories == nil {
|
||||
state.sourceDirectories = make(map[string]string)
|
||||
}
|
||||
state.sourceDirectories[id] = directory
|
||||
}
|
||||
return directory
|
||||
}
|
||||
|
||||
@@ -8,6 +8,44 @@ import (
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestSourceDirectoryLookupIsMemoizedPerLoad(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
state := newDocumentLoadState(dir)
|
||||
calls := make(map[string]int)
|
||||
lookup := func(id string) (configkit.SourceInfo, bool) {
|
||||
calls[id]++
|
||||
if id == "missing" {
|
||||
return configkit.SourceInfo{}, false
|
||||
}
|
||||
if id == "memory" {
|
||||
return configkit.SourceInfo{}, true
|
||||
}
|
||||
return configkit.SourceInfo{Path: filepath.Join(dir, id, "config.yml")}, true
|
||||
}
|
||||
for range 2 {
|
||||
for _, id := range []string{"first", "second", "missing", "memory"} {
|
||||
want := ""
|
||||
if id == "first" || id == "second" {
|
||||
want = filepath.Join(dir, id)
|
||||
}
|
||||
if got := state.sourceDirectory(id, lookup); got != want {
|
||||
t.Fatalf("directory(%s) = %q, want %q", id, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
for id, count := range calls {
|
||||
if count != 1 {
|
||||
t.Fatalf("source %s queried %d times", id, count)
|
||||
}
|
||||
}
|
||||
other := newDocumentLoadState(dir)
|
||||
if got := other.sourceDirectory("first", func(string) (configkit.SourceInfo, bool) {
|
||||
return configkit.SourceInfo{Path: filepath.Join(dir, "changed", "config.yml")}, true
|
||||
}); got != filepath.Join(dir, "changed") {
|
||||
t.Fatalf("another load reused directory %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentPathsUseDeclaringFilesAcrossImports(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -8,11 +8,10 @@ import (
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// documentLoadState keeps the spelling supplied by the user while resolver
|
||||
// operations move map entries to their canonical keys. It is intentionally
|
||||
// load-local: source-location handling consumes it during a later resolver
|
||||
// pass and it never becomes runtime configuration state.
|
||||
// documentLoadState keeps source-directory lookups and original map-key
|
||||
// spellings local to one load. It never becomes runtime configuration state.
|
||||
type documentLoadState struct {
|
||||
sourceDirectories map[string]string
|
||||
originalPaths map[string]configkit.Path
|
||||
mainDirectory string
|
||||
invocationDirectory string
|
||||
|
||||
Reference in New Issue
Block a user