Stable Structures - Increasing max_size on upgrade

we’re using stable-structures in our rust canisters.

Is it safe to change the max_size of a bounded type?

e.g change MAX_VALUE_SIZE to 150 and then upgrade the canister?

const MAX_VALUE_SIZE: u32 = 140;

#[derive(Serialize, Clone, Deserialize, CandidType, Debug, PartialEq, Eq)]

pub struct MyType {

// … code

}

impl Storable for MyType {

fn to_bytes(&self) → Cow<[u8]> {

Cow::Owned(Encode!(self).unwrap())

}

fn from_bytes(bytes: Cow<[u8]>) → Self {

Decode!(&bytes, Self).unwrap()

}

const BOUND: Bound = Bound::Bounded {

max_size: MAX_VALUE_SIZE,
}
}

Initial post does not give enough actionable details about the use case, so I can’t give feedback that would be safe for every scenario. And even if it had, I don’t think it would be a safe answer. So I’d rather avoid suggesting something that could be dangerous in certain cases.

Therefore, I’d recommend treating a max size change as a data-layout change and doing a proper migration instead of modifying the bound in place.

The safest approach is to define a new “v2” type/structure with the new bond and new memory id. Copy existing data into it with proper migration/transformation if needed. Switch to v2 once the copy completes.

Currently it still keeps allocated memory tied to v1 structure. You might use it later in the future when doing another layout-change migration and re-use this memory id for v3.

We are currently working on additional options to re-claim previously used memory to improve the situation.

Alternatively, if you are using MyType only a StableBTreeMap, you can make the size Unbounded, and it should work.