# Big map id attribution
# What
Goal:
- reduce creation of temporary ids
- reduce unnecessary copies
- reduce creation of new ids
To keep in mind:
- [R1] contracts that do nothing weird with their big map should not have their ids change
# How
Note: the following is written for big maps but should be applied to all kinds of lazy storage.
Changes to the interpreter:
- internal operations are not fully finalized during the main interpretation loop, so that big map id stuff is resolved later
- bonus: if the operation is thrown away, that's that much work avoided,
- after the main interpretation loop (i.e. after the call to `interp` in `execute`), id resolution is done as follows:
1. we compute `unowned` the set of ids in the old storage,
1. we fold over:
1. the new storage, when encountering a big map
- with an `id` in `unowned`, we remove it from `unowned` and produce `Existing`,
- with an `id` not in `unowned`, we produce a `Copy` with a fresh non-temporary id,
- with no `id`, we produce an `Alloc` with a fresh non-temporary id,
1. values passed to be storage of internal originations, the same way,
1. values passed as parameters of internal transfers, when encountering a big map
- with an `id` in `unowned`, we remote it from `unowned` and use it,
- with an `id` not in `unowned`, we produce a `Copy` with a fresh temporary id,
- with no `id`, we keep it as is,
1. ids in `unowned` produce a `Remove`
1. elements in the big map diff are ordered so that all `Remove` happen first, then all `Copy`, then all `Existing`, then all `Alloc`.
Changes to apply:
- storage of external originations require the same treatment (with an empty `unowned`);
- no treatment needed for internal originations.
Changes to main:
- same thing as external originations.