# Motoko Wasm: Repeated Candid declarations

**URL:** <https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306>\
**Category:** Developers\
**Tags:** candid\
**Created:** [January 31, 2023, 3:46pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306 "2023-01-31T15:46:33Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [January 31, 2023, 3:46pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/1 "2023-01-31T15:46:33Z")

</div>

We have a large project that has been refactored a little bit into a good number of type, interface, etc modules.

Our wasm is getting big. When you drag it into the wasm editor it seems that there is a bunch of candid that is repeated over and over:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/5/5a43eb137f23b22cfe7467463c5fba45d1bf95bf.png)

This candid text seems to be repeated 32 times in the file. It would be great to:

1. Know how to keep this from happening
2. Maybe have the compiler be smart enough to know that it already has this text in the wasm somewhere and not repeat it.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [January 31, 2023, 7:12pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/2 "2023-01-31T19:12:22Z")

</div>

Does that code use actor classes? Each class will contain its own description of its candid interface. Also, there may be some duplication of types in the stable variable sections too

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [January 31, 2023, 7:15pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/3 "2023-01-31T19:15:06Z")

</div>

If possible, can you share the wasm for me to look at? Or point at the repo?

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [January 31, 2023, 7:22pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/4 "2023-01-31T19:22:54Z")

</div>

It is the same repo as last week. I’ll dm the branch.

You can just build one item in dfx and then inspect the wasm.

@ZhenyaUsenko Tried replacing some of the actor references with interface references that had imports and the file got BIGGER. Then he ran the distilled candid through the did → motoko to get rid of the imports and it got bigger again.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [January 31, 2023, 8:46pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/5 "2023-01-31T20:46:22Z")

</div>

Actually, I totally forgot (or did not know) that we have a compiler option to omit metadata sections, e.g. `--omit-metadata candid:args` (or perhaps `--omit-metadata "candid:args"`) should do just that.

Probably worth an experiment to see if it cuts down on binary size significantly, but omitting other meta data might make upgrading a little scary… (since dfx will have nothing to check against).

```auto
[nix-shell:~/motoko/doc]$ moc --help
...
  --omit-metadata <name> omit icp custom section <name> (candid:args or candid:service or motoko:stable-types or motoko:compiler)
...

```

---

<div class="post-metadata">

**Author:** ![ZhenyaUsenko](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zhenyausenko/32/12200_2.png) [@ZhenyaUsenko](https://forum.dfinity.org/u/ZhenyaUsenko)\
**Post date:** [February 1, 2023, 9:11am UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/6 "2023-02-01T09:11:36Z")

</div>

no\_args - 11\_256 KB  
candid:args - 11\_254 KB  
candid:service - 11\_150 KB  
motoko:stable-types - 11\_086 KB  
motoko:compiler - 11\_254 KB

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 11:11am UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/7 "2023-02-01T11:11:13Z")

</div>

Thanks, but what are those numbers exactly? The total number of bytes in each kind of section or something else? The last one (motoko:compiler) should be much, much smaller

---

<div class="post-metadata">

**Author:** ![ZhenyaUsenko](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zhenyausenko/32/12200_2.png) [@ZhenyaUsenko](https://forum.dfinity.org/u/ZhenyaUsenko)\
**Post date:** [February 1, 2023, 11:25am UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/8 "2023-02-01T11:25:50Z")

</div>

Wasm size in KB (updated the comment)

I rechecked **motoko:compiler**. Got the same size

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 12:20pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/9 "2023-02-01T12:20:13Z")

</div>

Isn’t that 11MB way over the 2MB (or is 3MB?) binary size limit, regardless of metadata size?

---

<div class="post-metadata">

**Author:** ![ZhenyaUsenko](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zhenyausenko/32/12200_2.png) [@ZhenyaUsenko](https://forum.dfinity.org/u/ZhenyaUsenko)\
**Post date:** [February 1, 2023, 1:59pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/10 "2023-02-01T13:59:02Z")

</div>

It is 3.1 MB when gzipped

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [February 1, 2023, 2:27pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/11 "2023-02-01T14:27:05Z")

</div>

We are getting close to the limit of where we’ll be able to deploy. It would be good to find the culprit so we can engineer for it.

I’m thinking we may need to end up having “factory” canistors that only reference one actor and are asked to deploy and transfer control to our management canisters so that it doesn’t have to reference the actors directly. This would be unnecessary complexity but may become necessary unless we can identify an optimization.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 3:05pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/12 "2023-02-01T15:05:12Z")

</div>

I was thinking of suggesting that solution too, but now that I’ve managed to build your project and look at the wasm, I’m beginning to wonder if we don’t actually have a bug in the code generator.

Your code seems to import 6 or so actor classes, non-recursively as far as I can tell, and that doesn’t seem to match the number of meta-data sections I’m seeing in the binary. So I’m wondering if we are indeed duplicating string constants, perhaps by forgetting to purge the constant pool when we do a recursive build of an actor class.

I’ll investigate some more.

Sorry to be so slow.

-c

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 4:39pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/13 "2023-02-01T16:39:12Z")

</div>

Ok, I think I know what might be going on.

I suspect the compiler is linearizing the dependencies between libraries for the entire project, but when it compiles an imported actor class, it’s including all the (preceding) libraries in the global order, not just this class’s actual dependencies, leading to duplication of code.

So when you import a sequence of actor classes, each class will import all of the preceeding ones, not just the ones it actually needs.

This is pretty bad, but won’t be a super-quick fix.

@nomeata does that seem like a reasonable diagnosis?

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 7:16pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/14 "2023-02-01T19:16:09Z")

</div>

Ok, I think I actually have a fix that works. Might make it into 0.8.1.

It reduces the size of the wasm to 3MB, which gzips (after wasm-shrink) to 600K.  
Not too shabby (assuming it still works correctly).

The binary also now has the expected number of meta-data sections (just one per imported actor class plus the one for the main actor, for each sort of section).

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 7:17pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/15 "2023-02-01T19:17:33Z")

</div>

@ZhenyaUsenko @skilesare thank you very much for taking the time to report this!

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 1, 2023, 7:42pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/16 "2023-02-01T19:42:47Z")

</div>

[bugfix: fix for quadratic import of actor classes by crusso · Pull Request #3758 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/pull/3758) is the work in progress PR fixing this.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [February 1, 2023, 10:44pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/17 "2023-02-01T22:44:51Z")

</div>

![](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/3/365db1a309bd848683b23296b84dab26b2982e2d.gif)  
Celebrate the victories!!!

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [February 3, 2023, 8:36pm UTC](https://forum.dfinity.org/t/motoko-wasm-repeated-candid-declarations/18306/18 "2023-02-03T20:36:06Z")

</div>

Moc 0.8.1 has been released and should fix this issue.
