Skip to content

Commit

Permalink
gateway/assets: process each template and ETag hash in parallel
Browse files Browse the repository at this point in the history
From:
```
init github.com/ipfs/boxo/gateway/assets @26 ms, 18 ms clock, 9887320 bytes, 1496 allocs
```
To:
```
init github.com/ipfs/boxo/gateway/assets @10 ms, 3.3 ms clock, 3368 bytes, 11 allocs
```

This is not optimal but better solutions (like lazy loading) require breaking the API and it's a better job for future someone else.
  • Loading branch information
Jorropo committed Dec 30, 2023
1 parent 483bc39 commit 2e320fd
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions gateway/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"strconv"
"strings"
"sync"

"github.com/cespare/xxhash/v2"
"github.com/ipfs/boxo/path"
Expand All @@ -26,11 +27,39 @@ var (
)

func init() {
initAssetsHash()
initTemplates()
tmpls := [...]struct {
result **template.Template
sourceFile string
}{
{&DirectoryTemplate, "directory.html"},
{&DagTemplate, "dag.html"},
{&ErrorTemplate, "error.html"},
}

var wg sync.WaitGroup
wg.Add(1 + len(tmpls))
go initAssetsHash(&wg)

for _, tmpl := range tmpls {
tmpl := tmpl
go func() {
defer wg.Done()
var err error
*tmpl.result, err = BuildTemplate(assets, tmpl.sourceFile)
if err != nil {
panic(err)
}
}()
}

// @Jorropo: this is still waiting because I was too lazy to break the API of this public package.
// It sounds better if we would use sync.Once and maybe start goroutines in init().
wg.Wait()
}

func initAssetsHash() {
func initAssetsHash(wg *sync.WaitGroup) {
defer wg.Done()

sum := xxhash.New()
err := fs.WalkDir(assets, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -56,28 +85,6 @@ func initAssetsHash() {
AssetHash = strconv.FormatUint(sum.Sum64(), 32)
}

func initTemplates() {
var err error

// Directory listing template
DirectoryTemplate, err = BuildTemplate(assets, "directory.html")
if err != nil {
panic(err)
}

// DAG Index template
DagTemplate, err = BuildTemplate(assets, "dag.html")
if err != nil {
panic(err)
}

// Error template
ErrorTemplate, err = BuildTemplate(assets, "error.html")
if err != nil {
panic(err)
}
}

type MenuItem struct {
URL string
Title string
Expand Down

0 comments on commit 2e320fd

Please sign in to comment.