# Heap out of bounds ， use motoko

**URL:** <https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497>\
**Category:** Developers\
**Tags:** Functional-Programming\
**Created:** [January 26, 2022, 9:23am UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497 "2022-01-26T09:23:59Z")\
**Posts on this page:** 6\
**Page:** 3

<div class="post-metadata">

**Author:** ![infu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/infu/32/11321_2.png) [@infu](https://forum.dfinity.org/u/infu)\
**Post date:** [June 20, 2023, 1:51pm UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/41 "2023-06-20T13:51:30Z")

</div>

haven’t tried --rts-stack-pages also haven’t tested v7. I’ve shared the repo, you can check it out. Or I will a bit later

---

<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:** [June 20, 2023, 4:19pm UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/42 "2023-06-20T16:19:51Z")

</div>

@claudio I just implemented your singleton suggestion for v8 map  
Here is the branch [GitHub - ZhenyaUsenko/motoko-hash-map at stack-overflow-fix-attempt](https://github.com/ZhenyaUsenko/motoko-hash-map/tree/stack-overflow-fix-attempt)  
Still fails to upgrade. Increasing `--rts-stack-pages <n>` doesn’t help either

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 21, 2023, 8:59pm UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/43 "2023-06-21T20:59:51Z")

</div>

Sorry, didn’t see this and will try to take a look tomorrow.

The flag only affects one of the stacks used by the compiled code. The hard limit is determined by the Wasm stack which we can’t configure but is set by the IC. So it can help, but only so much.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 22, 2023, 10:41am UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/44 "2023-06-22T10:41:37Z")

</div>

So the introduction of [var \_] as you did should improve sharing, but I think if its failing with a stack overflow then it might be the use of deeply nested recursive value (of type Entry) that might be the other problem. Reading the code and this, [Map v8.0.0, it's finally here](https://forum.dfinity.org/t/map-v8-0-0-its-finally-here/18962), I wonder if it’s the support for the previous and next pointers,preserving insertion order, that (I guess) are linking all entries in a long list.

Just a hunch, I’m afraid. Could you record the hashes of previous and next entries instead and link them that way?

---

<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:** [June 22, 2023, 12:02pm UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/45 "2023-06-22T12:02:52Z")

</div>

> [@claudio](#):
>
> Could you record the hashes of previous and next entries instead and link them that way?

That’s an interesting idea. Could definitely be a temporary solution until we have a better serialization/deserialization algorithm on upgrades. One problem I see immediately is  
current structure `(..., links: [var Entry<K, V>])` will be replaced with  
double array `(..., branches: [var Entry<K, V>], prevNext: [var Nat32])`  
which as far as I understand will increase space per entry by 12 bytes

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 22, 2023, 1:00pm UTC](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497/46 "2023-06-22T13:00:32Z")

</div>

That sounds about right. It will be another 4 bytes larger in the incremental gc (as all objects are).

Still might be worth the experiment to see if it fixes the issue (or just take out support for pre and next to determine that).

It is just a hunch though. Sorry if it wastes your time in the end.

[Previous page](https://forum.dfinity.org/t/heap-out-of-bounds-use-motoko/10497.md?page=2)
