Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research reducing monomorphization bloat by delegating to non-generic implementations #910

Open
Tracked by #9354
cmichi opened this issue Sep 9, 2021 · 6 comments
Labels
A-ink_storage [ink_storage] Work Item B-enhancement New feature or request B-research Research task that has open questions that need to be resolved. C-discussion An issue for discussion for a given topic. P-high High priority issue.

Comments

@cmichi
Copy link
Collaborator

cmichi commented Sep 9, 2021

These two blog posts describe techniques for reducing monomorphization bloat by having only a thin generic interface layer, which immediately delegates to a non-generic implementation:

https://llogiq.github.io/2019/05/18/momo.html

https://matklad.github.io/2021/09/04/fast-rust-builds.html

The Hackernews comments also contain some good ideas: https://news.ycombinator.com/item?id=28425095.

There is also the momo crate, which implements a macro for this technique.

We should look into if this is applicable to ink!.

@cmichi cmichi added B-enhancement New feature or request A-ink_storage [ink_storage] Work Item C-discussion An issue for discussion for a given topic. B-research Research task that has open questions that need to be resolved. labels Sep 9, 2021
@Robbepop
Copy link
Collaborator

We might also find a lot of use for this technique in heavily used derive dependencies such as parity-scale-codec.

@cmichi
Copy link
Collaborator Author

cmichi commented Sep 13, 2021

We might also find a lot of use for this technique in heavily used derive dependencies such as parity-scale-codec.

That's a good point. And also a reminder of why the compiler team efforts are necessary despite any of the changes we will make to ink! (i.e. third party crates which have monomorphization bloat).

@ascjones
Copy link
Collaborator

I always wondered what that fn inner was about! TIL

@cmichi cmichi added the P-high High priority issue. label Sep 13, 2021
@xgreenx
Copy link
Collaborator

xgreenx commented Sep 13, 2021

What do you think about rewriting the parity-scale-codec and some traits/logic(SpreadLayout, StorageLayout and etc) in the ink! to use dynamic dispatching instead of monomorphization?

I think that the combination of #916 really will reduce the size of the contract.

BTW, adding the getter/setter/method to the contract adds ~0.3kb, maybe we also can try to reduce code-generation for call.

@Robbepop
Copy link
Collaborator

Robbepop commented Sep 14, 2021

Dynamic dispatch is not always the cure but also brings plenty of problems with it sometimes.
For example it is much harder for the compiler to optimize across function boundaries and therefore sometimes dynamic calls can even lead to more produced binary code in the worst case.
In ink! we profited a lot by producing code that can be heavily optimized by the compiler which can be observed by the fact that very small and contained smart contracts are compiled down to roughly 1kB which is close to optimal.
I am sure that with some other adjustments following the removal of state rent and some other techniques to tear down some monomorphisation bloat without reducing optimization potential we can gain a lot of ground for more complex smart contracts.

We see the biggest bloat in the ink_storage facilities due to the fact that they have been written for automatic removal of contract storage which no longer is a declared goal with removal of state rent. Also caching sometimes hurts the performance instead of helping it. We will push forward by rewriting some of our ink_storage facilities and simplify them as well as introducing new ones with even less overhead similar to the ones provided by Solidity.

@athei
Copy link
Contributor

athei commented Sep 17, 2021

Just linking #873 (comment) for reference. This is a method to check for monomorphization bloat. Maybe we can even add convenience command to cargo contract?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ink_storage [ink_storage] Work Item B-enhancement New feature or request B-research Research task that has open questions that need to be resolved. C-discussion An issue for discussion for a given topic. P-high High priority issue.
Projects
None yet
Development

No branches or pull requests

5 participants