Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: compute labelMap when indexing #1872

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions resolve/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,35 @@ type CrossResolver interface {
// RuleIndex is a table of rules in a workspace, indexed by label and by
// import path. Used by Resolver to map import paths to labels.
type RuleIndex struct {
rules []*ruleRecord
labelMap map[label.Label]*ruleRecord
importMap map[ImportSpec][]*ruleRecord
mrslv func(r *rule.Rule, pkgRel string) Resolver
crossResolvers []CrossResolver

// The underlying state of rules. All indexing should be reproducible from this.
rules []*ruleRecord

// Rules indexed by label.
// Computed from `rules` when indexing.
labelMap map[label.Label]*ruleRecord

// Imports specs mapping to records producing those.
// Computed from `rules` when indexing.
importMap map[ImportSpec][]*ruleRecord

// Whether another rule of the same language embeds this rule.
// Embedded rules should not be indexed.
// Computed from `rules` when indexing.
embedded map[label.Label]struct{}

// The transitive closure of labels embedded within rules (as determined by the Embeds method).
// This only includes rules in the same language (i.e., it includes a go_library embedding
// a go_proto_library, but not a go_proto_library embedding a proto_library).
// Computed from `rules` when indexing.
embeds map[label.Label][]label.Label

// The transitive closure of all imports produced by each label.
// This includes transitive imports from embedded labels (as determined by
// the Embeds method). This may include imports of other languages.
// Computed from `rules` when indexing.
imports map[label.Label][]ImportSpec
}

Expand Down Expand Up @@ -126,7 +137,6 @@ func NewRuleIndex(mrslv func(r *rule.Rule, pkgRel string) Resolver, exts ...inte
}
}
return &RuleIndex{
labelMap: make(map[label.Label]*ruleRecord),
mrslv: mrslv,
crossResolvers: crossResolvers,
}
Expand Down Expand Up @@ -168,12 +178,7 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) {
embeds: embeds,
lang: lang,
}
if _, ok := ix.labelMap[record.label]; ok {
fmeum marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("multiple rules found with label %s", record.label)
return
}
ix.rules = append(ix.rules, record)
ix.labelMap[record.label] = record
}

// Finish constructs the import index and performs any other necessary indexing
Expand All @@ -183,8 +188,16 @@ func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) {
// Finish must be called after all AddRule calls and before any
// FindRulesByImport calls.
func (ix *RuleIndex) Finish() {
ix.labelMap = make(map[label.Label]*ruleRecord)
ix.imports = make(map[label.Label][]ImportSpec)

for _, r := range ix.rules {
if _, ok := ix.labelMap[r.label]; ok {
log.Printf("multiple rules found with label %s", r.label)
continue
}

ix.labelMap[r.label] = r
ix.imports[r.label] = r.importedAs
}

Expand Down