Skip to content

Add TSK_TRANSFER_OWNERSHIP flag #2006

Closed

Description

@jeromekelleher

There's a few places where we want to be able to transfer the responsibility for freeing an object:

tsk_reference_sequence_set_data(self, data, data_length, options)

Here, we have a string malloced by the client that we want to "give" directly to the library, rather than taking a copy

tsk_x_table_set_columns(table, col1, col2, ..., options)

Here, we have individually malloced columns col1, etc which we want to give to the library. Note that once these are pointers obtained from malloc, there's no need to mark the tables read-only, we can operate as normal.

tsk_treeseq_init(ts, tables, options)

Here, we want to pass ownership of the tables from the client to the tree sequence. Currently treeseq_init always takes a copy, which is often unnecessary.

I suggest we use the same flag in each case. We could also use TSK_NO_COPY, but it's not as clear that we're transferring the responsibility for freeing as well then.

@bhaller, @molpopgen, any thoughts on the flag naming and semantics as downstream users?

With these three things, and some downstream kastore updates (https://github.com/tskit-dev/kastore/milestone/9) we should be able to reduce our memory usage for tskit.load() by a factor of 3 because we'll only allocate the buffers for each array once.

Activity

  1. bhaller commented on Dec 3, 2021

    @bhaller

    I'm a fan of this idea. Will probably use it only in the reference sequence case, in SLiM, but it sounds generally useful. TSK_TRANSFER_OWNERSHIP, as in the issue title, seems pretty clear. TSK_TAKE_OWNERSHIP might be even clearer, to say "tsk is taking" – active construct, versus the more passive "transferring". You could emphasize that the block must be malloced, as in TSK_TAKE_MALLOCED_BLOCKS or something, but that's pretty wordy, and makes it sounds like maybe if the blocks aren't malloced then tsk just won't take them (when in fact a crash would presumably eventually result); and anyway I think everybody who isn't a total newbie programmer would assume that if tsk is taking ownership then the blocks have to be malloced. So I think my vote is for TSK_TAKE_OWNERSHIP maybe?

  2. jeromekelleher commented on Dec 3, 2021

    @jeromekelleher
    MemberAuthor

    Let's have some votes (:+1: :-1:)

    data = malloc(big_number);
    ret = tsk_reference_sequence_set_data(&tables->reference_sequence, 
        data, big_number, TSK_TRANSFER_OWNERSHIP);
  3. jeromekelleher commented on Dec 3, 2021

    @jeromekelleher
    MemberAuthor

    or

    data = malloc(big_number);
    ret = tsk_reference_sequence_set_data(&tables->reference_sequence, 
        data, big_number, TSK_TAKE_OWNERSHIP);
  4. jeromekelleher commented on Dec 3, 2021

    @jeromekelleher
    MemberAuthor

    It depends on who you see as doing the "transferring" and "taking" doesn't it? On balance I think TSK_TAKE_OWNERSHIP is clearer, you're telling tskit to take ownership of the buffer.

  5. molpopgen commented on Dec 3, 2021

    @molpopgen
    Member

    This will be a great addition.

  6. molpopgen commented on Dec 3, 2021

    @molpopgen
    Member

    I should do a mock-up (for my own edification at least) of the implications for rust. The borrow checker is strict!(

  7. jeromekelleher commented on Dec 3, 2021

    @jeromekelleher
    MemberAuthor

    Which is timely... We're hitting some serious const tedium when I started working through this.

    All the pointers that we're passing will have to be marked non-const, which will really muck up the test suite.

    It would nearly be easier to have a separate set of methods to be honest... Any opinions?

  8. molpopgen commented on Dec 3, 2021

    @molpopgen
    Member

    Ugh, right--const! I hadn't even gone there yet in my head. Yeah, it seems that a "tree seq from const table collection" function is the way to go?

  9. bhaller commented on Dec 3, 2021

    @bhaller

    Is it important to any clients of tskit to have that API guarantee of constness? Const is nice to support when it works well, but if a bunch of API would have to be doubled, maybe it just isn't worth it...?

  10. jeromekelleher commented on Dec 3, 2021

    @jeromekelleher
    MemberAuthor

    So, we could change to something like

    char * data = malloc(big_number);
    tsk_reference_sequence_set_data(&tables->reference_sequence,  data, big_number);

    and

    const char * data = "ACGT";
    tsk_reference_sequence_copy_data(&tables->reference_sequence,  data, big_number);

    So the set form takes ownership of the reference, and the copy for takes a copy.

    We'd also have to rename the current x_table_set_columns to x_table_copy_columns and make a new non-const methods x_table_set_columns.

    Overall, this is a more logical setup tbh. Compiler warnings are there for a reason...

  11. bhaller commented on Dec 3, 2021

    @bhaller

    That makes sense; I would just say that you probably shouldn't switch the semantics of any existing API from "take a copy" to "take ownership", because that will introduce nasty crashing bugs, right? If you're going to change the ownership semantics, it would probably be best to abandon the old API completely, to force compile errors that people have to think about the right way to fix. Of course the refseq APIs are brand new anyway, so nobody should be surprised if their semantics are still in flux; but you're talking about doing this API shift for other calls too, right? So maybe use take_data() and copy_data(), and abandon set_data() names throughout?

  12. jeromekelleher commented on Dec 4, 2021

    @jeromekelleher
    MemberAuthor

    Yes, good points @bhaller, that's one to consider carefully. You're right we shouldn't change the semantics of set_columns, so take_columns is better. The question then is whether we bother renaming set_columns to copy_columns...

    OK, I think this is definitive. I'll open/close a bunch of issues to try and split things into independent discussions.

  13. jeromekelleher commented on Dec 5, 2021

    @jeromekelleher
    MemberAuthor

    Closing this for now in favour of the "takeset" methods in #2016. We'll still probably want a "TAKE_OWNERSHIP" flag for the treeseq_init case, but lets deal with that separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions