# Debugging "complexity exceeds the maximum complexity allowed"

**URL:** <https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892>\
**Category:** Developers\
**Created:** [November 27, 2024, 10:50am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892 "2024-11-27T10:50:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 27, 2024, 10:50am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/1 "2024-11-27T10:50:30Z")

</div>

Hey, I was testing a Rust canister locally and I got the following error in the dfx replica:

```auto
Canister's Wasm module is not valid: Wasm module contains a function at index 0 with complexity 1029495 which exceeds the maximum complexity allowed 1000000.

```

How can I go about debugging these issues? are there tools which will allow me to inspect the complexity of a function and find the offending one?  
Also, what are the recommended ways to reduce a function’s complexity?  
I am trying to verify a merkle proof that can be pretty big (proof of membership among \>30k elements)

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [November 27, 2024, 11:59am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/2 "2024-11-27T11:59:07Z")

</div>

`wasm-objdump` from the [wabt package](https://github.com/WebAssembly/wabt) would be my first step. The error says the function is at index 0, so you should be able to see which one it is with `wasm-objdump -j Function`. Generally this doesn’t happen with a handwritten function, but when you’re using some macros which generate huge amounts of code - is it possible you’re doing something like that? The solution would be to break up this large function into a couple smaller ones.

Edit: my mistake, it should be the code section with `wasm-objdump -j Code`.

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 27, 2024, 12:29pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/3 "2024-11-27T12:29:56Z")

</div>

This is the truncated output:

```auto
Function[6785]:
 - func[30] sig=1 <_ZN100_$LT$serde..de..impls..ArrayVisitor$LT$$u5b$T$u3b$$u20$32$u5d$$GT$$u20$as$u20$serde..de..Visitor$GT$9visit_seq17h528fd31d9258ab95E>
.... more lines below this

```

Is `<serde::de::impls::ArrayVisitor<[T; 32]> as serde::de::Visitor>::visit_seq::h64a6699ae0cae3ae`  
The offending function?

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 27, 2024, 1:52pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/4 "2024-11-27T13:52:35Z")

</div>

I did a complete objdump instead of just `-j Function`

```auto
Import[30]:
 - func[0] sig=1 <_ZN3ic03ic011debug_print17h2c2d5d086965e01eE> <- ic0.debug_print

```

Is this the function at index 0? am I printing something that’s too complex?

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [November 27, 2024, 2:55pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/5 "2024-11-27T14:55:08Z")

</div>

Sorry, the index here actually refers to the code section. We should probably improve the error to clarify that. So try looking at `wasm-objdump -x -j Code`.

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 27, 2024, 3:25pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/6 "2024-11-27T15:25:57Z")

</div>

Here’s the truncated output

```auto
Code[6785]:
 - func[30] size=4942 <_ZN100_$LT$serde..de..impls..ArrayVisitor$LT$$u5b$T$u3b$$u20$32$u5d$$GT$$u20$as$u20$serde..de..Visitor$GT$9visit_seq17h528fd31d9258ab95E>

```

Which seems to be the same  
I checked the implementation of ArrayVisitor and its indeed maco heavy, so I switched to `GenericArray<u8, U32>`  
However I still got the error, albeit with a different complexity:

```auto
Failed to install wasm on canister: Principal { len: 10, bytes: [128, 0, 0, 0, 0, 16, 0, 49, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }, error: "Error from Canister fvpkz-24aaa-aaaaa-qaazq-cai: Canister's Wasm module is not valid: Wasm module contains a function at index 0 with complexity 1012563 which exceeds the maximum complexity allowed 1000000."

```

Checking the dump again:

```auto
Code[6798]:
 - func[30] size=514 <_ZN10num_bigint6bigint7convert18to_signed_bytes_le17hb08e9bc707f174a9E>

```

Which is `num_bigint::bigint::convert::to_signed_bytes_le::hb08e9bc707f174a9`

I am not explicitly calling this function anywhere, and its used by many libraries (ic libs included), so removing its usage isn’t really feasible for me

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [November 27, 2024, 4:11pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/7 "2024-11-27T16:11:37Z")

</div>

Something seems weird here. I wouldn’t expect either of those functions to hit the limits, especially because their sizes aren’t very big. Maybe if you have some enum type with many variants the serde (de)serialize functions for that type would hit the limit. Would you be able to send me the problematic Wasm?

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 27, 2024, 4:18pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/8 "2024-11-27T16:18:28Z")

</div>

Sure, [individual\_user\_template.wasm - Deupload](https://app.deupload.com/drive/s/sQenmkG9BC6jY6gMx1mV7MBZJfrarg)

---

<div class="post-metadata">

**Author:** ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)\
**Post date:** [November 28, 2024, 12:59am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/9 "2024-11-28T00:59:50Z")

</div>

Did you run optimization on the wasm?

I am asking because I have run into this error when I was trying to optimize a wasm file with binaryen.

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 28, 2024, 5:49am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/10 "2024-11-28T05:49:55Z")

</div>

no i am just using dfx build

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [November 28, 2024, 7:58am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/11 "2024-11-28T07:58:39Z")

</div>

Are you sure the wasm you sent me is the same one that’s giving you trouble? It doesn’t seem to hit the limit when I test it out locally. This is using dfx 0.24.2. I wasn’t able to fully install it because I’m not sure what arguments your `canister_init` expects, but the Wasm is definitely passing the step where the complexity is checked.

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [November 29, 2024, 3:55pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/12 "2024-11-29T15:55:32Z")

</div>

Apologies, I was sending the wasm from `target/release/wasm32-unknown-unknown` instead of  
`.dfx/`! 🙈

it seems that the error originates from ciborium:

```auto
Section Details:

Code[3724]:
 - func[30] size=282794 <_ZN91_$LT$$RF$mut$u20$ciborium..de..Deserializer$LT$R$GT$$u20$as$u20$serde..de..Deserializer$GT$15deserialize_map17h0a05ef429be6690cE>

```

Here’s the correct wasm for your reference as well:

> **[Deupload - Movies, TV and Celebrities](https://app.deupload.com/drive/s/wsOlh1SYX0aTvaiF9WkY9HIFHxoQj6)**
>
> (Deupload) provides secure cloud storage for your photos, videos, music and other files.

I’ll look into replacing ciborium

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [December 2, 2024, 8:15am UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/13 "2024-12-02T08:15:25Z")

</div>

I’m not sure if ciborium itself is the problem. The Wasm generated by cargo/rustc is fine (since that’s the first one you sent me and it doesn’t hit this limit), so maybe the dfx post-processing steps are just doing a bit too much inlining or something. Which version of dfx are you using and what’s in your `dfx.json` file?

In that large function’s implementation I’m seeing calls to some `ic_stable_structures::BTreeMap` functions, so I think the full decoding of the types that are being stored in this tree might be getting inlined.

---

<div class="post-metadata">

**Author:** ![rupansh-gob](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rupansh-gob/32/16888_2.png) [@rupansh-gob](https://forum.dfinity.org/u/rupansh-gob)\
**Post date:** [December 2, 2024, 9:19pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/14 "2024-12-02T21:19:16Z")

</div>

its dfx 0.24.

Here’s the config for the canister in dfx.json

```auto
    "individual_user_template": {
      "candid": "./src/canister/individual_user_template/can.did",
      "declarations": {
        "node_compatibility": true,
        "output": "./export/declarations/individual_user_template"
      },
      "gzip": true,
      "optimize": "size",
      "package": "individual_user_template",
      "type": "rust"
    },

```

You can also check the source as well(though you can see i’ve switched to minicbor for now to get unblocked): [hot-or-not-backend-canister/src/canister/individual\_user\_template at rupansh/pop · yral-dapp/hot-or-not-backend-canister · GitHub](https://github.com/yral-dapp/hot-or-not-backend-canister/tree/rupansh/pop/src/canister/individual_user_template)

We’re not doing serialization/deserialization of stable btree map, but the std BTreeMap

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [December 3, 2024, 2:28pm UTC](https://forum.dfinity.org/t/debugging-complexity-exceeds-the-maximum-complexity-allowed/37892/15 "2024-12-03T14:28:18Z")

</div>

Yeah that all seems pretty reasonable. It might just be that your `CanisterData` type is a bit large and that its serialization creates a big function once everything is inlined.
