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 more recursion from concrete::policy #606

Merged

Conversation

tcharding
Copy link
Member

Remove the last of the recursion in functions that process policies. Note there is still recursion in the from_tree_prob function that creates the policies.

Currently the `is_valid` function recursively calls itself, we can use
the `pre_order_iter` to loop over the policy nodes instead and remove
the recursion.

Note that this whole `is_valid` function is pretty inefficient because
we have already iterated the nodes twice already (in `check_timelocks`
and `check_duplicate_keys`).
As we have done for other `Policy` functions remove the recursive calls
in `is_safe_nonmalleable` and use the `post_order_iter` to process each
node accumulating the required results in a vector during iteration.
Currently there are a bunch of places where when matching on the
`Policy` enum variants are matched in different order to the enum.
During recent work this was maintained to make diffs easier to review
however since we just modified a lot of this file lets clean it up while
we are here.

Move the match arms around so that they are ordered in the same order as
the variants are defined in the `Policy` enum.

Refactor only, no logic changes.
@apoelstra
Copy link
Member

Looks great! Interesting observation that we're iterating thrice in is_valid (and each one potentially is allocating and deallocating vectors to deal with threshold branches).

I think this is fine, because improving this would result in much uglier code... but we should keep this in the back of our minds, and if it's a common pattern maybe we should try to refactor it out somehow.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 0452040

@tcharding
Copy link
Member Author

tcharding commented Oct 3, 2023

Yeah this is where my question the other day about why we call is_valid came from because not only is this function inefficient, its called often. I think improving the types helps because I think we can get rid one out of three tree iterations - I agree doing it all in one is likely ugly.

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

utACK 0452040

@apoelstra apoelstra merged commit 1ebde2d into rust-bitcoin:master Oct 6, 2023
@tcharding tcharding deleted the 10-02-is-valid-rm-recursion branch October 9, 2023 18:02
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