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 non_owning_*::get_raw_pointer(). #2497

Merged
merged 2 commits into from
Sep 14, 2023
Merged

Conversation

1uc
Copy link
Collaborator

@1uc 1uc commented Sep 13, 2023

No description provided.

@1uc
Copy link
Collaborator Author

1uc commented Sep 13, 2023

I would like towards allowing the stable identifiers to be reference counted. One prerequisite for this to work without risking segfaults is that nobody has the address of the stable identifier. Instead they should just have a non_owning_identifier of some sort (with or without container).

@azure-pipelines
Copy link

✔️ b92adf5 -> Azure artifacts URL

@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Merging #2497 (159a007) into master (93c671b) will decrease coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #2497      +/-   ##
==========================================
- Coverage   61.49%   61.49%   -0.01%     
==========================================
  Files         624      624              
  Lines      119189   119190       +1     
==========================================
- Hits        73294    73292       -2     
- Misses      45895    45898       +3     
Files Changed Coverage Δ
src/neuron/container/non_owning_soa_identifier.hpp 100.00% <ø> (ø)

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@bbpbuildbot

This comment has been minimized.

@1uc
Copy link
Collaborator Author

1uc commented Sep 14, 2023

ModelDB CI passed, see:
https://github.com/neuronsimulator/nrn-modeldb-ci/actions/runs/6173216548

Given how recent the addition of the method is I suspect that we can remove it without any breakage. I feels it crucial that we do not expose the address of the stable identifier. The reason for this it's a requirement for understanding the lifetime of the stable identifiers.

As mentioned before it's a requirement to make the stable identifiers reference counted; because if we expose the address, we can't know when it's safe to deallocate.

Finally, we can always add it back, but removing this method once it's in use could easily become a very hard problem.

@1uc 1uc marked this pull request as ready for review September 14, 2023 05:59
Copy link
Member

@nrnhines nrnhines left a comment

Choose a reason for hiding this comment

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

I presume this has nothing to do with handles to raw pointers?

@1uc
Copy link
Collaborator Author

1uc commented Sep 14, 2023

Summary: no, this function returns the address of the stable identifier. Not the raw pointer that can be stored in a data_handle.

Detailed explanation: There's two levels of lifetime at play. One is to keep rows in soa containers alive. This happens via owning_{handle,identifier} classes. Then there's the data_handle object, they loosely act as pointer to rows, but don't keep the row alive. They have access to a stable identifier, the size_t * inside the non_owning_identifier_without_container. The handles can check that they're valid by dereferencing that size_t *; if they get size_t(-1) then the memory that this handle points to has been freed.

You'll notice the second level of ownership, how does the data_handle know that it's safe to dereference the size_t *? Currently, it knows because we promise to leak the stable identifier, i.e. we simply never deallocate the size_t.

In simulations at BBP we've seen that the amount of memory used by these identifiers isn't high, but high enough to optimize it away (if possible).

The question is then: When is it safe to deallocate the stable identifier? Currently the answer is that we can't know, because someone could have gotten the address of the stable identifier via get_raw_pointer and then shared it with anyone. If we remove this method, then a possible answer is, that we could use reference counting to know when the last object that has access to this stable identifier has been deallocated. Once that happens, we are free to deallocate the stable identifier, because nobody is using it anymore. Essentially, the exact same logic as with std::shared_ptr.

However, this method should be removed regardless of the reference counting issue; simply because it makes the developers life much harder if it's we start using it.

@azure-pipelines
Copy link

✔️ 159a007 -> Azure artifacts URL

@nrnhines nrnhines merged commit e742cdf into master Sep 14, 2023
33 checks passed
@nrnhines nrnhines deleted the 1uc/remove-get_raw_pointer branch September 14, 2023 14:29
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