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

ink_codec library to use dynamic dispatch instead of monomorphization #973

Open
xgreenx opened this issue Oct 21, 2021 · 1 comment
Open

Comments

@xgreenx
Copy link
Collaborator

xgreenx commented Oct 21, 2021

As part of our grant, we want to help to reduce the size of contracts. We analyzed the binary(web assembler) file and found that ink! generates many the same code in the case of scale::Decode and scale::Encode.

So, we decided to create the ink_codec library which allows us to use it in a dynamic dispatch manner.

Example of traits:

pub trait Encodable {
    /// Convert self to an owned vector.
    fn encode(&self) -> Vec<u8> {
        let mut dest = Vec::new();
        self.encode_to(&mut dest);
        dest
    }

    /// Convert self to a slice and append it to the destination.
    fn encode_to(&self, dest: &mut dyn Output);
}

pub trait Decodable {
    /// Attempt to deserialize the value from input.
    /// 
    /// # Note
    /// The object must exist before calling the `decode`.
    fn decode(&mut self, input: &mut dyn Input) -> Result<(), Error>;
}

We've already started working on this change(it is based on the code of release 3.0-rc5). This issue is needed to avoid duplication of work(as it was with #945) and track the progress. After the implementation of the change, we will provide a report about the size of ERC-20(and maybe other heavy contracts from the example folder) and the source code. If the result is good and you agree with our idea, we will open a pull request.

@Robbepop
Copy link
Collaborator

Note that dynamic dispatch prevents tons of optimizations that the compiler usually can perform with static dispatch and inlined functions. So inlining usually is not only evil with respect to Wasm file sizes but might even be superior to what you save through dynamic dispatch and non-inlining.
Still, I am looking forward to your research results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants