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

Add support for const function signatures #1911

Merged
merged 3 commits into from
Dec 14, 2020

Conversation

webmaster128
Copy link
Contributor

@webmaster128 webmaster128 commented Dec 10, 2020

Description

FunctionType contains variable length vectors and I'm sure there is a good reason for this design. However, it makes creating them a bit annoying since it connot be done in constants. With this PR I propose a representation that uses a tuple of arrays, which can be converted into FunctionType on demand. This allows developers to use constant signatures and push them to a better place in their code.

In https://github.com/CosmWasm/cosmwasm/pull/659/files you see a demonstration of how this deduplicates code and pushes the definitions out of my way.

Since the conversion &FunctionType to FunctionType is implemented here, this should no be breaking anyone's code. In cases where you can pass an owned FunctionType instead of &FunctionType you save one clone.

Review

  • Add a short description of the the change to the CHANGELOG.md file

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. I don't see exactly how it improves Wasmer itself, but I can understand how it can be convenient for the user!

I'm approving this, but I would appreciate another feedback on this from @syrusakbary, @nlewycky, @MarkMcCaskey or @jubianchi before merging :-).

lib/api/src/externals/function.rs Outdated Show resolved Hide resolved
lib/api/src/externals/function.rs Outdated Show resolved Hide resolved
Comment on lines +306 to +310
impl From<&FunctionType> for FunctionType {
fn from(as_ref: &FunctionType) -> Self {
as_ref.clone()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Hywan Hywan self-assigned this Dec 11, 2020
@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-types About wasmer-types 🧪 tests I love tests labels Dec 11, 2020
Copy link
Member

@syrusakbary syrusakbary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@syrusakbary
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Dec 11, 2020
1911: Add support for const function signatures r=syrusakbary a=webmaster128

# Description

`FunctionType` contains variable length vectors and I'm sure there is a good reason for this design. However, it makes creating them a bit annoying since it connot be done in constants. With this PR I propose a representation that uses a tuple of arrays, which can be converted into `FunctionType` on demand. This allows developers to use constant signatures and push them to a better place in their code.

In https://github.com/CosmWasm/cosmwasm/pull/659/files you see a demonstration of how this deduplicates code and pushes the definitions out of my way.

Since the conversion `&FunctionType` to `FunctionType` is implemented here, this should no be breaking anyone's code. In cases where you can pass an owned `FunctionType` instead of `&FunctionType` you save one clone.

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Simon Warta <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 11, 2020

Build failed:

@webmaster128
Copy link
Contributor Author

yo bors, I rebased this PR to lastest master and fixed the compile error

webmaster128 added a commit to CosmWasm/cosmwasm that referenced this pull request Dec 14, 2020
@Hywan
Copy link
Contributor

Hywan commented Dec 14, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 14, 2020

Merge conflict.

@Hywan
Copy link
Contributor

Hywan commented Dec 14, 2020

bors r+

bors bot added a commit that referenced this pull request Dec 14, 2020
1911: Add support for const function signatures r=Hywan a=webmaster128

# Description

`FunctionType` contains variable length vectors and I'm sure there is a good reason for this design. However, it makes creating them a bit annoying since it connot be done in constants. With this PR I propose a representation that uses a tuple of arrays, which can be converted into `FunctionType` on demand. This allows developers to use constant signatures and push them to a better place in their code.

In https://github.com/CosmWasm/cosmwasm/pull/659/files you see a demonstration of how this deduplicates code and pushes the definitions out of my way.

Since the conversion `&FunctionType` to `FunctionType` is implemented here, this should no be breaking anyone's code. In cases where you can pass an owned `FunctionType` instead of `&FunctionType` you save one clone.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Simon Warta <[email protected]>
Co-authored-by: Ivan Enderlin <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 14, 2020

Build failed:

@webmaster128
Copy link
Contributor Author

Oh, the failing test now shows we are using some &&wasmer::FunctionType. I guess this is not intended and should be &wasmer::FunctionType, right?

error[E0277]: the trait bound `wasmer::FunctionType: std::convert::From<&&wasmer::FunctionType>` is not satisfied
   --> lib/c-api/src/wasm_c_api/externals/function.rs:81:20
    |
81  |     let function = Function::new(&store.inner, &func_sig, inner_callback);
    |                    ^^^^^^^^^^^^^ the trait `std::convert::From<&&wasmer::FunctionType>` is not implemented for `wasmer::FunctionType`
    | 
   ::: /home/runner/work/wasmer/wasmer/lib/api/src/externals/function.rs:102:13
    |
102 |         FT: Into<FunctionType>,
    |             ------------------ required by this bound in `wasmer::Function::new`
    |
    = help: the following implementations were found:
              <wasmer::FunctionType as std::convert::From<&wasmer::FunctionType>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 0])>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 1])>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 2])>>
            and 97 others
    = note: required because of the requirements on the impl of `std::convert::Into<wasmer::FunctionType>` for `&&wasmer::FunctionType`

error[E0277]: the trait bound `wasmer::FunctionType: std::convert::From<&&wasmer::FunctionType>` is not satisfied
   --> lib/c-api/src/wasm_c_api/externals/function.rs:155:20
    |
155 |     let function = Function::new_with_env(
    |                    ^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<&&wasmer::FunctionType>` is not implemented for `wasmer::FunctionType`
    | 
   ::: /home/runner/work/wasmer/wasmer/lib/api/src/externals/function.rs:178:13
    |
178 |         FT: Into<FunctionType>,
    |             ------------------ required by this bound in `wasmer::Function::new_with_env`
    |
    = help: the following implementations were found:
              <wasmer::FunctionType as std::convert::From<&wasmer::FunctionType>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 0])>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 1])>>
              <wasmer::FunctionType as std::convert::From<([wasmer::ValType; 0], [wasmer::ValType; 2])>>
            and 97 others
    = note: required because of the requirements on the impl of `std::convert::Into<wasmer::FunctionType>` for `&&wasmer::FunctionType`

error: aborting due to 2 previous errors

@webmaster128
Copy link
Contributor Author

I'll take care of this now

@webmaster128
Copy link
Contributor Author

Rebased to latest master and 1 commit added (8a8e8dc)

@Hywan
Copy link
Contributor

Hywan commented Dec 14, 2020

bors try

bors bot added a commit that referenced this pull request Dec 14, 2020
@bors
Copy link
Contributor

bors bot commented Dec 14, 2020

@MarkMcCaskey
Copy link
Contributor

bors r+

@webmaster128
Copy link
Contributor Author

Could you restart this job? https://github.com/wasmerio/wasmer/runs/1551129968 Seems like it never started

@bors
Copy link
Contributor

bors bot commented Dec 14, 2020

@bors bors bot merged commit ae9a5f0 into wasmerio:master Dec 14, 2020
@webmaster128 webmaster128 deleted the const-FunctionType branch December 14, 2020 16:51
@MarkMcCaskey
Copy link
Contributor

Random follow up but I wanted to ping you, @webmaster128 , to make sure you know about NativeFunc: if you know the signature of your host function at compile time, you can avoid specifying the type signature of your host function entirely by using Function::new_native and Function::new_native_with_env and it should have less runtime overhead.

We just had another issue where someone missed that, so I wanted to make sure you knew about it too in case the docs aren't clear here.

@webmaster128
Copy link
Contributor Author

Thank you @MarkMcCaskey, much appreciated. This was not clear to me indeed for a long time, especially when upgrading from statically typed Func closures in 0.17 to the new API. Now I already migrated all functions to native that do not capture variables. One function could be refactored to storing a variable in Env instead of capturing. And some are remaining to be refactored from capturing closures to native functions accessing data via Env.

@webmaster128
Copy link
Contributor Author

I'm very open to discuss if this PR is more destructive than helpful. Does it encourage users to follow the wrong path? Could all those testing dummies be replaced with statically typed closures? CosmWasm/cosmwasm@65a7d1f

@MarkMcCaskey
Copy link
Contributor

@webmaster128 Yeah, it seems like those examples could be made into "native" functions if you wanted to! Don't worry about it, I think this PR is a good change, it's nice to have the ability to avoid heap allocations. I've updated the docs locally to make it more clear what the options are!

Just wanted to make sure you knew about it in case you didn't!

@webmaster128
Copy link
Contributor Author

Alright cool. One thought why this might be tricky for people who are migrating: it seems like the default chained from native to dynamic. To me Function::new feels like the direct replacement of Func::new. But it looks like you got a native function in 0.17 and get a dynamic function in 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-types About wasmer-types 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants