Bangle.js has 64kb RAM, about 40k of this is usable by user JS code.
There are 3 main strings that should interest us.
Dees not use JSVars to store the data, its pointed to directly. This behaves like a pointer, a function that generates one is eg. E.memoryArea(addr,len). So it points to RAM on bangle.js because the SPI flash is not memory mapped.
Thats where flash strings come in, these are the same as NativeStrings except they reference SPI Flash and are read-only.
Uses contiguous blocks of JSVars to store the string in RAM. These are what ArrayBuffers.
You can use the upload function of the web ide to put an image that is larger than Bangle.js RAM onto the flash. Its possible to render this image, thanks to FlashStrings.
When espruino is loading and parsing a javascript file, it uses "Storage".read() which returns a FlashString. Thus the Lexer, when parsing the code and getting next character jslGetNextCh
and jsvStringIteratorNew
call the function jsvStringIteratorLoadFlashString
. The JsvStringIterator
struct has a flashStringBuffer[16]
member, this is where flash data is read and copied into, in chunks, when needed. Thus only ever using 16 bytes overhead of RAM at any given moment during parsing.
There is a technique to save on memory but consume more cpu. That is to store a function callback that returns data instead of store the data directly into a variable. Because the function pointer is stored as a pointer to Flash.