mattbierner / hamt

Javascript Hash Array Mapped Trie
MIT License
251 stars 16 forks source link

Transaction Interface #1

Closed mattbierner closed 10 years ago

mattbierner commented 10 years ago
hamt.mutate(function(h) {
    for (var i =0 ; i < 1000; ++i)
        h = hamt.set(i, i, h);
   return h;
}, h);

All inner set operations would be mutations instead of persistent update. This would greatly improve performance by reducing memory allocations and path copying.

mutate would make the input hamt as a mutable, and mark the return value as immutable. It is assumed these would referenced be the same, although this would not be enforced. The internal update operation would simply check to see if we are in mutable mode when deciding if we should transform or mutate.

Capturing h in the mutate function for use outside of the function would result in undefined behavior.

mattbierner commented 10 years ago

Because this requires tracking an additional edit property separate from the tree itself, I decided to fork this work to hamt_plus instead. The API of hamt_plus is nearly identical to hamt, but hamt_plus supports a few more features in exchange for some additional overhead.