Skip to content

Commit

Permalink
hugofs: Make FileMeta a struct
Browse files Browse the repository at this point in the history
This commit started out investigating a `concurrent map read write` issue, ending by replacing the map with a struct.

This is easier to reason about, and it's more effective:

```
name                                  old time/op    new time/op    delta
SiteNew/Regular_Deep_content_tree-16    71.5ms ± 3%    69.4ms ± 5%    ~     (p=0.200 n=4+4)

name                                  old alloc/op   new alloc/op   delta
SiteNew/Regular_Deep_content_tree-16    29.7MB ± 0%    27.9MB ± 0%  -5.82%  (p=0.029 n=4+4)

name                                  old allocs/op  new allocs/op  delta
SiteNew/Regular_Deep_content_tree-16      313k ± 0%      303k ± 0%  -3.35%  (p=0.029 n=4+4)
```

See #8749
  • Loading branch information
bep committed Jul 15, 2021
1 parent f27e542 commit 022c479
Show file tree
Hide file tree
Showing 44 changed files with 423 additions and 440 deletions.
4 changes: 2 additions & 2 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ func (c *commandeer) getDirList() ([]string, error) {
return filepath.SkipDir
}

filenames = append(filenames, fi.Meta().Filename())
filenames = append(filenames, fi.Meta().Filename)
}

return nil
Expand All @@ -706,7 +706,7 @@ func (c *commandeer) getDirList() ([]string, error) {
watchFiles := c.hugo().PathSpec.BaseFs.WatchDirs()
for _, fi := range watchFiles {
if !fi.IsDir() {
filenames = append(filenames, fi.Meta().Filename())
filenames = append(filenames, fi.Meta().Filename)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion commands/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newContentPathSection(h *hugolib.HugoSites, path string) (string, string) {

if h != nil {
for _, dir := range h.BaseFs.Content.Dirs {
createpath = strings.TrimPrefix(createpath, dir.Meta().Filename())
createpath = strings.TrimPrefix(createpath, dir.Meta().Filename)
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/hugo/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetExecEnviron(workDir string, cfg config.Provider, fs afero.Fs) []string {
if err == nil {
for _, fi := range fis {
key := fmt.Sprintf("HUGO_FILE_%s", strings.ReplaceAll(strings.ToUpper(fi.Name()), ".", "_"))
value := fi.(hugofs.FileMetaInfo).Meta().Filename()
value := fi.(hugofs.FileMetaInfo).Meta().Filename
config.SetEnvVars(&env, key, value)
}
}
Expand Down
14 changes: 7 additions & 7 deletions create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func NewContent(

func targetSite(sites *hugolib.HugoSites, fi hugofs.FileMetaInfo) *hugolib.Site {
for _, s := range sites.Sites {
if fi.Meta().Lang() == s.Language().Lang {
if fi.Meta().Lang == s.Language().Lang {
return s
}
}
Expand All @@ -137,7 +137,7 @@ func newContentFromDir(
cm archetypeMap, name, targetPath string) error {
for _, f := range cm.otherFiles {
meta := f.Meta()
filename := meta.Path()
filename := meta.Path
// Just copy the file to destination.
in, err := meta.Open()
if err != nil {
Expand Down Expand Up @@ -166,7 +166,7 @@ func newContentFromDir(
}

for _, f := range cm.contentFiles {
filename := f.Meta().Path()
filename := f.Meta().Path
s := targetSite(sites, f)
targetFilename := filepath.Join(targetPath, strings.TrimPrefix(filename, archetypeDir))

Expand Down Expand Up @@ -274,15 +274,15 @@ func resolveContentPath(sites *hugolib.HugoSites, fs afero.Fs, targetPath string

for _, dir := range sites.BaseFs.Content.Dirs {
meta := dir.Meta()
contentDir := meta.Filename()
contentDir := meta.Filename

if !strings.HasSuffix(contentDir, helpers.FilePathSeparator) {
contentDir += helpers.FilePathSeparator
}

if strings.HasPrefix(targetPath, contentDir) {
siteContentDir = contentDir
dirLang = meta.Lang()
dirLang = meta.Lang
break
}
}
Expand Down Expand Up @@ -317,8 +317,8 @@ func resolveContentPath(sites *hugolib.HugoSites, fs afero.Fs, targetPath string
} else {
var contentDir string
for _, dir := range sites.BaseFs.Content.Dirs {
contentDir = dir.Meta().Filename()
if dir.Meta().Lang() == s.Lang() {
contentDir = dir.Meta().Filename
if dir.Meta().Lang == s.Lang() {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func PrintFs(fs afero.Fs, path string, w io.Writer) {
var filename string
var meta interface{}
if fim, ok := info.(hugofs.FileMetaInfo); ok {
filename = fim.Meta().Filename()
filename = fim.Meta().Filename
meta = fim.Meta()
}
fmt.Fprintf(w, " %q %q\t\t%v\n", path, filename, meta)
Expand Down
12 changes: 7 additions & 5 deletions hugofs/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/spf13/afero"
)

func decorateDirs(fs afero.Fs, meta FileMeta) afero.Fs {
func decorateDirs(fs afero.Fs, meta *FileMeta) afero.Fs {
ffs := &baseFileDecoratorFs{Fs: fs}

decorator := func(fi os.FileInfo, name string) (os.FileInfo, error) {
Expand Down Expand Up @@ -82,9 +82,11 @@ func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero

decorator := func(fi os.FileInfo, filename string) (os.FileInfo, error) {
// Store away the original in case it's a symlink.
meta := FileMeta{metaKeyName: fi.Name()}
meta := NewFileMeta()
meta.Name = fi.Name()

if fi.IsDir() {
meta[metaKeyJoinStat] = func(name string) (FileMetaInfo, error) {
meta.JoinStatFunc = func(name string) (FileMetaInfo, error) {
joinedFilename := filepath.Join(filename, name)
fi, _, err := lstatIfPossible(fs, joinedFilename)
if err != nil {
Expand All @@ -102,15 +104,15 @@ func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero

isSymlink := isSymlink(fi)
if isSymlink {
meta[metaKeyOriginalFilename] = filename
meta.OriginalFilename = filename
var link string
var err error
link, fi, err = evalSymlinks(fs, filename)
if err != nil {
return nil, err
}
filename = link
meta[metaKeyIsSymlink] = true
meta.IsSymlink = true
}

opener := func() (afero.File, error) {
Expand Down
Loading

0 comments on commit 022c479

Please sign in to comment.