Preupgrade and postupgrade limits

Are there cycle limits on preupgrade and postupgrade? I would imagine that large hashmaps, lists, etc being output to an array will take some significant cycles once they get pretty large. Likewise, loading a few hundred thousand items out of an array into a HashMap will take some time as well.

1 Like

The cycles limits are enforced at the message level not at the pre/post upgrade canister method level. You can find the definition here:

If you have inefficient serialization/deserialization of your stable memory data in those pre/post hooks, you can hit the execution message limit. If you hit this limit, your state will not be committed (it’ll roll back) but the cycles that’ve been consumed will be deducted from your canister’s cycle balance.

2 Likes

Thanks!

Do you know where we can find that execution limit?

You can find some of the execution limits in the file I linked above, there are subnet type specific limits defined in there.

Does it mean that if my canister hits a limit and preupgrade method will never succeed - I will never be able to “upgrade” my canister? The only way to change the code will be “reinstall”?

Correct – let me shamelessly plug my canister audit advice about the risks related to canister upgrades.

2 Likes

Thank for sharing a link.

What about MAX_INSTRUCTIONS_PER_INSTALL_CODE constant?

I thought that “pre/post upgrade” uses MAX_INSTRUCTIONS_PER_INSTALL_CODE but not MAX_INSTRUCTIONS_PER_MESSAGE …

Please clarify this question…

I believe you are right. The constant for that is here:

It’s a little disappointing to read that Motoko stable variables are not recommended for use due to:

  • Motoko serializing stable variables into the main wasm memory before copying the bytes into stable memory (thus making a 4 GB canister memory limit more like 2 GB)
  • the aforementioned instruction limit on the pre-upgrade and post-upgrade hooks

I’m really hoping these will be addressed soon, as Motoko stable variables are the first place new IC developers turn to having read the docs.

2 Likes