Skip to content

Commit d05657c

Browse files
committed
optimize go generate
Do not repeatedly parse the same yang files. Saves about 7 seconds for me.
1 parent 9121533 commit d05657c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

gen/generator.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func parseAttribute(e *yang.Entry, attr *YamlConfigAttribute) {
437437
}
438438
}
439439

440-
func augmentConfig(config *YamlConfig, modelPaths []string) {
440+
func augmentConfig(config *YamlConfig, yangModules *yang.Modules) {
441441
path := ""
442442
if config.AugmentPath != "" {
443443
path = config.AugmentPath
@@ -446,7 +446,7 @@ func augmentConfig(config *YamlConfig, modelPaths []string) {
446446
}
447447

448448
module := strings.Split(path, ":")[0]
449-
e, errors := yang.GetModule(module, modelPaths...)
449+
e, errors := yangModules.GetModule(module)
450450
if len(errors) > 0 {
451451
fmt.Printf("YANG parser error(s): %+v\n\n", errors)
452452
return
@@ -548,19 +548,25 @@ func main() {
548548
}
549549

550550
items, _ = os.ReadDir(modelsPath)
551-
modelPaths := make([]string, 0)
551+
552+
yangModules := yang.NewModules()
552553

553554
// Iterate over yang models
554555
for _, item := range items {
555-
if filepath.Ext(item.Name()) == ".yang" {
556-
modelPaths = append(modelPaths, filepath.Join(modelsPath, item.Name()))
556+
if filepath.Ext(item.Name()) != ".yang" {
557+
continue
558+
}
559+
560+
fn := filepath.Join(modelsPath, item.Name())
561+
if err := yangModules.Read(fn); err != nil {
562+
log.Fatalf("yang parser: %v", err)
557563
}
558564
}
559565

560566
for i := range configs {
561567
// Augment config by yang models
562568
if !configs[i].NoAugmentConfig {
563-
augmentConfig(&configs[i], modelPaths)
569+
augmentConfig(&configs[i], yangModules)
564570
}
565571

566572
fmt.Printf("Augumented %d/%d: %v\n", i+1, len(configs), configs[i].Name)

0 commit comments

Comments
 (0)