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

Use Box<[Type]> inside of FunctionType` #2036

Merged
merged 1 commit into from
Jan 22, 2021

Conversation

MarkMcCaskey
Copy link
Contributor

This saves 16 bytes (on 64bit systems) on the stack for each FunctionType and
additional space on the heap by not allowing resizing (like Vec does)

There are ways to save even more memory here but they're all heavier weight than this simple change.

This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and
additional space on the heap by not allowing resizing (like Vec does)
@syrusakbary
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Jan 21, 2021
2036: Use Box<[Type]>` inside of `FunctionType` r=syrusakbary a=MarkMcCaskey

This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and
additional space on the heap by not allowing resizing (like Vec does)

There are ways to save even more memory here but they're all heavier weight than this simple change.


Co-authored-by: Mark McCaskey <[email protected]>
params: params.into(),
results: returns.into(),
params: params.into().into_boxed_slice(),
results: returns.into().into_boxed_slice(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Rust can infer the target type of returns.into() to the temporary Vec<Type>? Impressive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, params and returns and generic and the only thing we know about these types is that they implement Into<Vec<Type>>, but I agree that it is nice!

@@ -229,9 +229,9 @@ impl ExternType {
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct FunctionType {
/// The parameters of the function
params: Vec<Type>,
params: Box<[Type]>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this affect serde somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't believe it should, but that's a good thing to check -- thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, checked the serde source code: they're both serialized as sequences, so there shouldn't be any effect on the serialization in any format

@bors
Copy link
Contributor

bors bot commented Jan 21, 2021

Build failed:

@syrusakbary
Copy link
Member

Can wait until we can use Github Actions in our own M1 machines

@MarkMcCaskey
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Jan 21, 2021
2036: Use Box<[Type]>` inside of `FunctionType` r=MarkMcCaskey a=MarkMcCaskey

This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and
additional space on the heap by not allowing resizing (like Vec does)

There are ways to save even more memory here but they're all heavier weight than this simple change.


Co-authored-by: Mark McCaskey <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jan 21, 2021

Build failed:

@MarkMcCaskey
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Jan 21, 2021
2036: Use Box<[Type]>` inside of `FunctionType` r=MarkMcCaskey a=MarkMcCaskey

This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and
additional space on the heap by not allowing resizing (like Vec does)

There are ways to save even more memory here but they're all heavier weight than this simple change.


Co-authored-by: Mark McCaskey <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jan 22, 2021

Build failed:

@syrusakbary
Copy link
Member

Merging manually since all tests pass except of the macos-11 due to issues in Github Actions (not in our codebase)

@syrusakbary syrusakbary merged commit a1043b7 into master Jan 22, 2021
@bors bors bot deleted the fix/waste-less-memory-with-function-type branch January 22, 2021 03:00
bors bot added a commit that referenced this pull request Feb 26, 2021
2141: feat(types) Avoid allocating a `Vec` when calling `FunctionType::new` r=Hywan a=Hywan

# Description

Sequel of #2036. We can go further by defining `Params` and `Results` of `FunctionType::new` as `Into<Box<[Type]>>` directly rather than `Into<Vec<Type>>`. It simplifies the code: `params.into()` rather than `params.into().into_boxed_slice()`. I assume it doesn't allocate an intermediate vector.

Since `From<Box<[T]>>` is implemented for `Vec<T>`, I reckon it's not a BC break.

# Review

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


Co-authored-by: Ivan Enderlin <[email protected]>
bors bot added a commit that referenced this pull request Mar 2, 2021
2141: feat(types) Avoid allocating a `Vec` when calling `FunctionType::new` r=syrusakbary a=Hywan

# Description

Sequel of #2036. We can go further by defining `Params` and `Results` of `FunctionType::new` as `Into<Box<[Type]>>` directly rather than `Into<Vec<Type>>`. It simplifies the code: `params.into()` rather than `params.into().into_boxed_slice()`. I assume it doesn't allocate an intermediate vector.

Since `From<Box<[T]>>` is implemented for `Vec<T>`, I reckon it's not a BC break.

# Review

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


Co-authored-by: Ivan Enderlin <[email protected]>
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