V7 memory optimization

techniques

 

@CesantaHQ

by Marko Mikuličić, Cesanta Software, for LD2015 Pisa

Implicit data structures

O(1) compacting collector

What is V7

  • Embedded JavaScript engine written in C
  • Compliant to ISO C90 and ISO C++98
  • Targets embedded systems
  • Or C/C++ programs that need scripting engine
  • Conforms to ECMA 5.1 standard
  • Easy to embed: only two files, v7.c and v7.h
  • Easy to use embedding API
  • Open Source, under GPLv2/commercial license
  • GitHub repo: https://github.com/cesanta/v7/

Who are we

  • Irish Startup founded by ex-Googlers in 2013
  • Makers of the Mongoose embedded web server, over 1M downloads since 2004
  • Goal: make it easy for everyone to make connected devices

Why JavaScript ?

Why JavaScript ?

  • Why not Lua, Python, LISP, brainfeck, ... 
  • Well, why scripting ?

Why JavaScript

  • It's for everyone, and their dog
  • Popular, Widely available
  • Maturing but not stagnating

Goals: V7 vs other JS VMs

  • Portable
  • Small runtime overhead
  • Small code footprint
  • Reasonably fast
  • Simple

Overview

Interpreter

VM

StdLib

GC

Parser

Compiler

Heap

User C code

Memory savings

AST

AST

AST

Implicit data structures

Implicit data structures are data structures with negligible space overhead;  auxiliary information is mostly represented by permuting the elements cleverly

Object model

typedef val_t double; /* IEEE 745 NaN packing */

struct v7_property {
  struct v7_property *next; /* Linkage in struct v7_object::properties */
  unsigned int attributes;
  val_t name;  /* Property name (a string) */
  val_t value; /* Property value */
};

struct v7_object {
  struct v7_property *properties;
  struct v7_object *prototype;
  uint8_t attributes;
};
#include <v7.h>

int main() {
  struct v7 *v7 = v7_create();
  v7_val_t res;

  v7_exec(v7, "function random() { return 42; }", NULL);
  v7_exec(v7, "function msg() { return 'foo'; }", NULL);


  v7_exec(v7, "random()", &res);
  printf("random number (really): %lg\n", v7_to_number(res));


  v7_exec(v7, "msg()", &res);
  printf("msg: %s\n", v7_to_cstring(res));


  v7_destroy(v7);
  return 0;
}

Object model


struct Value {
        union {
                int boolean;
                double number;
                String *str;
                Object *object;
                /* more */
        } u;
        char type; /* type tag */
        /* padding */
};

NaN packing

V7 Strings

V7 Strings

V7 Strings

Compacting GC

Challenges:

  • Updating values anywhere
  • Taking O(1) extra space

Compacting GC

Compacting GC

Compacting GC

Compacting GC

Questions ?

Made with Slides.com