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

[ML] Convert std::shared_ptrs to std::unique_ptrs where possible #115

Merged
merged 2 commits into from
Jun 8, 2018

Conversation

tveasey
Copy link
Contributor

@tveasey tveasey commented May 31, 2018

As originally implemented in #108, the fix for m_Exemplar being null after restore relied on writing additional state. This fails if the fix is present on master and not 6.4: elastic/elasticsearch#30982. In fact, this doesn't need to be fixed by modifying the state we write, instead we can just as well pass this into the restore constructor.

In the first commit this reapplies #108, which I reverted from master. In the second I've implemented the alternative fix. This commit is therefore the only one which needs reviewing.

Just to reiterate, the affects on results should be small:

  1. I account for additional memory, for shared pointers, which we were previously missing. I've also converted many instances of shared pointer to unique pointers. This saves us 16 bytes per pointer; for example, this reduces the size of our distribution models by a fraction under 20%. (This is also useful because I have an upcoming change which will mean we use more distribution models per time series model.) The upshot of these two changes is in the case of memory limiting we'll potentially get different results.
  2. It fixes a bug in restore of CNaiveBayes. This code isn't released, but as it is the exemplar would wind up null after a persist and restore. This is then a crash waiting to happen: since we'll try and clone it if we get a new (classification) class down the road.

@tveasey tveasey force-pushed the enhancement/unique-ptrs-fix branch from 344ec5f to 90f22e2 Compare May 31, 2018 14:01
Copy link
Contributor

@edsavage edsavage left a comment

Choose a reason for hiding this comment

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

The fix looks good to me Tom

@tveasey tveasey merged commit 3bf8897 into elastic:master Jun 8, 2018
tveasey added a commit to tveasey/ml-cpp-1 that referenced this pull request Jun 8, 2018
…stic#115)

Many of our current uses of shared pointer date from when not all our target platforms were
C++11 compliant and in particular move semantics weren't available. Now that they all are, we can
switch these to std::unique_ptr. This has a few advantages: i) it saves us 16 bytes (8 for the extra
pointer to the reference count and 8 for the reference count) per instance, ii) it avoids atomic
updates of the reference count, iii) it forces one to have the correct copy semantics in such
cases. For example, this showed up that we had an implicit shallow copy (not appropriate) on our
naive Bayes implementation and fixing this showed up a bug in restore. This also fixes an error in
memory accounting for shared pointers, which wasn't including the extra memory for the
reference count.
@sophiec20 sophiec20 added :ml and removed :ml labels Jun 12, 2018
@lcawl lcawl added the >bug label Aug 13, 2018
@tveasey tveasey deleted the enhancement/unique-ptrs-fix branch March 22, 2019 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants