Skip to content

Commit

Permalink
Improve content map, memory cache and dependency resolution
Browse files Browse the repository at this point in the history
TODO(bep) improve commit message.

Hugo has always been a active user of in-memory caches, but before this commit we did nothing to control the memory usage.

One failing example would be loading lots of big JSON data files and unmarshal them via `transform.Unmarshal`.

This commit consolidates all these caches into one single LRU cache with an eviction strategy that also considers used vs. available memory.

Hugo will try to limit its memory usage to 1/4 or total system memory, but this can be controlled with the `HUGO_MEMORYLIMIT` environment variable (a float value representing Gigabytes).

A natural next step after this would be to use this cache for `.Content`.

Fixes gohugoio#10386
Fixes gohugoio#8307
Fixes gohugoio#8498
Fixes gohugoio#8927
Fixes gohugoio#9192
Fixes gohugoio#9189
Fixes gohugoio#7425
Fixes gohugoio#7437
Fixes gohugoio#7436
Fixes gohugoio#7882
Updates gohugoio#7544
Fixes gohugoio#9224
Fixes gohugoio#9324
Fixes gohugoio#9352
Fixes gohugoio#9343
Fixes gohugoio#9171
Fixes gohugoio#10104
Fixes gohugoio#10380
  • Loading branch information
bep committed Nov 6, 2022
1 parent 1fd3320 commit 14bd813
Show file tree
Hide file tree
Showing 254 changed files with 13,830 additions and 11,159 deletions.
Empty file added .hugo_build.lock
Empty file.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"autoHide.autoHidePanel": false
}
37 changes: 0 additions & 37 deletions bench.sh

This file was deleted.

12 changes: 0 additions & 12 deletions benchSite.sh

This file was deleted.

2 changes: 1 addition & 1 deletion benchbep.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gobench -package=./hugolib -bench="BenchmarkSiteNew/Deep_content_tree"
gobench --package ./hugolib --bench "BenchmarkSiteNew/Regular_Deep" -base v0.89.4
1 change: 0 additions & 1 deletion bepdock.sh

This file was deleted.

3 changes: 2 additions & 1 deletion cache/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package filecache

import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
Expand Down Expand Up @@ -163,7 +164,7 @@ func (c *Cache) ReadOrCreate(id string,
// GetOrCreate tries to get the file with the given id from cache. If not found or expired, create will
// be invoked and the result cached.
// This method is protected by a named lock using the given id as identifier.
func (c *Cache) GetOrCreate(id string, create func() (io.ReadCloser, error)) (ItemInfo, io.ReadCloser, error) {
func (c *Cache) GetOrCreate(ctx context.Context, id string, create func() (io.ReadCloser, error)) (ItemInfo, io.ReadCloser, error) {
id = cleanID(id)

c.nlocker.Lock(id)
Expand Down
7 changes: 4 additions & 3 deletions cache/filecache/filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package filecache

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -134,7 +135,7 @@ dir = ":cacheDir/c"

for _, ca := range []*Cache{caches.ImageCache(), caches.AssetsCache(), caches.GetJSONCache(), caches.GetCSVCache()} {
for i := 0; i < 2; i++ {
info, r, err := ca.GetOrCreate("a", rf("abc"))
info, r, err := ca.GetOrCreate(context.TODO(), "a", rf("abc"))
c.Assert(err, qt.IsNil)
c.Assert(r, qt.Not(qt.IsNil))
c.Assert(info.Name, qt.Equals, "a")
Expand All @@ -152,7 +153,7 @@ dir = ":cacheDir/c"
c.Assert(err, qt.IsNil)
c.Assert(string(b), qt.Equals, "abc")

_, r, err = ca.GetOrCreate("a", rf("bcd"))
_, r, err = ca.GetOrCreate(context.TODO(), "a", rf("bcd"))
c.Assert(err, qt.IsNil)
b, _ = ioutil.ReadAll(r)
r.Close()
Expand Down Expand Up @@ -229,7 +230,7 @@ dir = "/cache/c"
ca := caches.Get(cacheName)
c.Assert(ca, qt.Not(qt.IsNil))
filename, data := filenameData(i)
_, r, err := ca.GetOrCreate(filename, func() (io.ReadCloser, error) {
_, r, err := ca.GetOrCreate(context.TODO(), filename, func() (io.ReadCloser, error) {
return hugio.ToReadCloser(strings.NewReader(data)), nil
})
c.Assert(err, qt.IsNil)
Expand Down
Loading

0 comments on commit 14bd813

Please sign in to comment.