The easiest way to store up to 40 billion items* in a list in Scratch. The demo is pretty basic; it stores 1 million elements of data which you can access. Press "See inside" to test it for yourself. * The theoretical upper limit. In practice, your computer would crash before you could use all of that data.
How it works: The Mass Storage Engine divides its simulated memory into 200_000 chunks. Each chunk is initially stored as a string which stores several variables separated by the null character �. So a chunk containing the 4 values "hello", "world" , "apple" and "banana" would be stored as "hello�world�apple�banana". This is how the mass storage engine stores so many variables. But if this was all there was to it, it would work very slowly. The engine would have to iterate over a whole chunk every time it wanted to get or set the value of a variable. Instead, before a chunk is accessed, its contents are loaded into a list known as the cache. This still requires the whole string to be iterated over, but after that, variables in that chunk can be read and changed almost as quickly as variables in a regular list! (This is why chunks are limited to 200_000 elements: otherwise they would not fit in the cache) The cache can store several chunks at a time. In this demo, each chunk has a size of 40, so the cache can store 5_000 chunks. When there is not enough room to load another chunk into the cache, a chunk that is in the cache will be compressed back into string form to make room. How does the engine choose which chunk to replace? The following policy is currently used: * The chunks representing the first 100_000 elements are always loaded in the cache. * Other chunks are replaced using a First-in, First-out policy: the chunk which was cached first is decached.