github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/filecache/compilationcache.go (about)

     1  package filecache
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"io"
     6  )
     7  
     8  // Cache allows the compiler engine to skip compilation of wasm to machine code
     9  // where doing so is redundant for the same wasm binary and version of wazero.
    10  //
    11  // This augments the default in-memory cache of compiled functions, by
    12  // decoupling it from a wazero.Runtime instance. Concretely, a runtime loses
    13  // its cache once closed. This cache allows the runtime to rebuild its
    14  // in-memory cache quicker, significantly reducing first-hit penalty on a hit.
    15  //
    16  // See New for the example implementation.
    17  type Cache interface {
    18  	// Get is called when the runtime is trying to get the cached compiled functions.
    19  	// Implementations are supposed to return compiled function in io.Reader with ok=true
    20  	// if the key exists on the cache. In the case of not-found, this should return
    21  	// ok=false with err=nil. content.Close() is automatically called by
    22  	// the caller of this Get.
    23  	//
    24  	// Note: the returned content won't go through the validation pass of Wasm binary
    25  	// which is applied when the binary is compiled from scratch without cache hit.
    26  	Get(key Key) (content io.ReadCloser, ok bool, err error)
    27  	//
    28  	// Add is called when the runtime is trying to add the new cache entry.
    29  	// The given `content` must be un-modified, and returned as-is in Get method.
    30  	//
    31  	// Note: the `content` is ensured to be safe through the validation phase applied on the Wasm binary.
    32  	Add(key Key, content io.Reader) (err error)
    33  	//
    34  	// Delete is called when the cache on the `key` returned by Get is no longer usable, and
    35  	// must be purged. Specifically, this is called happens when the wazero's version has been changed.
    36  	// For example, that is when there's a difference between the version of compiling wazero and the
    37  	// version of the currently used wazero.
    38  	Delete(key Key) (err error)
    39  }
    40  
    41  // Key represents the 256-bit unique identifier assigned to each cache entry.
    42  type Key = [sha256.Size]byte