main.go: fix handling for empty list (#3861)

* main.go: fix handling for empty list

The length of plMap and finalMap are not equal in this case.

* main.go: avoid resolving empty lists repeatedly

When there's an empty list and included by n lists,
the empty list would be resolved (n+1) times.
Add a bool key to ParsedList to avoid this.

ParsedList.Name is not used, so remove it.
This commit is contained in:
MkQtS authored and GitHub committed 2026-07-21 16:54:40 +08:00
1 parent bcc30e72bc
commit 462dc5706f
1 file changed
+11 -10
+11 -10
View File
@@ -38,7 +38,7 @@ type Inclusion struct {
} }
type ParsedList struct { type ParsedList struct {
Name string Resolved bool
Inclusions []*Inclusion Inclusions []*Inclusion
Entries []*Entry Entries []*Entry
} }
@@ -338,7 +338,7 @@ func validateSiteName(name string) bool {
func (p *Processor) getOrCreateParsedList(name string) *ParsedList { func (p *Processor) getOrCreateParsedList(name string) *ParsedList {
pl, exist := p.plMap[name] pl, exist := p.plMap[name]
if !exist { if !exist {
pl = &ParsedList{Name: name} pl = &ParsedList{Resolved: false}
p.plMap[name] = pl p.plMap[name] = pl
} }
return pl return pl
@@ -458,13 +458,13 @@ func polishList(roughMap map[string]*Entry) []*Entry {
} }
func (p *Processor) resolveList(plname string) error { func (p *Processor) resolveList(plname string) error {
if _, pldone := p.finalMap[plname]; pldone { pl, ok := p.plMap[plname]
return nil if !ok {
}
pl, plexist := p.plMap[plname]
if !plexist {
return fmt.Errorf("list %q not found", plname) return fmt.Errorf("list %q not found", plname)
} }
if pl.Resolved {
return nil
}
if p.cirIncMap[plname] { if p.cirIncMap[plname] {
return fmt.Errorf("circular inclusion in: %q", plname) return fmt.Errorf("circular inclusion in: %q", plname)
} }
@@ -487,10 +487,11 @@ func (p *Processor) resolveList(plname string) error {
} }
} }
if len(roughMap) == 0 { if len(roughMap) == 0 {
fmt.Printf("[Warn] ignore empty list %q", plname) fmt.Printf("[Warn] ignore empty list %q\n", plname)
} else { } else {
p.finalMap[plname] = polishList(roughMap) p.finalMap[plname] = polishList(roughMap)
} }
pl.Resolved = true
return nil return nil
} }
@@ -516,8 +517,7 @@ func run() error {
return fmt.Errorf("failed to loadData: %w", err) return fmt.Errorf("failed to loadData: %w", err)
} }
// Generate finalMap // Generate finalMap
sitesCount := len(processor.plMap) processor.finalMap = make(map[string][]*Entry, len(processor.plMap))
processor.finalMap = make(map[string][]*Entry, sitesCount)
processor.cirIncMap = make(map[string]bool) processor.cirIncMap = make(map[string]bool)
for plname := range processor.plMap { for plname := range processor.plMap {
if err := processor.resolveList(plname); err != nil { if err := processor.resolveList(plname); err != nil {
@@ -547,6 +547,7 @@ func run() error {
} }
// Generate proto sites // Generate proto sites
sitesCount := len(processor.finalMap)
gs := &GeoSites{ gs := &GeoSites{
Sites: make([]*router.GeoSite, 0, sitesCount), Sites: make([]*router.GeoSite, 0, sitesCount),
SiteIdx: make(map[string]int, sitesCount), SiteIdx: make(map[string]int, sitesCount),