Wasm module contains a function that is too complex

I hope we release 0.12.0-beta.4 soon as we had a bug in dfx generate. I’m trying to get the complexity limit into that as well.

1 Like

I just tried out dfx 0.12.0-beta.4 and it looks like the complexity limit is included. I’ve been able to switch over to ic-wasm from ic-cdk-optimizer. I’m still using a modified version of RustPython, which had the original limit on the number of functions. I will eventually try to get back to the upstream version of RustPython and then hopefully this entire problem for me has been resolved.

Thanks for all of the work you’ve been putting in.

Update: I still can’t use ic-wasm, as the optimization in Wasm binary size is not as good as ic-cdk-optimizer.

For a simple Kybra (Python) canister that I’m using to test with:

When using ic-cdk-optimizer, my original Wasm binary is 9.8MiB, which is then optimized to 5.4MiB, which is then gzipped to 1.8MiB, which can be successfully deployed.

When using ic-wasm shrink, my original Wasm binary is 9.8MiB, which is then optimized to 8.7MiB, which is then gzipped to 2.2MiB, which cannot be successfully deployed.

Is there something I’m missing in my usage of ic-wasm?

ic-wasm shrink only performs dead code elimination, so it’s expected that wasm-opt/ic-cdk-optimizer can shrink the wasm size much further. The only problem with wasm-opt is that it also removes the canister metadata from wasm module, and there doesn’t seem to have an easy way to disable it. Well, we could store the metadata before wasm-opt and then save them back after the optimization…


I just wanted to confirm that the Wasm module contains a function that is too complex issue has been resolved for us as of dfx 0.12.0. All of our Kybra tests are passing without the hack we had to do in RustPython to get around this issue.


Hey, we just updated our Rust Python interpreter in Kybra to the latest, and we now are unable to deploy because there is a function (I’m not sure which) with a complexity of 16_722. Is there hope of further increasing the complexity limit in dfx?

1 Like

Let me ping sdk folks @lastmjs

1 Like

A 20_000 limit would be nice. The complexity comes from the same code as before, RustPython uses lalrpop to generate its parser, and that auto-generated code is extremely complicated. There is a function called __reduce that is almost 9,000 lines long. Because the complexity limit is too low, in our fork of RustPython (which we’re trying to eliminate), we have to check in this extremely large generated file and hard-code a split-up of that __reduce function. It would be so nice to not have to do this.


Is it one of these two errors? ic/rs/types/wasm_types/src/errors.rs at master · dfinity/ic · GitHub

“Wasm module contains a function at index {} with complexity {} which exceeds the maximum complexity allowed {}”
“Wasm module contains a function at index {} of size {} that exceeds the maximum allowed size of {}”

1 Like

Yes I believe it’s similar to or that exact error you linked to. It’s specifically the complexity of the function, which is 16_722, and the complexity limit I believe is now set to 15_000.

Wasm module contains a function at index 0 with complexity 16722 which exceeds the maximum complexity allowed 15000
Deploying all canisters.
All canisters have already been created.
Building canisters...
Executing 'python -m kybra query src/main.py src/main.did'

Building canister query

[1/3] 🔨 Compiling Python... 5.0s
[2/3] 🚧 Building Wasm binary... 115.2s
[3/3] 🚀 Optimizing Wasm binary... 52.41s

🎉 Built canister query at .kybra/query/query.wasm.gz

Done in 172.93s.
Installing canisters...
Installing code for canister query, with canister ID rrkah-fqaaa-aaaaa-aaaaq-cai
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'query'.
      Failed to install wasm in canister 'rrkah-fqaaa-aaaaa-aaaaq-cai'.
        Failed to install wasm.
          The Replica returned an error: code 5, message: "Wasm module of canister rrkah-fqaaa-aaaaa-aaaaq-cai is not valid: Wasm module contains a function at index 0 with complexity 16722 which exceeds the maximum complexity allowed 15000"

Okay. I’ve asked if we can increase the limit further. This validation comes from the replica, not dfx, so even if it is feasible it will take some time before we will have a replica that we can package into a dfx update.


Our workaround will hold for a while, but would love to have this addressed as soon as feasible, thank you!

1 Like

those animations looks pretty cool.
can you contribute something similar to that for rust-cdk too?

Are there any news regarding increased complexity limit? Thanks

Do you still face this issue? It seems it was addressed a few months ago. The allowed complexity should be 1M now.

Well, we are using dfx 0.12.1 in one of our projects… Which is the minimal version to get higher than 15000?

The [9e220fc7a] Runtime: Expand complexity limit was released in rc–2023-08-23 (64016bcd) and was included in the dfx 0.15.0.