Skip to content

constify C Table API - #919

Merged
mergify[bot] merged 1 commit into
tskit-dev:mainfrom
castedo:pr/const_c_table_api
Oct 26, 2020
Merged

mergify[bot] merged 1 commit into
tskit-dev:mainfrom
castedo:pr/const_c_table_api

Conversation

@castedo

@castedo castedo commented Oct 21, 2020

Copy link
Copy Markdown
Member

As a first stage for #844 I am focusing on the Table API which does not depend on other parts of the C API.

I will probably have this "constification" of the Table API ready today.

I'm not sure how long it will take the do the rest of the C API yet. But this topic branch focused on only the Table API could be merged before the rest of the C API is constified.

@castedo
castedo marked this pull request as draft October 21, 2020 12:18
@AdminBot-tskit

Copy link
Copy Markdown
Collaborator

📖 Docs for this PR can be previewed here

@castedo

castedo commented Oct 21, 2020

Copy link
Copy Markdown
Member Author

There are a few more functions in the Table API that I am still investigating. Specifically the _get_row( functions which set (return) row structures. At the moment two of these row structure types, namely tsk_individual_t and tsk_site_t look like they maybe can be used to modify the contents of the table those rows "point" to. Depending on the intended semantics, thos _get_row functions maybe should be constafied too.

@castedo castedo changed the title constify clearly consty Table API functions constify C Table API Oct 21, 2020
@benjeffery

Copy link
Copy Markdown
Member

Thanks for picking this up @castedo! To avoid large conflicts we can merge as you have chunks ready.

@codecov

codecov Bot commented Oct 21, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #919 into main will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #919   +/-   ##
=======================================
  Coverage   93.43%   93.43%           
=======================================
  Files          25       25           
  Lines       20065    20083   +18     
  Branches      808      808           
=======================================
+ Hits        18747    18765   +18     
  Misses       1281     1281           
  Partials       37       37           
Impacted Files Coverage Δ
c/tskit/tables.c 90.61% <100.00%> (+0.03%) ⬆️

Continue to review full report at Codecov.

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

@castedo

castedo commented Oct 21, 2020

Copy link
Copy Markdown
Member Author

Ready for review now.

There is a type change which I'd like to bring to attention. The type change is part of making a number of other functions const-correct, namely the _get_row( functions setting (returning) tsk_individual_t and tsk_site_t.

The high-level semantic clarficiation I've coded is the following:

that the eight tsk_..._table_get_row( functions are intended to get a "row" from the table and NOT to use that "row" struct to then make changes to the original tsk_..._table_t. In other words, the row returned is conceptually a const iterator/pointer for read-only access.

In the current main branch the only ambiguity is with tsk_individual_table_get_row and tsk_site_table_get_row because the "row" types returned contain non-const pointers. In this PR I have changed those pointers to be const, as summarized in code here:

typedef struct {
...
    const double *location;
...
    const tsk_id_t *nodes;
...
} tsk_individual_t;

typedef struct {
...
    const tsk_mutation_t *mutations;
...
} tsk_site_t;

This is consistent with the const char * data members.

@castedo
castedo marked this pull request as ready for review October 21, 2020 14:28
@castedo

castedo commented Oct 21, 2020

Copy link
Copy Markdown
Member Author

FYI, the unsuccessful check is due to Too long with no output (exceeded 10m0s): context deadline exceeded after running pytest. I suspect it's some hiccup on the part of CircleCI. pytest passed fine on my local machine.

@benjeffery

Copy link
Copy Markdown
Member

FYI, the unsuccessful check is due to Too long with no output (exceeded 10m0s): context deadline exceeded after running pytest. I suspect it's some hiccup on the part of CircleCI. pytest passed fine on my local machine.

This is a flakey test introduced by #909. I'll re-run circle CI.

@castedo

castedo commented Oct 22, 2020

Copy link
Copy Markdown
Member Author

If you can't get enough of reviewing const parameter changes, endulge some more on #920! 😄
Take this PR now and #920 later or just take #920 which includes everything in this PR. They are commits touching separate files.

@benjeffery benjeffery left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yikes, thanks for this @castedo - looks like a lot of work.
All these changes make sense to me.

I'll ping @molpopgen for a quick look - ignore if you're too busy with teaching.

@castedo

castedo commented Oct 22, 2020

Copy link
Copy Markdown
Member Author

looks like a lot of work.

It's so much easier to suggest an API should use const vs actually doing the work to make it reality. 😆

Originally I thought this work would be tweaking a few function declarations. But I ended up deciding I should actually confirm that const parameters really don't get changed. And in confirming that, I basically was figuring out what parameters should be const for all the functions that get called. It was kind of fun figuring out what the code does! I keep hoping I'm going to spot an interesting bug.

@jeromekelleher jeromekelleher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this a big improvement and a significant boost for library correctness and documentation - thanks very much for taking this on @castedo! I have a few questions and I think there's a some minor inconsistencies in places, but I think we should do this.

I was worried about const poisoning, but it doesn't seem to have affected our test code at all, and we have compiler flags on very high paranoia settings so that seems fine.

Comment thread c/tskit/tables.c Resolved
Comment thread c/tskit/tables.c Resolved
Comment thread c/tskit/tables.h Resolved
@jeromekelleher

Copy link
Copy Markdown
Member

I keep hoping I'm going to spot an interesting bug.

Keep looking, I'm sure there's some there! 😉

@castedo
castedo force-pushed the pr/const_c_table_api branch from 2060d1b to c238d57 Compare October 24, 2020 14:58
@castedo

castedo commented Oct 24, 2020

Copy link
Copy Markdown
Member Author

I've resolved remaining comments. This should be ready to merge. Looks like the build fail is another timeout and not real fail.

However, related to the comments is two parts of the header file I have not "constified". One is this section of "Undocumented methods":

int tsk_table_collection_link_ancestors(tsk_table_collection_t *self, tsk_id_t *samples,

which looks like there could be some clarification on what params are or are not left unmodified. However, when reading the implementation code, it wasn't quickly clear to me.

And then there is the "experimental IBD API" section:

/* IBD finder API. This is experimental and the interface may change. */

which I haven't bothered to read at all really.

I'm inclined to skip them, but mainly because I don't totally understand the implementation code.

@castedo

castedo commented Oct 24, 2020 •

Copy link
Copy Markdown
Member Author

@jeromekelleher

I was worried about const poisoning, but it doesn't seem to have affected our test code at all

I've saved that fun for last 😄, specificly the last of three PRs. I've staged the most straight forward const-ifications in the two current open PRs. The third stage I have not PR'd yet but have most of it stached away locally in git. It changes way more files. We can see how scary it looks in a third draft PR.

@jeromekelleher jeromekelleher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks @castedo! Looking forward to the next installation¬!

@mergify
mergify Bot merged commit e417876 into tskit-dev:main Oct 26, 2020
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.

4 participants