Skip to content

Commit

Permalink
Replace to gopkg.in/yaml with github.com/goccy/go-yaml (note)
Browse files Browse the repository at this point in the history
Out of the box it's significantly slower than what we had:

```
pkg: github.com/gohugoio/hugo/parser/metadecoders
cpu: Apple M1 Pro
                   │ master.bench │        fix-goyaml-8822.bench         │
                   │    sec/op    │    sec/op      vs base               │
DecodeYAMLToMap-10   6.758µ ± 20%   10.505µ ± 25%  +55.43% (p=0.000 n=8)

                   │ master.bench │        fix-goyaml-8822.bench         │
                   │     B/op     │     B/op       vs base               │
DecodeYAMLToMap-10   8.461Ki ± 0%   11.021Ki ± 0%  +30.26% (p=0.000 n=8)

                   │ master.bench │       fix-goyaml-8822.bench        │
                   │  allocs/op   │ allocs/op   vs base                │
DecodeYAMLToMap-10     108.0 ± 0%   281.0 ± 0%  +160.19% (p=0.000 n=8)
```

Fixes #8822
  • Loading branch information
bep committed Nov 13, 2024
1 parent 8aba6dc commit 016d927
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 55 deletions.
2 changes: 1 addition & 1 deletion commands/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/bep/simplecobra"
"github.com/goccy/go-yaml"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/docshelper"
"github.com/gohugoio/hugo/helpers"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/gohugoio/hugo/parser"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"gopkg.in/yaml.v2"
)

func newGenCommand() *genCommand {
Expand Down
2 changes: 1 addition & 1 deletion create/skeletons/skeletons.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func newSiteCreateConfig(fs afero.Fs, createpath string, format string) (err err

func newSiteCreateArchetype(fs afero.Fs, createpath string, format string) (err error) {
in := map[string]any{
"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
"title": "{{ replace .File.ContentBaseName `-` ` ` | title }}",
"date": "{{ .Date }}",
"draft": true,
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/gobuffalo/flect v1.0.3
github.com/gobwas/glob v0.2.3
github.com/goccy/go-yaml v1.14.0
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
github.com/gohugoio/hashstructure v0.1.0
github.com/gohugoio/httpcache v0.7.0
Expand Down Expand Up @@ -82,7 +83,6 @@ require (
golang.org/x/text v0.19.0
golang.org/x/tools v0.26.0
google.golang.org/api v0.191.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down Expand Up @@ -164,6 +164,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240812133136-8ffd90a71988 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
software.sslmate.com/src/go-pkcs12 v0.2.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ github.com/bep/goat v0.5.0 h1:S8jLXHCVy/EHIoCY+btKkmcxcXFd34a0Q63/0D4TKeA=
github.com/bep/goat v0.5.0/go.mod h1:Md9x7gRxiWKs85yHlVTvHQw9rg86Bm+Y4SuYE8CTH7c=
github.com/bep/godartsass v1.2.0 h1:E2VvQrxAHAFwbjyOIExAMmogTItSKodoKuijNrGm5yU=
github.com/bep/godartsass v1.2.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8=
github.com/bep/godartsass/v2 v2.1.0 h1:fq5Y1xYf4diu4tXABiekZUCA+5l/dmNjGKCeQwdy+s0=
github.com/bep/godartsass/v2 v2.1.0/go.mod h1:AcP8QgC+OwOXEq6im0WgDRYK7scDsmZCEW62o1prQLo=
github.com/bep/godartsass/v2 v2.2.0 h1:3hO9Dt4BOnxkKmRxc+OpoKVFrDvBycpSCXEdElVAMVI=
github.com/bep/godartsass/v2 v2.2.0/go.mod h1:AcP8QgC+OwOXEq6im0WgDRYK7scDsmZCEW62o1prQLo=
github.com/bep/golibsass v1.2.0 h1:nyZUkKP/0psr8nT6GR2cnmt99xS93Ji82ZD9AgOK6VI=
Expand All @@ -143,10 +141,6 @@ github.com/bep/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o=
github.com/bep/helpers v0.5.0/go.mod h1:dSqCzIvHbzsk5YOesp1M7sKAq5xUcvANsRoKdawxH4Q=
github.com/bep/imagemeta v0.8.1 h1:tjZLPRftjxU7PTI87o5e5WKOFQ4S9S0engiP1OTpJTI=
github.com/bep/imagemeta v0.8.1/go.mod h1:5piPAq5Qomh07m/dPPCLN3mDJyFusvUG7VwdRD/vX0s=
github.com/bep/lazycache v0.4.0 h1:X8yVyWNVupPd4e1jV7efi3zb7ZV/qcjKQgIQ5aPbkYI=
github.com/bep/lazycache v0.4.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
github.com/bep/lazycache v0.6.0 h1:0vCgFo7TBtMQpSx64jnH1sagmw0ZougIFRpsqPHTa5U=
github.com/bep/lazycache v0.6.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
github.com/bep/lazycache v0.7.0 h1:VM257SkkjcR9z55eslXTkUIX8QMNKoqQRNKV/4xIkCY=
github.com/bep/lazycache v0.7.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
github.com/bep/logg v0.4.0 h1:luAo5mO4ZkhA5M1iDVDqDqnBBnlHjmtZF6VAyTp+nCQ=
Expand Down Expand Up @@ -233,6 +227,8 @@ github.com/gobuffalo/flect v1.0.3 h1:xeWBM2nui+qnVvNM4S3foBhCAL2XgPU+a7FdpelbTq4
github.com/gobuffalo/flect v1.0.3/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-yaml v1.14.0 h1:G/NDXJvf1CX0FshjxKn2AOL0MnrxsSJNpY9FpvMRblw=
github.com/goccy/go-yaml v1.14.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e h1:QArsSubW7eDh8APMXkByjQWvuljwPGAGQpJEFn0F0wY=
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e/go.mod h1:3Ltoo9Banwq0gOtcOwxuHG6omk+AwsQPADyw2vQYOJQ=
github.com/gohugoio/hashstructure v0.1.0 h1:kBSTMLMyTXbrJVAxaKI+wv30MMJJxn9Q8kfQtJaZ400=
Expand Down
4 changes: 2 additions & 2 deletions hugolib/frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Strings: {{ printf "%T" .Params.strings }} {{ range .Params.strings }}Strings: {

b.Build()

b.AssertFileContent("public/post/one/index.html", "Ints: []interface {} Int: 1 (int)|Int: 2 (int)|Int: 3 (int)|")
b.AssertFileContent("public/post/one/index.html", "Mixed: []interface {} Mixed: 1 (string)|Mixed: 2 (int)|Mixed: 3 (int)|")
b.AssertFileContent("public/post/one/index.html", "Ints: []interface {} Int: 1 (uint64)|Int: 2 (uint64)|Int: 3 (uint64)|")
b.AssertFileContent("public/post/one/index.html", "Mixed: []interface {} Mixed: 1 (string)|Mixed: 2 (uint64)|Mixed: 3 (uint64)|")
b.AssertFileContent("public/post/one/index.html", "Strings: []string Strings: 1 (string)|Strings: 2 (string)|Strings: 3 (string)|")
}
2 changes: 1 addition & 1 deletion langs/i18n/translationProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

"github.com/gohugoio/hugo/common/paths"

yaml "github.com/goccy/go-yaml"
"github.com/gohugoio/hugo/common/herrors"
"golang.org/x/text/language"
yaml "gopkg.in/yaml.v2"

"github.com/gohugoio/go-i18n/v2/i18n"
"github.com/gohugoio/hugo/helpers"
Expand Down
2 changes: 1 addition & 1 deletion parser/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

toml "github.com/pelletier/go-toml/v2"

yaml "gopkg.in/yaml.v2"
yaml "github.com/goccy/go-yaml"

xml "github.com/clbanning/mxj/v2"
)
Expand Down
32 changes: 2 additions & 30 deletions parser/metadecoders/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/niklasfasching/go-org/org"

xml "github.com/clbanning/mxj/v2"
yaml "github.com/goccy/go-yaml"
toml "github.com/pelletier/go-toml/v2"
"github.com/spf13/afero"
"github.com/spf13/cast"
yaml "gopkg.in/yaml.v2"
)

// Decoder provides some configuration options for the decoders.
Expand Down Expand Up @@ -164,35 +164,7 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error {
case TOML:
err = toml.Unmarshal(data, v)
case YAML:
err = yaml.Unmarshal(data, v)
if err != nil {
return toFileError(f, data, fmt.Errorf("failed to unmarshal YAML: %w", err))
}

// To support boolean keys, the YAML package unmarshals maps to
// map[interface{}]interface{}. Here we recurse through the result
// and change all maps to map[string]interface{} like we would've
// gotten from `json`.
var ptr any
switch vv := v.(type) {
case *map[string]any:
ptr = *vv
case *any:
ptr = *vv
default:
// Not a map.
}

if ptr != nil {
if mm, changed := stringifyMapKeys(ptr); changed {
switch vv := v.(type) {
case *map[string]any:
*vv = mm.(map[string]any)
case *any:
*vv = mm
}
}
}
return yaml.Unmarshal(data, v)
case CSV:
return d.unmarshalCSV(data, v)

Expand Down
8 changes: 4 additions & 4 deletions parser/metadecoders/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func TestUnmarshalToMap(t *testing.T) {
{`a = "b"`, TOML, expect},
{`a: "b"`, YAML, expect},
// Make sure we get all string keys, even for YAML
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
{"a:\n true: 1\n false: 2", YAML, map[string]any{"a": map[string]any{"true": 1, "false": 2}}},
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": uint64(2), "d": []any{uint64(3), uint64(4)}}}},
{"a:\n true: 1\n false: 2", YAML, map[string]any{"a": map[string]any{"true": uint64(1), "false": uint64(2)}}},
{`{ "a": "b" }`, JSON, expect},
{`<root><a>b</a></root>`, XML, expect},
{`#+a: b`, ORG, expect},
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestUnmarshalToInterface(t *testing.T) {
{[]byte(`a: "b"`), YAML, expect},
{[]byte(`<root><a>b</a></root>`), XML, expect},
{[]byte(`a,b,c`), CSV, [][]string{{"a", "b", "c"}}},
{[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
{[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": uint64(2), "d": []any{uint64(3), uint64(4)}}}},
// errors
{[]byte(`a = "`), TOML, false},
} {
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestUnmarshalStringTo(t *testing.T) {
{"32", int64(1234), int64(32)},
{"32", int(1234), int(32)},
{"3.14159", float64(1), float64(3.14159)},
{"[3,7,9]", []any{}, []any{3, 7, 9}},
{"[3,7,9]", []any{}, []any{uint64(3), uint64(7), uint64(9)}},
{"[3.1,7.2,9.3]", []any{}, []any{3.1, 7.2, 9.3}},
} {
msg := qt.Commentf("%d: %T", i, test.to)
Expand Down
12 changes: 8 additions & 4 deletions testscripts/commands/new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ hugo new site toml-site --format toml
hugo new site yaml-site --format yaml
[!windows] cmp yaml-site/archetypes/default.md archetype-golden-yaml.md

cd yaml-site
hugo new foo-bar.md
grep 'title: "Foo Bar"' content/foo-bar.md

-- myexistingsite/hugo.toml --
theme = "mytheme"
-- myexistingsite/content/p1.md --
Expand All @@ -99,17 +103,17 @@ Dummy content.
{
"date": "{{ .Date }}",
"draft": true,
"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}"
"title": "{{ replace .File.ContentBaseName `-` ` ` | title }}"
}
-- archetype-golden-toml.md --
+++
date = '{{ .Date }}'
draft = true
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
+++
-- archetype-golden-yaml.md --
---
date: '{{ .Date }}'
date: "{{ .Date }}"
draft: true
title: '{{ replace .File.ContentBaseName "-" " " | title }}'
title: "{{ replace .File.ContentBaseName `-` ` ` | title }}"
---
8 changes: 4 additions & 4 deletions tpl/transform/remarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func TestRemarshal(t *testing.T) {
yamlExample := `resources:
- params:
byline: picasso
src: '**image-4.png'
src: "**image-4.png"
title: The Fourth Image!
- name: my-cool-image-:counter
params:
byline: bep
src: '**.png'
title: 'TOML: The Image #:counter'
src: "**.png"
title: "TOML: The Image #:counter"
title: Test Metadata
`

Expand Down Expand Up @@ -126,7 +126,7 @@ title: Test Metadata
c.Assert(err, qt.IsNil, fromTo)
diff := htesting.DiffStrings(v1.data, converted)
if len(diff) > 0 {
t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
t.Fatalf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%#v", fromTo, v1.data, converted, diff)
}

}
Expand Down

0 comments on commit 016d927

Please sign in to comment.