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

Add tree_pos to tsk_tree_t struct #2867

Closed
wants to merge 3 commits into from

Conversation

duncanMR
Copy link
Contributor

@duncanMR duncanMR commented Nov 1, 2023

Description

This PR aims to address the first part of #2795 by adding the tree_position class to the tsk_tree_t struct. I've done a first draft of this and it passes all the unit tests without any memory issues detected by Valgrind. I'm not confident that the memory management is correct yet, though!

PR Checklist:

  • Tests that fully cover new/changed functionality.
  • Documentation including tutorial content if appropriate.
  • Changelogs, if there are API changes.

@duncanMR
Copy link
Contributor Author

duncanMR commented Nov 1, 2023

@jeromekelleher I see why you made the comment about doxygen in the trees header file: moving tsk_position_t above tsk_tree_t broke the docs!

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Merging #2867 (b72b3c2) into main (813f4ed) will decrease coverage by 12.68%.
The diff coverage is 50.00%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2867       +/-   ##
===========================================
- Coverage   89.69%   77.02%   -12.68%     
===========================================
  Files          30       30               
  Lines       30159    30146       -13     
  Branches     5860     5596      -264     
===========================================
- Hits        27052    23220     -3832     
- Misses       1778     5538     +3760     
- Partials     1329     1388       +59     
Flag Coverage Δ
c-tests 86.07% <50.00%> (-0.03%) ⬇️
lwt-tests 80.78% <ø> (ø)
python-c-tests 67.89% <ø> (ø)
python-tests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
c/tskit/trees.c 90.13% <50.00%> (-0.04%) ⬇️

... and 16 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 813f4ed...b72b3c2. Read the comment docs.

Copy link
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

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

Yep, good start. Memory management isn't quite right, as you say, but easily fixed.

c/tskit/trees.c Outdated
@@ -4520,12 +4520,14 @@ tsk_tree_init(tsk_tree_t *self, const tsk_treeseq_t *tree_sequence, tsk_flags_t
{
int ret = TSK_ERR_NO_MEMORY;
tsk_size_t num_samples, num_nodes, N;
tsk_tree_position_t tree_pos;
Copy link
Member

Choose a reason for hiding this comment

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

Don't bother making a separate tree_pos struct here, it won't make any difference. One thing to realise is that

tsk_memset(self, 0, sizeof(tsk_tree_t));

will also zero out the embedded tree_position_t struct.

c/tskit/trees.c Outdated
@@ -4565,6 +4568,11 @@ tsk_tree_init(tsk_tree_t *self, const tsk_treeseq_t *tree_sequence, tsk_flags_t
goto out;
}
}

ret = tsk_tree_position_init(&tree_pos, tree_sequence, 0);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ret = tsk_tree_position_init(&tree_pos, tree_sequence, 0);
ret = tsk_tree_position_init(&self->tree_pos, tree_sequence, 0);

c/tskit/trees.c Outdated
@@ -4613,6 +4621,7 @@ tsk_tree_free(tsk_tree_t *self)
tsk_safe_free(self->next_sample);
tsk_safe_free(self->num_children);
tsk_safe_free(self->edge);
tsk_safe_free(self->tree_pos);
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong , and I'm surprised valgrind doesn't catch it. The golden rule is, you only free a pointer than you have previously malloced. The tree_pos here is an embedded struct, so it's not a pointer that can be freed.

You should be calling tsk_tree_position_free(&self->tree_pos); instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do: thanks for the tip! At the moment, tsk_tree_position_free doesn't do anything:

int
tsk_tree_position_free(tsk_tree_position_t *TSK_UNUSED(self))
{
    return 0;
}

Was this just a placeholder, or is there nothing we need to free?

Copy link
Member

Choose a reason for hiding this comment

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

It's just a placeholder, better to have it in client code and use it in case we end up doing some mallocing later. Writing good C code depends a lot on following conventions, and it's much easier to just follow a given set of conventions than to think locally about how things should be done.

c/tskit/trees.h Outdated
const tsk_treeseq_t *tree_sequence;
} tsk_tree_position_t;

int tsk_tree_position_init(
Copy link
Member

Choose a reason for hiding this comment

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

Methods are declared further down the file, separate from struct definitions. Just move the function signatures to the bottom of the file.

@duncanMR
Copy link
Contributor Author

duncanMR commented Nov 6, 2023

@jeromekelleher, I've added a commit with all your suggested changes. The docs are still failing after moving all the methods to the bottom of trees.h. Doxygen runs successfully, and it seems like the xml file for the tsk_tree_t class it generates is correct. Also, the tree section of docs/_build/html/c-api.html looks correct to me; I'm confused as to what is causing the warning.

@jeromekelleher
Copy link
Member

I think the docs build is working @duncanMR - I just checked it out and the build is fine for me. Also the CI docs build is working.

I think the CI failures are due to a stale package cache. Probably simplest to just start a new PR from a fresh branch.

@duncanMR
Copy link
Contributor Author

duncanMR commented Dec 4, 2023

I think the docs build is working @duncanMR - I just checked it out and the build is fine for me. Also the CI docs build is working.

I think the CI failures are due to a stale package cache. Probably simplest to just start a new PR from a fresh branch.

Yes, I figured out the docs problem: there is a hardcoded exception in the Sphinx config specifically for the tree class that needed an update. I'll make a new PR now

@jeromekelleher
Copy link
Member

I just saw a "merge" here @duncanMR - merge is not your friend! Rebase rebase rebase

@duncanMR
Copy link
Contributor Author

duncanMR commented Dec 5, 2023

@jeromekelleher Sorry about that! I accidentally pulled changes instead of just fetching. Will be more careful in future

@jeromekelleher
Copy link
Member

@jeromekelleher Sorry about that! I accidentally pulled changes instead of just fetching. Will be more careful in future

No need to apologise, just trying to help

@duncanMR duncanMR closed this Dec 5, 2023
@duncanMR duncanMR deleted the tree_pos_revision branch December 5, 2023 14:48
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.

2 participants