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

Improve tree/node copying #57

Merged
merged 1 commit into from Nov 5, 2022
Merged

Improve tree/node copying #57

merged 1 commit into from Nov 5, 2022

Conversation

renatahodovan
Copy link
Owner

The patch removes the superfluous copy/deepcopy methods from tree nodes, since they only called the default implementation. On the other hand, it defines the way of deepcopying a whole tree, which means deepcopying the root node and annotate the new tree.

@coveralls
Copy link

coveralls commented Nov 3, 2022

Coverage Status

Coverage decreased (-0.04%) to 72.018% when pulling 3d522d5 on copy into 21a4fc1 on master.

@@ -19,6 +19,11 @@ def __init__(self, root):
self.root = root
self.node_dict = None

def __deepcopy__(self, memo):
tree = Tree(deepcopy(self.root))
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't memo be passed on to deepcopy?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm not sure. deepcopy(self.root) creates a deepcopy of a tree hierarchy rooted from self.root which is different from the Tree object. It calls the default deepcopy implementation which initiates memo to an empty dict if it's not defined, which is perfect for us. Besides, these tree hierarchies must not contain recursive loops anyhow (except if they were manually crafted so, but that's a bad design and it will fail a lot of other ways probably).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, as the current implementation stands, memo may not be of big use. But what can go wrong if we do pass on memo? Wouldn't it make the implementation more future-proof? (Btw, loops are not the only construct where memo can help. DAGs may also benefit from memo, if I get it right. But certainly, we cannot have DAGs either at the moment.)

Copy link
Owner Author

Choose a reason for hiding this comment

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

We can do that, it won't cause any harm indeed (since it will execute exactly the same code path as now). But once again, the current default implementation - which initiates the unset memo parameter to dict - also handles a possible recursion (or even DAGs).

The patch removes the superfluous copy/deepcopy methods from tree
nodes, since they only called the default implementation.
On the other hand, it defines the way of deepcopying a whole tree,
which means deepcopying the root node and annotate the new tree.
Copy link
Collaborator

@akosthekiss akosthekiss left a comment

Choose a reason for hiding this comment

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

lgtm

@akosthekiss akosthekiss merged commit 7125c85 into master Nov 5, 2022
@renatahodovan renatahodovan deleted the copy branch November 5, 2022 15:11
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.

None yet

3 participants