# Mini BigMap library

**URL:** <https://forum.dfinity.org/t/mini-bigmap-library/5794>\
**Category:** Developers\
**Created:** [July 8, 2021, 6:35pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794 "2021-07-08T18:35:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [July 8, 2021, 6:35pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/1 "2021-07-08T18:35:16Z")

</div>

Hi everyone,  
As BigMap library is late to the party I decided to build a mini BigMap library which is an auto-scalable canister solution.

Here’s the code: [GitHub - gabrielnic/motoko-cdn: Motoko cdn/storage solution. ie: mini big-map](https://github.com/gabrielnic/motoko-cdn)  
Demo: [https://b2r3f-wiaaa-aaaae-aaaxa-cai.ic0.app/](https://b2r3f-wiaaa-aaaae-aaaxa-cai.ic0.app/)

Note this is an MVP and there’s still work to do. eg: inter-canister query calls etc.

From README:

The core architecture relies on actor classes that allows canister creation on the fly. The container actor stores an array of all the canisters created on the fly and the size of that canisters.

In order to get a canister memory size I’m using `rts_memory_size` : the rts\_memory\_size is the current size of the Wasm memory array (mostly the same as the canister memory size). This can only grow, never shrink in Wasm, even if most of it is unused. However, unused/unmodified canister memory ought to have no cost on the IC.

You can use the IC management canister interface to get the memory of a canister as well ie: `IC.status ` but rts\_memory\_size and IC.status numbers are not the same - the implementation will do some more work before and after after you call rts\_memory\_size (deserializing arguments and serializing results, which may affect memory size after you’ve read it. So you can use either but from my tests `rts_memory_size` is closer to reality.

Due to the way garbage collector is built in motoko (takes about half of the canister memory so a bit less than 2GB) and from my tests I decided to set a threshold of 2GB `private let threshold = 2147483648; // ~2GB` so once the threshold is reached the container will spawn a new canister for storage. `Note` : you only pay for what you consume.

> **[Internet Computer | Documentation](https://sdk.dfinity.org/docs/interface-spec/index.html#ic-management-canister)**
>
> Documentation for the internet computer.

Using the IC management canister I can update the new canister settings `compute_allocation = ?5; memory_allocation = ?4294967296;` and the controllers to the wallet canister and the container canister.

Frontend: you can upload any type of file from this category: `jpeg, gif, jpg, png, svg, avi, aac, mp4, wav, mp3` but you can update the front-end `getFileExtension` to allow/remove types.

Files are split in chunks of 500Kb and uploaded into an available bucket.

Any improvements/comments are welcome.  
Many thanks to @claudio

---

<div class="post-metadata">

**Author:** ![ComputerInternetMan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/computerinternetman/32/1848_2.png) [@ComputerInternetMan](https://forum.dfinity.org/u/ComputerInternetMan)\
**Post date:** [July 8, 2021, 6:38pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/2 "2021-07-08T18:38:41Z")

</div>

Absolutely awesome. This is going to require more than one pot of coffee 🕶 Looking forward to contributing to this worthy effort!

I am happy to report that a 7MB [Pets.Com](http://Pets.Com) Sock Puppet png was successfully uploaded on mobile and downloaded on desktop.

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [July 8, 2021, 9:20pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/3 "2021-07-08T21:20:50Z")

</div>

Brilliant work Gabriel!!

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [July 8, 2021, 10:28pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/4 "2021-07-08T22:28:08Z")

</div>

Very cool, works like a charm! Thank you for putting the effort into making this

---

<div class="post-metadata">

**Author:** ![matthewhammer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/matthewhammer/32/323_2.png) [@matthewhammer](https://forum.dfinity.org/u/matthewhammer)\
**Post date:** [July 8, 2021, 11:09pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/5 "2021-07-08T23:09:22Z")

</div>

This is fantastic! I just used it to upload a gif and it worked as advertised. Very cool.

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [July 9, 2021, 7:42am UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/6 "2021-07-09T07:42:37Z")

</div>

Very nice!

A small aside from something I noticed during a quick glance at the code: an expression like

```auto
do? { f(x)! }

```

is the same as simply writing

```auto
f(x)

```

Similarly, any use of `do?` where the only occurrence of `!` is at the very end.

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [July 9, 2021, 12:24pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/7 "2021-07-09T12:24:50Z")

</div>

Thank you all 🙂 Hope this will be helpful to others.

Yeah initially I wanted to return an array of values or an empty array but then discovered that’s not how it works. I will update my code. Thank you.

---

<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:** [July 9, 2021, 4:22pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/8 "2021-07-09T16:22:28Z")

</div>

If you want to speed up the getters (without support for nested queries), as an experiment, why not try replacing each getter on Container with two calls to queries, the first to get the bucket from the container, and the second query the bucket itself. I expect two queries will still be much faster than one nested update call.

This won’t lead to any improvement for canisters getting data, but should speed up any frontend getting data.

Curious to see what the performance results are, or any reason this should not work.

Even better would be if the queries were _certified_, but that’s a lot more challenging…

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [September 16, 2021, 3:43pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/9 "2021-09-16T15:43:55Z")

</div>

Hi @claudio

Sorry for the late response, it went straight to the backlog for any upgrades.

Indeed I’ve separated the logic a bit so now each file info is kept in container so I can get access to query calls.

Next stop: inter-canister query calls but I’m pretty sure that’s not going to happen soon(like a few months ?)

Any news on the new garbage collector?

Thanks

---

<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:** [September 16, 2021, 7:10pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/10 "2021-09-16T19:10:47Z")

</div>

Yeah, I wouldn’t hold my breath for inter canister query calls.

A compacting garbage collector, that makes more memory available to the heap, and better GC scheduling in general (don’t GC on every message, only after reasonable growth), has been out for a while and can be specified as a moc command line option in dfx.json. See the discussion here:

> [@Increased Canister Smart Contract Memory](https://forum.dfinity.org/t/increased-canister-storage/6148/75):
>
> –compacting-gc should enable it. Dfx supports this via the optional “args” field in the dfx.json record for Motoko projects.

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 22, 2022, 2:07pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/11 "2022-06-22T14:07:18Z")

</div>

Hey, I am wondering is this library still maintained and is the work ongoing for the “roadmap” specified in the readme of it? 🙂 Or, is there some other library based on this one that is supposed to be used instead of it? Thanks a lot for the contribution!

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [June 23, 2022, 9:23am UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/12 "2022-06-23T09:23:51Z")

</div>

Hey, I’ve been slowly working on a new architecture but it’s still WIP. It will come out with the latest dfx. If you have have any suggestions/code improvements feel free to open a PR 🙂  
Thanks

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 24, 2022, 12:28pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/13 "2022-06-24T12:28:20Z")

</div>

I am guessing this can only be tested live on the ICP blockchain as it uses the IC container to get `canister_status`, `update_settings` etc?

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [June 24, 2022, 3:20pm UTC](https://forum.dfinity.org/t/mini-bigmap-library/5794/14 "2022-06-24T15:20:43Z")

</div>

erm no, you can test those on local as well with the IC management canister.
