cartesi / machine-emulator

The off-chain implementation of the Cartesi Machine
GNU Lesser General Public License v3.0
64 stars 33 forks source link

Add support for snapshot/rollback in local machines #237

Open edubart opened 5 months ago

edubart commented 5 months ago

This is an experimental PR adding support for performing snapshot/rollback on local machines. Making the APIs available in Windows and WASM. It uses the naive implementation of duplicating the whole machine memory.

This PR does not try to optimize this operation to compete with JSONRPC fork(), it just provides the naive implementation that will work on any system.

TODO

Benchmark

To comparise the of performance of local vs jsonrpc machines, I tested booting the last release of tools rootfs tanking random roollbacks:

math.randomseed(0)
while not machine:read_iflags_H() do
    machine:snapshot()
    machine:run(machine:read_mcycle() + 1000)
    if math.random() < 0.3 then
        machine:rollback()
    else
        machine:commit()
    end
end

This is the result:

local       67 iterations/s
jsonrpc    895 iterations/s

Therefore jsonrpc is about 13x times faster than local, the performance for local would be even worse from bigger machines.