Immutability Helper Problem
Description
LeetCode Problem 2691.
Creating clones of immutable objects with minor alterations can be a tedious process. Write a class ImmutableHelper that serves as a tool to help with this requirement. The constructor accepts an immutable object obj which will be a JSON object or array.
The class has a single method produce which accepts a function mutator. The function returns a new object which is similar to the original except it has those mutations applied.
mutator accepts a proxied version of obj. A user of this function can (appear to) mutate this object, but the original object obj should not actually be effected.
For example, a user could write code like this:
const originalObj = {"x": 5};
const helper = new ImmutableHelper(originalObj);
const newObj = helper.produce((proxy) => {
proxy.x = proxy.x + 1;
});
console.log(originalObj); // {"x": 5}
console.log(newObj); // {"x": 6}
Properties of the mutator function:
- It will always return
undefined. - It will never access keys that don't exist.
- It will never delete keys (
delete obj.key) - It will never call methods on a proxied object (
push,shift, etc). - It will never set keys to objects (
proxy.x = {})
Note on how the solution will be tested: the solution validator will only analyze differences between what was returned and the original obj. Doing a full comparison would be too computationally expensive. Also, any mutations to the original object will result in a wrong answer.
Example 1:
Input:
obj = {"val": 10},
mutators = [
proxy => { proxy.val += 1; },
proxy => { proxy.val -= 1; }
]
Output:
[
{"val": 11},
{"val": 9}
]
Explanation:
const helper = new ImmutableHelper({val: 10});
helper.produce(proxy => { proxy.val += 1; }); // { "val": 11 }
helper.produce(proxy => { proxy.val -= 1; }); // { "val": 9 }
Example 2:
Input:
obj = {"arr": [1, 2, 3]}
mutators = [
proxy => {
proxy.arr[0] = 5;
proxy.newVal = proxy.arr[0] + proxy.arr[1];
}
]
Output:
[
{"arr": [5, 2, 3], "newVal": 7 }
]
Explanation: Two edits were made to the original array. The first element in the array was to set 5. Then a new key was added with a value of 7.
Example 3:
Input:
obj = {"obj": {"val": {"x": 10, "y": 20}}}
mutators = [
proxy => {
let data = proxy.obj.val;
let temp = data.x;
data.x = data.y;
data.y = temp;
}
]
Output:
[
{"obj": {"val": {"x": 20, "y": 10}}}
]
Explanation: The values of "x" and "y" were swapped.
Constraints:
2 <= JSON.stringify(obj).length <= 4 * 10^5mutatorsis an array of functionstotal calls to produce() < 10^5
Solution
Approach: Copy-on-write proxies
Never touch the original: give the mutator a proxy and copy lazily, only along the path that is written to (structural sharing, the idea behind Immer).
makeProxy(original, onCopy)wraps one object. It keeps its own shallowcopy, created on the first write, and tells its parent throughonCopyso the parent can point at the new child (which in turn makes the parent copy itself, all the way to the root).- A
setcreates the copy if needed and writes the value into it. - A
getreads from the copy if there is one, otherwise from the original. When the value is an object, return a (cached) child proxy built on the original child, so repeated reads see the same proxy and its pending copy. - After the mutator runs, the result is the root copy, or the untouched original if nothing was written.
Objects that were never written to are shared with the original, so only the changed path costs memory. The tricky case is a child that is modified before the parent: the child's onCopy forces the parent's copy first.
Complexity: Time O(depth) per access and per write plus the size of each copied level; space proportional to the objects along modified paths.
class ImmutableHelper {
constructor(obj) {
this.obj = obj;
}
produce(mutator) {
let result = this.obj;
// Proxy for `original`; onCopy(copy) is called once, when the first write creates the copy.
const makeProxy = (original, onCopy) => {
let copy = null;
const children = {};
const ensureCopy = () => {
if (copy === null) {
copy = Array.isArray(original) ? [...original] : { ...original };
onCopy(copy);
}
return copy;
};
return new Proxy(original, {
get(_target, key) {
const value = copy !== null ? copy[key] : original[key];
if (value === null || typeof value !== 'object') return value;
if (!(key in children)) {
children[key] = makeProxy(original[key], childCopy => {
ensureCopy()[key] = childCopy;
});
}
return children[key];
},
set(_target, key, value) {
ensureCopy()[key] = value;
return true;
},
});
};
mutator(
makeProxy(this.obj, copy => {
result = copy;
}),
);
return result;
}
}