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

Remove recursion in semantic::Policy::normalized #614

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

tcharding
Copy link
Member

The last 5 patches, the rest is from #612

Make sure you have a cup of tea to review this one, its pretty wild - took all of my brain power to work this out.

@tcharding tcharding force-pushed the 10-12-rm-recursion-normalized branch from db50bf1 to 333682e Compare October 12, 2023 03:41
@tcharding
Copy link
Member Author

Fuzz fail looks real, this must be buggy. Converting to draft.

@tcharding tcharding marked this pull request as draft October 12, 2023 04:21
Remove unnecessary code comment, the local variables names are clear enough.
This function is fiendishly hard to understand, add a line of whitespace
as a tiny baby step towards making it clearer.
Currently we are using `map_or` in a convoluted manner, we can just use
`unwrap_or` to get the inner value or default to 0.

Refactor only, no logic changes.
We have a little typo in an error string, fix it up.
Use standard from for import statements. (Note `Arc` is already in scope
in `super`.)
When we implemented the `TreeLike` trait for the concrete `Policy` we
put it in the `iter` module, it doesn't really live there, better to put
the impl in the same place as the definition of the implementor.

Move the impl of `TreeLike` for `concrete::Policy` to the `concrete`
module.
In preparation for removing recursive algorithms in the `semantic`
module add an `Arc` wrapper around the policies in the `Thresh` vector.

Note we use the more explicit `Arc::new` instead of `into` because a lot
of these should be removed when we remove recursion, later it may be
necessary to check that we are not creating new references when a clone
will suffice, the explicit `Arc::new` is easier to check. In tests
however just use `into`.
In preparation for removing recursive algorithms in the `semantic`
module implement the `TreeLike` trait to enable iteration over policy
nodes.

This is a direct copy of the `concrete::Policy` impl with the `And` and
`Or` variants removed.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::for_each_key`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::translate_pk`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::n_terminals`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in the `semantic::Policy::real_*_timelocks` functions.

Done together because they are basically identical.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::at_age` and
`semantic::Policy::at_age`.

Done together because they are basically identical.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::n_keys`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::minimum_n_keys`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::sorted`.
The `semantic::Policy::normalized` function is recursive and has nested
processing of `Threshold`s. It is not trivial to fully grok.

The function is already tested elsewhere in the unit tests but we don't
have simple single purpose tests for it.

Add various unit tests for `normalized`, these serve as documentation of
the function as much as anything.
Currently we are creating a new `Arc<Policy::Threshold>` from the pieces
of `sub` that we pattern matched on. We can just clone the original
`sub` instead.
In `normalized` we process the threshold policy as well as its child
threshold policy and we shadow `subs` - this is all very confusing. Add
parent/child terminology and a couple extra local variables in an
attempt to make the code more clear.

Refactor only, no logic changes.
The `normalize` function spans more than a screen (on my monitor), as
such it is hard to work on.

Refactor out the (m, n)-thresh calculation to a function so that the
body of the function becomes shorter.

Refactor only, no logic changes.
Remove the recursive call in `semantic::Policy::normalized`, using
the `TreeLike` trait to iterate nodes instead.
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.

1 participant