# Map v8.0.0, it's finally here

**URL:** <https://forum.dfinity.org/t/map-v8-0-0-its-finally-here/18962>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [March 16, 2023, 1:52pm UTC](https://forum.dfinity.org/t/map-v8-0-0-its-finally-here/18962 "2023-03-16T13:52:12Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![ZhenyaUsenko](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zhenyausenko/32/12200_2.png) [@ZhenyaUsenko](https://forum.dfinity.org/u/ZhenyaUsenko)\
**Post date:** [September 1, 2023, 7:19am UTC](https://forum.dfinity.org/t/map-v8-0-0-its-finally-here/18962/12 "2023-09-01T07:19:23Z")

</div>

# Map V9 is out!

## It is 2.25 times more space efficient than V7

## It is 4.00 times more space efficient than V8

> **[GitHub - ZhenyaUsenko/motoko-hash-map: Stable hash maps for Motoko](https://github.com/ZhenyaUsenko/motoko-hash-map)**
>
> Stable hash maps for Motoko

The primary goal of this release is to fix the issue some people started experiencing some time ago which can prevent canister upgrades when your map grows too big.

> [@Heap out of bounds ， use motoko](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/28):
>
> @claudio @ZhenyaUsenko @skilesare I’ve got a similar problem. At first, I thought it was my Mac, but then I deployed in the motoko playground and I am getting the same results. [https://m7sm4-2iaaa-aaaab-qabra-cai.ic0.app/?tag=2112554174](https://m7sm4-2iaaa-aaaab-qabra-cai.ic0.app/?tag=2112554174) repo: [https://github.com/infu/bug\_x\_weird](https://github.com/infu/bug_x_weird) You will probably need “Internet Base” VS Code extension so you can run the ./check.blast (from repo) to populate it with 100k records. These records look like this: Once they get inserted, the canister …

**V9** reverts its internal structure to the one that is more similar to **V7** (as linked nature on **V8** was the subject of upgrade issues) but keeps all the improvements described in the original post (like DEQ methods, improved iterators, bidirectionality). The only things that were taken out (impossible to support with the current internal structure) are:

- **putBefore** and **putAfter** are no longer present
- mutation during iteration is no longer supported
- (not a direct functionality downgrade) a shift in internal structure makes rehashes necessary again, decreasing the worst case complexity of some methods to linear

**V8** map is moving to another repo ( **Motoko Hash Tree** ) and will be supported in the future. Its algorithm still has some advantages that can not be achieved with more common hash map algorithms. Though, its usage will be discouraged until we get a motoko compiler that fully fixes serialization/deserialization on upgrades for linked data structures.

> **[GitHub - ZhenyaUsenko/motoko-hash-tree: Motoko Hash Tree](https://github.com/ZhenyaUsenko/motoko-hash-tree)**
>
> Motoko Hash Tree

On top of fixing the upgrade issue **V9** provides huge improvements in terms of consumed space. On average it will consume `1.7` times less space than **V7** and `2.7` times less space than **V8**. In the best case scenario these differences jump up to `2` and `3.5` times respectively. **V9** also does not experience an increase in space consumption with Incremental GC enabled (unlike **V7** and **V8** ) which will further increase the multipliers to `2.25` and `4`. Here I need to note that although **V8** seems terrible in terms of space consumption looking at those multipliers, its worst case scenario was always better than **V7** (more testing below).

Here is a performance comparison between recent Map versions using 100\_000 **(Nat32, Nat32)** entries.

| Structure | Insert | Get | Update | Delete | Delete Desc | Space + Garbage | Space |
| --- | --- | --- | --- | --- | --- | --- | --- |
| V9 Map | 100\_565\_493 | 31\_135\_243 | 32\_997\_376 | 93\_732\_517 | 91\_153\_853 | 6\_292\_948 | 2\_097\_264 |
| V8 Map | 78\_535\_383 | 54\_324\_546 | 73\_834\_132 | 109\_237\_393 | 93\_839\_974 | 8\_000\_016 | 5\_600\_016 |
| V7 Map | 78\_533\_461 | 27\_591\_504 | 37\_182\_651 | 66\_453\_451 | 64\_171\_242 | 18\_706\_608 | 3\_448\_676 |

Percentage table, comparing other versions to **V9**.

| Structure | Insert | Get | Update | Delete | Delete Desc | Space + Garbage | Space |
| --- | --- | --- | --- | --- | --- | --- | --- |
| V9 Map | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 |
| V8 Map | 0.781 | 1.745 | 2.238 | 1.165 | 1.029 | 1.271 | 2.670 |
| V7 Map | 0.781 | 0.886 | 1.127 | 0.709 | 0.704 | 2.973 | 1.644 |

The next table shows the amount of bytes each map version occupies per single **(Nat32, Nat32)** entry. Both the default GC and incremental GC were tested.

| Structure | Min | Max | Incremental Min | Incremental Max |
| --- | --- | --- | --- | --- |
| V9 Map | 16.00 | 42.66 | 16.00 | 42.66 |
| V8 Map | 56.00 | 56.00 | 64.00 | 64.00 |
| V7 Map | 32.00 | 63.50 | 36.00 | 67.50 |

# Upgrade strategy

- Motoko compiler v0.9.8 (or higher) is required for Map **V9**
- Add **HashUtils** as a second parameter to **pop** and **cycle** methods
- The **new** method no longer requires **HashUtils** as a first param
- Migrate all your stable maps using `let newMap = Map_9.fromIter(Map_8.entries(oldMap), Map_9.x_hash)`
- Custom **HashUtils** no longer needs its third compound `getNullKey` which was a point of misunderstanding for some people (it also no longer requires to exclude **0xffffffff** hash value)

---

_[View the full topic](https://forum.dfinity.org/t/map-v8-0-0-its-finally-here/18962)._
