1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 12:41:13 +01:00

Fix a perf regression; update benchmarks

This commit is contained in:
Oliver Steele 2017-07-09 13:12:30 -04:00
parent b96f177f2a
commit 95e7ad8a7b
5 changed files with 61 additions and 49 deletions

View File

@ -80,12 +80,10 @@ These will probably not change.
| Executable | Options | Time |
|------------|-----------------------------|---------------|
| jekyll | | 18.53s |
| gojekyll | single-threaded; cold cache | 3.31s ± 0.07s |
| gojekyll | single-threaded; warm cache | 2.50s ± 0.04s |
| gojekyll | multi-threaded | 1.51s ± 0.01s |
| gojekyll | multi-threaded | 0.80s ± 0.09s |
This isn't an apples-to-ranges comparison. Gojekyll doesn't use all the plugins that Jekyll does. In particular, `jekyll-mentions` parses each page's HTML. This could slow Gojekyll down once it's added.
| gojekyll | single-threaded; cold cache | 2.96s ± 0.09s |
| gojekyll | single-threaded; warm cache | 2.51s ± 0.04s |
| gojekyll | multi-threaded; cold cache | 1.37s ± 0.03s |
| gojekyll | multi-threaded; warm cache | 0.80s ± 0.06s |
There's currently no way to disable the cache. It was disabled off by re-building the executable.

45
helpers/html.go Normal file
View File

@ -0,0 +1,45 @@
package helpers
import (
"bytes"
"io"
"golang.org/x/net/html"
)
// ApplyToHTMLText applies a filter only to the text within an HTML document.
func ApplyToHTMLText(doc []byte, fn func(string) string) []byte {
z := html.NewTokenizer(bytes.NewReader(doc))
buf := new(bytes.Buffer)
body := false
outer:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
if z.Err() == io.EOF {
break outer
}
panic(z.Err())
case html.StartTagToken, html.EndTagToken:
tn, _ := z.TagName()
if string(tn) == "body" {
body = tt == html.StartTagToken
}
case html.TextToken:
if body {
s := (string(z.Text()))
_, err := buf.WriteString(fn(s))
if err != nil {
panic(err)
}
continue outer
}
}
_, err := buf.Write(z.Raw())
if err != nil {
panic(err)
}
}
return buf.Bytes()
}

View File

@ -1,11 +1,7 @@
package helpers
import (
"bytes"
"io"
"regexp"
"golang.org/x/net/html"
)
// LeftPad left-pads s with spaces to n wide. It's an alternative to http://left-pad.io.
@ -60,40 +56,3 @@ func StringArrayToMap(strings []string) map[string]bool {
}
return stringMap
}
// ApplyToHTMLText applies a filter only to the text within an HTML document.
func ApplyToHTMLText(doc []byte, fn func(string) string) []byte {
z := html.NewTokenizer(bytes.NewReader(doc))
buf := new(bytes.Buffer)
body := false
outer:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
if z.Err() == io.EOF {
break outer
}
panic(z.Err())
case html.StartTagToken, html.EndTagToken:
tn, _ := z.TagName()
if string(tn) == "body" {
body = tt == html.StartTagToken
}
case html.TextToken:
if body {
s := (string(z.Text()))
_, err := buf.WriteString((fn(s)))
if err != nil {
panic(err)
}
continue outer
}
}
_, err := buf.Write(z.Raw())
if err != nil {
panic(err)
}
}
return buf.Bytes()
}

View File

@ -42,7 +42,8 @@ func (s *Site) WriteDocument(w io.Writer, p pages.Document) error {
if err != nil {
return err
}
return p.Write(w, s)
_, err = w.Write(c)
return err
}
// WritePages writes output files.

View File

@ -9,6 +9,15 @@ import (
"path/filepath"
)
var disableCache = false
func init() {
s := os.Getenv("GOJEKYLL_DISABLE_CACHE")
if s != "" && s != "0" && s != "false" {
disableCache = true
}
}
// withFileCache looks (header, content) up in a user-specific file cache.
// If found, it writes the file contents. Else it calls fn to write to
// both the writer and the file system.
@ -32,7 +41,7 @@ func withFileCache(header string, content string, fn func() (string, error)) (st
// WriteFile truncates the file before writing it, so ignore empty files.
// If the writer actually wrote an empty file, we'll end up gratuitously
// re-running it, which is okay.
if b, err := ioutil.ReadFile(cachefile); err == nil && len(b) > 0 {
if b, err := ioutil.ReadFile(cachefile); err == nil && len(b) > 0 && !disableCache {
return string(b), err
}
s, err := fn()