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

Implement wasi-runtime-config #8950

Merged
merged 2 commits into from
Jul 17, 2024

Conversation

iawia002
Copy link
Contributor

Part of #8929, the integration of wasmtime-cli will be implemented in the next separate PR.

close #8939
close #8928

cc @alexcrichton

@iawia002 iawia002 requested review from a team as code owners July 13, 2024 06:21
@iawia002 iawia002 requested review from fitzgen and removed request for a team July 13, 2024 06:21
@iawia002 iawia002 force-pushed the wasi-runtime-config branch 2 times, most recently from db8c1a4 to 81842a5 Compare July 13, 2024 07:03
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks! This all looks good to me.

Could this also get hooked up to the CLI as well? Under perhaps -Sruntime-config and/or some other -S flags for the actual keys themselves?

crates/wasi-runtime-config/tests/main.rs Outdated Show resolved Hide resolved
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks! A bunch of stylistic/idiomatic/ergonomic suggestions and nitpicks below, but this looks good, and we should be able to land it shortly after those things are addressed.

ci/vendor-wit.sh Outdated Show resolved Hide resolved
Comment on lines 13 to 105
/// Capture the state necessary for use in the `wasi-runtime-config` API implementation.
pub struct WasiRuntimeConfigCtx {
runtime_config: HashMap<String, String>,
}

impl WasiRuntimeConfigCtx {
/// Create a new context.
pub fn new<S, I>(config: I) -> Self
where
S: Into<String>,
I: IntoIterator<Item = (S, S)>,
{
Self {
runtime_config: config
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
}
}
}

/// A wrapper capturing the needed internal `wasi-runtime-config` state.
pub struct WasiRuntimeConfigView<'a> {
ctx: &'a WasiRuntimeConfigCtx,
}

impl<'a> WasiRuntimeConfigView<'a> {
/// Create a new view into the `wasi-runtime-config` state.
pub fn new(ctx: &'a WasiRuntimeConfigCtx) -> Self {
Self { ctx }
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Is the idea here to separate the configured variables from the per-Store state, so that the variables can be reused across many Stores?

Can you document that fact?

Copy link
Contributor Author

@iawia002 iawia002 Jul 16, 2024

Choose a reason for hiding this comment

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

Is the idea here to separate the configured variables from the per-Store state, so that the variables can be reused across many Stores?

The idea here is very simple: it's just passing some configuration variables from the runtime to the component. I did add some documentation, but it is not described this way. Please check if the current description is reasonable. Feel free to add more comments.

crates/wasi-runtime-config/src/lib.rs Show resolved Hide resolved
crates/wasi-runtime-config/src/lib.rs Outdated Show resolved Hide resolved
crates/wasi-runtime-config/src/lib.rs Outdated Show resolved Hide resolved
crates/wasi-runtime-config/src/lib.rs Outdated Show resolved Hide resolved
crates/wasi-runtime-config/src/lib.rs Outdated Show resolved Hide resolved
/// Add all the `wasi-runtime-config` world's interfaces to a [`wasmtime::component::Linker`].
pub fn add_to_linker<T>(
l: &mut wasmtime::component::Linker<T>,
f: impl Fn(&mut T) -> WasiRuntimeConfigView + Send + Sync + Copy + 'static,
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this ... + 'static bound going to mean that you can effectively only use WasiRuntimeConfigView<'static>?

If so -- and if the 'static bound is necessary, which I think it is -- then instead of using lifetimes and borrows between WasiRuntimeConfigVariables and WasiRuntimeConfig, we should make WasiRuntimeConfig a newtype of Arc<WasiRuntimeConfigVariables>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The 'static bound is necessary, but this made me think further, since the wasi-runtime-config API is very simple, so I reduced some excessive encapsulation by removing WasiRuntimeConfigVariables/WasiRuntimeConfigCtx and keeping only WasiRuntimeConfig. Now, this problem should no longer exists.

Copy link
Member

Choose a reason for hiding this comment

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

The + 'static doesn't require <'static> in this case since the + 'static only applies to the lifetime of the captures of the closure itself. This I think was the desired signature which allows borrowing the config without cloning it on import calls.

@iawia002 iawia002 force-pushed the wasi-runtime-config branch from 81842a5 to d68819a Compare July 16, 2024 07:25
@iawia002
Copy link
Contributor Author

Could this also get hooked up to the CLI as well? Under perhaps -Sruntime-config and/or some other -S flags for the actual keys themselves?

I made some changes based on all the comments except this one, because I plan to add support for the wasi-runtime-config API to wasmtime-cli in a separate PR. Otherwise, this PR would have too many changes, let's focus on the wasi-runtime-config implementation only in this PR.

And I think all comments have been addressed, PTAL.

/// Add all the `wasi-runtime-config` world's interfaces to a [`wasmtime::component::Linker`].
pub fn add_to_linker<T>(
l: &mut wasmtime::component::Linker<T>,
f: impl Fn(&mut T) -> WasiRuntimeConfig + Send + Sync + Copy + 'static,
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that this is the signature that you want because it would imply that every time an import is called it would .clone() the entire WasiRuntimeConfig structure which could be pretty expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you are right, I did not understand it correctly last time. I made some changes (99b6a11) and returned to the original WasiRuntimeConfigVariables + WasiRuntimeConfig style. Hope I have understood it correctly this time.

/// Add all the `wasi-runtime-config` world's interfaces to a [`wasmtime::component::Linker`].
pub fn add_to_linker<T>(
l: &mut wasmtime::component::Linker<T>,
f: impl Fn(&mut T) -> WasiRuntimeConfigView + Send + Sync + Copy + 'static,
Copy link
Member

Choose a reason for hiding this comment

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

The + 'static doesn't require <'static> in this case since the + 'static only applies to the lifetime of the captures of the closure itself. This I think was the desired signature which allows borrowing the config without cloning it on import calls.

@alexcrichton alexcrichton added this pull request to the merge queue Jul 17, 2024
Merged via the queue into bytecodealliance:main with commit 0a296b3 Jul 17, 2024
37 checks passed
@fitzgen
Copy link
Member

fitzgen commented Jul 17, 2024

Thanks!

@iawia002 iawia002 deleted the wasi-runtime-config branch July 18, 2024 00:58
@iawia002
Copy link
Contributor Author

Thanks for the review!

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

Successfully merging this pull request may close these issues.

3 participants