Proto-Functions

Proto-functions are a way to restore the execution state of a function withoutrerunning the code that got it to that state.

Proto-functions mean we can snapshot a function when it's running, then restoreit later, resuming its execution. The same proto-function can be restored multipletimes and across different hosts.

Reducing cold starts

Proto-functions can be used to reduce function cold starts by:

  • Running initialisation code once when the function is uploaded
  • Creating a proto-function after this function has been run
  • Restoring all subsequent function calls from this snapshot

Any slow or resource-intensive initialisation that would otherwise be performed on everycall can be captured in this proto-function snapshot, thus repeatedly saving on overheads.

Defining a proto-function

Proto-functions can be defined in C/C++ with the FAASM_ZYGOTE macro:

  1. #include "faasm/faasm.h"
  2.  
  3. int myGlobal;
  4.  
  5. FAASM_ZYGOTE() {
  6. // Run once
  7. myGlobal = 5;
  8.  
  9. return 0;
  10. }
  11.  
  12. FAASM_MAIN_FUNC() {
  13. // Context available to all subsequent function calls
  14. printf("My global = %i\n", myGlobal);
  15.  
  16. return 0;
  17. }

Cross-host migration

Proto-functions are also used in Faasm to migrate functions across hosts.