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

Reduce redundant closurizing when concatenating Arrays. #717

Merged
merged 6 commits into from
Jun 2, 2022

Conversation

fuzzypixelz
Copy link
Contributor

The important bit is the match arm in eval::eval_closure which uses a new attribute set in Array terms to remember when an array was closurized. This is then exploited in the ArrayConcat operation to avoid needlessly calling closurize on expressions of the form a1 @ a2 @ ... @ an.

In order to preserve (de)serialization of Arrays we need custom implementations like those of Num and Record; this is because serde will simply insert the extra attributes in the output and expect them in the input even though, in this case, they are just implementation details.

And because of this, testing is a bit more involved, for example the evaluation_full test initially failed for me because it compared equality of a parsing result (not closurized) to full evaluation result (closurized). If we write more of these kinds of tests, we may need some helper functions for this.

This makes it so array concatenation doesn't need to call closurize, by
introducing a flag in ArrayAttrs.

We still assume in most of the code that Arrays (especially thoses
generated by the interpreter) are not closurized, though there may be
room for improvement there.

In order to hide the added ArrayAttrs during serialization (and set it
to a sane default in deserialization) we need a custom implementation
like the ones for Num and Record.
This is mostly done for instances where we create Array Str; and for
when the interpreter inserts an already closurized Array.
@github-actions github-actions bot temporarily deployed to pull request June 1, 2022 09:04 Inactive
Copy link
Member

@yannham yannham 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! I think you don't need all this serde boilerplate in the end, and I let a few other nitpicks.

src/eval/merge.rs Outdated Show resolved Hide resolved
src/eval/operation.rs Show resolved Hide resolved
Comment on lines +126 to +151
/// Serialize for an Array. Required to hide the internal attributes.
pub fn serialize_array<S>(
terms: &Vec<RichTerm>,
_attrs: &ArrayAttrs,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(terms.len()))?;
for term in terms.iter() {
seq.serialize_element(term)?;
}

seq.end()
}

/// Deserialize for an Array. Required to set the default attributes.
pub fn deserialize_array<'de, D>(deserializer: D) -> Result<(Vec<RichTerm>, ArrayAttrs), D::Error>
where
D: Deserializer<'de>,
{
let terms: Vec<RichTerm> = Vec::deserialize(deserializer)?;
Ok((terms, Default::default()))
}

Copy link
Member

Choose a reason for hiding this comment

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

You may actually not need this code, because #[serde(skip)] seems to work for variant fields of tuple as well, so you should be able to do instead:

RichTerm { 
  ...
  Array(Vec<RichTerm>, #[serde(skip)] ArrayAttrs)
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm. It seems array serialization tests are failing, because of an extra [] added by serde.

So instead of { "bar": ["str", true] } w get { "bar": [ ["str", true] ] }. Maybe this happened because we now have a two-element tuple instead of one vector. I'll look into serde attributes.

src/term.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot temporarily deployed to pull request June 1, 2022 14:48 Inactive
@github-actions github-actions bot temporarily deployed to pull request June 1, 2022 14:54 Inactive
@fuzzypixelz
Copy link
Contributor Author

fuzzypixelz commented Jun 1, 2022

I think the benchmark runner still fails with the classic Unrecognized option: 'save-baseline' (it failed for 1256593). I looked at bheisler/criterion.rs#193 and it seems setting bench = false in lib and exes is enough, which is already there in Cargo.toml. Maybe we need to do it for NLS too? I'm not sure.

@github-actions github-actions bot temporarily deployed to pull request June 2, 2022 12:43 Inactive
@fuzzypixelz fuzzypixelz merged commit f7e0ced into master Jun 2, 2022
@fuzzypixelz fuzzypixelz deleted the array-closurization branch June 2, 2022 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants