From 9f1b891312e2c28f466d984b47ff8ce6d52e940d Mon Sep 17 00:00:00 2001 From: Anirudh Sudhir Date: Fri, 5 Apr 2024 22:01:11 +0530 Subject: [PATCH] perf: implement worker pool --- pkg/engine/user_engine.go | 63 +++++++++++-------- .../engine/render_user_defined/rendered/.html | 1 + 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/pkg/engine/user_engine.go b/pkg/engine/user_engine.go index 4415ce1..2c29625 100644 --- a/pkg/engine/user_engine.go +++ b/pkg/engine/user_engine.go @@ -4,18 +4,24 @@ import ( "bytes" "html/template" "os" - "runtime" "strings" - "sync" "github.com/acmpesuecc/anna/pkg/parser" ) +// TODO: Eliminate this struct type postsTemplateData struct { Posts []parser.TemplateData parser.TemplateData } +type Job struct { + FileInPath string + FileOutPath string + TemplateData parser.TemplateData + Templ *template.Template +} + func (e *Engine) RenderEngineGeneratedFiles(fileOutPath string, templ *template.Template) { // Rendering "posts.html" var postsBuffer bytes.Buffer @@ -41,37 +47,44 @@ func (e *Engine) RenderEngineGeneratedFiles(fileOutPath string, templ *template. } func (e *Engine) RenderUserDefinedPages(fileOutPath string, templ *template.Template) { - numCPU := runtime.NumCPU() numTemplates := len(e.Templates) - concurrency := numCPU * 2 // Adjust the concurrency factor based on system hardware resources - if numTemplates < concurrency { - concurrency = numTemplates - } + numJobs := numTemplates / 5 - templateURLs := make([]string, 0, numTemplates) - for templateURL := range e.Templates { - templateURLs = append(templateURLs, string(templateURL)) + jobs := make(chan Job, numTemplates) + results := make(chan struct{}, numTemplates) + + //Starting workers + for i := 0; i <= numJobs; i++ { + go e.RenderPageWorker(jobs, results) } - var wg sync.WaitGroup - semaphore := make(chan struct{}, concurrency) + // Sending jobs over a channel + for templateURL := range e.Templates { + templData := e.Templates[template.URL(templateURL)] + fileInPath := strings.TrimSuffix(string(templData.CompleteURL), ".html") - for _, templateURL := range templateURLs { - wg.Add(1) - semaphore <- struct{}{} + job := Job{ + FileInPath: fileInPath, + FileOutPath: fileOutPath, + TemplateData: templData, + Templ: templ, + } - go func(templateURL string) { - defer func() { - <-semaphore - wg.Done() - }() + jobs <- job + } + close(jobs) - templData := e.Templates[template.URL(templateURL)] - fileInPath := strings.TrimSuffix(string(templData.CompleteURL), ".html") - e.RenderPage(fileOutPath, template.URL(fileInPath), templData, templ, "page") - }(templateURL) + for i := 1; i <= numTemplates; i++ { + <-results } - wg.Wait() +} + +func (e *Engine) RenderPageWorker(jobs <-chan Job, results chan<- struct{}) { + for job := range jobs { + fileInPath := job.FileInPath + e.RenderPage(job.FileOutPath, template.URL(fileInPath), job.TemplateData, job.Templ, "page") + results <- struct{}{} + } } diff --git a/test/engine/render_user_defined/rendered/.html b/test/engine/render_user_defined/rendered/.html index c3dcf98..3dd0104 100644 --- a/test/engine/render_user_defined/rendered/.html +++ b/test/engine/render_user_defined/rendered/.html @@ -8,3 +8,4 @@

Index Page

+