# Is there any way to mutate the value inside IC-Stable-Structures BTreeMap?

**URL:** <https://forum.dfinity.org/t/is-there-any-way-to-mutate-the-value-inside-ic-stable-structures-btreemap/25673>\
**Category:** Developers\
**Tags:** rust\
**Created:** [December 18, 2023, 11:08am UTC](https://forum.dfinity.org/t/is-there-any-way-to-mutate-the-value-inside-ic-stable-structures-btreemap/25673 "2023-12-18T11:08:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NS01](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ns01/32/9816_2.png) [@NS01](https://forum.dfinity.org/u/NS01)\
**Post date:** [December 18, 2023, 11:08am UTC](https://forum.dfinity.org/t/is-there-any-way-to-mutate-the-value-inside-ic-stable-structures-btreemap/25673/1 "2023-12-18T11:08:30Z")

</div>

I’ve got two canisters which index ICP transactions - one uses the IC stable memory ([ic\_stable\_memory - Rust](https://docs.rs/ic-stable-memory/latest/ic_stable_memory/index.html)) by senior.joinu. This works well and can update up to 10,000 entries in the SBTreeMap without hitting the canister instruction limit (using get\_mut method on the map).

The IC Stable Structures canister however can’t even process 2000 without hitting the instruction limit. I think this is down to the fact that (unless I’m missing something) to update a value using the IC Stable Structures version, you first need to get the value, copy it/ modify it and then re-insert it?

Are there any plans to add a get\_mut method to IC-Stable-Structures BTreeMap?

---

<div class="post-metadata">

**Author:** ![ielashi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ielashi/32/15704_2.png) [@ielashi](https://forum.dfinity.org/u/ielashi)\
**Post date:** [December 18, 2023, 3:35pm UTC](https://forum.dfinity.org/t/is-there-any-way-to-mutate-the-value-inside-ic-stable-structures-btreemap/25673/2 "2023-12-18T15:35:38Z")

</div>

Thanks Nathan for sharing this observation. I agree there’s a long way to go as far as the performance of stable structures.

> [@NS01](#):
>
> I think this is down to the fact that (unless I’m missing something) to update a value using the IC Stable Structures version, you first need to get the value, copy it/ modify it and then re-insert it?

This is actually _not_ the performance bottleneck currently. As in, introducing a `get_mut` wouldn’t change the performance in a meaningful way. The performance bottleneck is in loading (deserializing) and saving (serializing) BTree nodes. If we cache nodes so we don’t need to deserialize them every time we access them, I expect the performance to improve significantly. I filed [a ticket](https://github.com/dfinity/stable-structures/issues/166) to track this.

Can you share the type of the entries you’re modifying? We can then use that to do some benchmarking.

---

<div class="post-metadata">

**Author:** ![NS01](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ns01/32/9816_2.png) [@NS01](https://forum.dfinity.org/u/NS01)\
**Post date:** [December 18, 2023, 3:53pm UTC](https://forum.dfinity.org/t/is-there-any-way-to-mutate-the-value-inside-ic-stable-structures-btreemap/25673/3 "2023-12-18T15:53:49Z")

</div>

Ah ok, I was just guessing it was the double read of the map. I’m using a fairly large struct which is 3 layers deep. It’s a lot to load/ save and probably is making this issue a LOT worse. I could spit the data into separate BTreeMaps but I like the idea of being able to pull all the Account data in a single lookup.

```auto
The main map is <key: String, value: AccountData>

pub struct AccountData {
    pub overview: Overview,
    pub data: IndexData,
}

 pub struct Overview {
    pub first_active: u64,
    pub last_active: u64,
    pub sent: (u32, Nat), // count, value 
    pub received: (u32, Nat), // count, value
    pub balance: Nat, 
 }

pub struct IndexData {
    pub links: BTreeMap<u64, LinkData>,
    pub blocks: Vec<u64>
}

pub struct LinkData {
     pub linked_from: u64,
     pub linked_id: u64,
     pub number_txs: u32,
     pub gross: Nat,
     pub net: i128
}

```
