Skip to content

Commit

Permalink
perf: implement worker pool
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhsudhir committed Apr 5, 2024
1 parent c2576f4 commit 9f1b891
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
63 changes: 38 additions & 25 deletions pkg/engine/user_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}{}
}
}
1 change: 1 addition & 0 deletions test/engine/render_user_defined/rendered/.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
<h1>Index Page</h1>
</body>
</html>

0 comments on commit 9f1b891

Please sign in to comment.