diff options
Diffstat (limited to 'vendor/github.com/tetratelabs/wazero/internal/filecache')
| -rw-r--r-- | vendor/github.com/tetratelabs/wazero/internal/filecache/compilationcache.go | 42 | ||||
| -rw-r--r-- | vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go | 76 | 
2 files changed, 118 insertions, 0 deletions
| diff --git a/vendor/github.com/tetratelabs/wazero/internal/filecache/compilationcache.go b/vendor/github.com/tetratelabs/wazero/internal/filecache/compilationcache.go new file mode 100644 index 000000000..b2dbd4650 --- /dev/null +++ b/vendor/github.com/tetratelabs/wazero/internal/filecache/compilationcache.go @@ -0,0 +1,42 @@ +package filecache + +import ( +	"crypto/sha256" +	"io" +) + +// Cache allows the compiler engine to skip compilation of wasm to machine code +// where doing so is redundant for the same wasm binary and version of wazero. +// +// This augments the default in-memory cache of compiled functions, by +// decoupling it from a wazero.Runtime instance. Concretely, a runtime loses +// its cache once closed. This cache allows the runtime to rebuild its +// in-memory cache quicker, significantly reducing first-hit penalty on a hit. +// +// See New for the example implementation. +type Cache interface { +	// Get is called when the runtime is trying to get the cached compiled functions. +	// Implementations are supposed to return compiled function in io.Reader with ok=true +	// if the key exists on the cache. In the case of not-found, this should return +	// ok=false with err=nil. content.Close() is automatically called by +	// the caller of this Get. +	// +	// Note: the returned content won't go through the validation pass of Wasm binary +	// which is applied when the binary is compiled from scratch without cache hit. +	Get(key Key) (content io.ReadCloser, ok bool, err error) +	// +	// Add is called when the runtime is trying to add the new cache entry. +	// The given `content` must be un-modified, and returned as-is in Get method. +	// +	// Note: the `content` is ensured to be safe through the validation phase applied on the Wasm binary. +	Add(key Key, content io.Reader) (err error) +	// +	// Delete is called when the cache on the `key` returned by Get is no longer usable, and +	// must be purged. Specifically, this is called happens when the wazero's version has been changed. +	// For example, that is when there's a difference between the version of compiling wazero and the +	// version of the currently used wazero. +	Delete(key Key) (err error) +} + +// Key represents the 256-bit unique identifier assigned to each cache entry. +type Key = [sha256.Size]byte diff --git a/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go b/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go new file mode 100644 index 000000000..940a79a8d --- /dev/null +++ b/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go @@ -0,0 +1,76 @@ +package filecache + +import ( +	"encoding/hex" +	"errors" +	"io" +	"os" +	"path" +	"path/filepath" +) + +// New returns a new Cache implemented by fileCache. +func New(dir string) Cache { +	return newFileCache(dir) +} + +func newFileCache(dir string) *fileCache { +	return &fileCache{dirPath: dir} +} + +// fileCache persists compiled functions into dirPath. +// +// Note: this can be expanded to do binary signing/verification, set TTL on each entry, etc. +type fileCache struct { +	dirPath string +} + +func (fc *fileCache) path(key Key) string { +	return path.Join(fc.dirPath, hex.EncodeToString(key[:])) +} + +func (fc *fileCache) Get(key Key) (content io.ReadCloser, ok bool, err error) { +	f, err := os.Open(fc.path(key)) +	if errors.Is(err, os.ErrNotExist) { +		return nil, false, nil +	} else if err != nil { +		return nil, false, err +	} else { +		return f, true, nil +	} +} + +func (fc *fileCache) Add(key Key, content io.Reader) (err error) { +	path := fc.path(key) +	dirPath, fileName := filepath.Split(path) + +	file, err := os.CreateTemp(dirPath, fileName+".*.tmp") +	if err != nil { +		return +	} +	defer func() { +		file.Close() +		if err != nil { +			_ = os.Remove(file.Name()) +		} +	}() +	if _, err = io.Copy(file, content); err != nil { +		return +	} +	if err = file.Sync(); err != nil { +		return +	} +	if err = file.Close(); err != nil { +		return +	} +	err = os.Rename(file.Name(), path) +	return +} + +func (fc *fileCache) Delete(key Key) (err error) { +	err = os.Remove(fc.path(key)) +	if errors.Is(err, os.ErrNotExist) { +		err = nil +	} +	return +} | 
