From b832a38ca63a4bdc777291709baaad233e25e2a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Thu, 3 Mar 2022 12:57:21 +0100 Subject: [PATCH] hswg: parallelize rendering --- hswg/main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/hswg/main.go b/hswg/main.go index 9fe3fd7..65557c5 100644 --- a/hswg/main.go +++ b/hswg/main.go @@ -17,6 +17,7 @@ import ( "regexp" "sort" "strings" + "sync" "syscall" "time" @@ -121,7 +122,7 @@ type Entry struct { // Published returns the date when the entry was published, or nil if unknown. func (e *Entry) Published() *time.Time { - if d, _, err := e.Attributes.GetAsString("date"); err != nil { + if d, ok, err := e.Attributes.GetAsString("date"); !ok || err != nil { return nil } else if t, err := time.Parse(time.RFC3339, d); err == nil { return &t @@ -287,14 +288,25 @@ func writeIndex(path string, t *template.Template, func finalizeEntries(entries *map[string]*Entry, t *template.Template, indexPath string, indexT *template.Template) { + // The initial render of a large amount of entries is resource-intensive. + var wg sync.WaitGroup for name, e := range *entries { e.backlinks = map[string]bool{} - if e.raw == nil { + if e.raw != nil { + continue + } + + wg.Add(1) + go func(name string, e *Entry) { + defer wg.Done() if err := renderEntry(name, e); err != nil { log.Printf("%s: %s\n", name, err) } - } + }(name, e) } + + wg.Wait() + for name, e := range *entries { // Expand LinkWords anywhere between . // We want something like the inverse of Regexp.ReplaceAllStringFunc.