Skip to content

Commit c6f3be3

Browse files
authored
Revert "[ML] Convert std::shared_ptrs to std::unique_ptrs where possible (#108)" (#114)
This reverts commit 71dc485.
1 parent 22a57a2 commit c6f3be3

File tree

80 files changed

+609
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+609
-883
lines changed

docs/CHANGELOG.asciidoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ Secure the ML processes by preventing system calls such as fork and exec. The Li
4141
Seccomp BPF to intercept system calls and is available in kernels since 3.5. On Windows Job Objects prevent
4242
new processes being created and macOS uses the sandbox functionality ({pull}98[#98])
4343

44-
Fix a bug causing us to under estimate the memory used by shared pointers and reduce the memory consumed
45-
by unnecessary reference counting ({pull}108[#108])
46-
4744
=== Bug Fixes
4845

4946
Age seasonal components in proportion to the fraction of values with which they're updated ({pull}88[#88])

include/api/CForecastRunner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class API_EXPORT CForecastRunner final : private core::CNonCopyable {
115115
using TForecastModelWrapper = model::CForecastDataSink::SForecastModelWrapper;
116116
using TForecastResultSeries = model::CForecastDataSink::SForecastResultSeries;
117117
using TForecastResultSeriesVec = std::vector<TForecastResultSeries>;
118-
using TMathsModelPtr = std::unique_ptr<maths::CModel>;
118+
using TMathsModelPtr = std::shared_ptr<maths::CModel>;
119119

120120
using TStrUSet = boost::unordered_set<std::string>;
121121

include/core/CMemory.h

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,21 @@ class CORE_EXPORT CMemory : private CNonInstantiatable {
287287
static std::size_t
288288
dynamicSize(const T& t,
289289
typename boost::enable_if<typename boost::is_pointer<T>>::type* = nullptr) {
290-
return t == nullptr ? 0 : staticSize(*t) + dynamicSize(*t);
290+
if (t == nullptr) {
291+
return 0;
292+
}
293+
return staticSize(*t) + dynamicSize(*t);
291294
}
292295

293296
//! Overload for std::shared_ptr.
294297
template<typename T>
295298
static std::size_t dynamicSize(const std::shared_ptr<T>& t) {
296-
if (t == nullptr) {
299+
if (!t) {
297300
return 0;
298301
}
299302
long uc = t.use_count();
300-
// Note we add on sizeof(long) here to account for the memory
301-
// used by the shared reference count. Also, round up.
302-
return (sizeof(long) + staticSize(*t) + dynamicSize(*t) + std::size_t(uc - 1)) / uc;
303-
}
304-
305-
//! Overload for std::unique_ptr.
306-
template<typename T>
307-
static std::size_t dynamicSize(const std::unique_ptr<T>& t) {
308-
return t == nullptr ? 0 : staticSize(*t) + dynamicSize(*t);
303+
// Round up
304+
return (staticSize(*t) + dynamicSize(*t) + std::size_t(uc - 1)) / uc;
309305
}
310306

311307
//! Overload for boost::array.
@@ -696,40 +692,25 @@ class CORE_EXPORT CMemoryDebug : private CNonInstantiatable {
696692
static void dynamicSize(const char* name,
697693
const std::shared_ptr<T>& t,
698694
CMemoryUsage::TMemoryUsagePtr mem) {
699-
if (t != nullptr) {
695+
if (t) {
700696
long uc = t.use_count();
701697
// If the pointer is shared by multiple users, each one
702698
// might count it, so divide by the number of users.
703699
// However, if only 1 user has it, do a full debug.
704700
if (uc == 1) {
705-
// Note we add on sizeof(long) here to account for
706-
// the memory used by the shared reference count.
707-
mem->addItem("shared_ptr", sizeof(long) + CMemory::staticSize(*t));
701+
mem->addItem("shared_ptr", CMemory::staticSize(*t));
708702
dynamicSize(name, *t, mem);
709703
} else {
710704
std::ostringstream ss;
711705
ss << "shared_ptr (x" << uc << ')';
712-
// Note we add on sizeof(long) here to account for
713-
// the memory used by the shared reference count.
714-
// Also, round up.
715-
mem->addItem(ss.str(), (sizeof(long) + CMemory::staticSize(*t) +
706+
// Round up
707+
mem->addItem(ss.str(), (CMemory::staticSize(*t) +
716708
CMemory::dynamicSize(*t) + std::size_t(uc - 1)) /
717709
uc);
718710
}
719711
}
720712
}
721713

722-
//! Overload for std::unique_ptr.
723-
template<typename T>
724-
static void dynamicSize(const char* name,
725-
const std::unique_ptr<T>& t,
726-
CMemoryUsage::TMemoryUsagePtr mem) {
727-
if (t != nullptr) {
728-
mem->addItem("ptr", CMemory::staticSize(*t));
729-
memory_detail::SDebugMemoryDynamicSize<T>::dispatch(name, *t, mem);
730-
}
731-
}
732-
733714
//! Overload for boost::array.
734715
template<typename T, std::size_t N>
735716
static void dynamicSize(const char* name,

include/maths/CChecksum.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,13 @@ class CChecksumImpl<BasicChecksum> {
158158
: CChecksumImpl<typename selector<T>::value>::dispatch(seed, *target);
159159
}
160160

161-
//! Checksum a shared pointer.
161+
//! Checksum a pointer.
162162
template<typename T>
163163
static uint64_t dispatch(uint64_t seed, const std::shared_ptr<T>& target) {
164164
return !target ? seed
165165
: CChecksumImpl<typename selector<T>::value>::dispatch(seed, *target);
166166
}
167167

168-
//! Checksum a unique pointer.
169-
template<typename T>
170-
static uint64_t dispatch(uint64_t seed, const std::unique_ptr<T>& target) {
171-
return !target ? seed
172-
: CChecksumImpl<typename selector<T>::value>::dispatch(seed, *target);
173-
}
174-
175168
//! Checksum a pair.
176169
template<typename U, typename V>
177170
static uint64_t dispatch(uint64_t seed, const std::pair<U, V>& target) {

include/maths/CClusterer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class MATHS_EXPORT CClustererTypes {
125125
template<typename POINT>
126126
class CClusterer : public CClustererTypes {
127127
public:
128+
using TClustererPtr = std::shared_ptr<CClusterer>;
128129
using TPointVec = std::vector<POINT>;
129130
using TPointPrecise = typename SPromoted<POINT>::Type;
130131
using TPointPreciseVec = std::vector<TPointPrecise>;

include/maths/CClustererStateSerialiser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct SDistributionRestoreParams;
4343
//! to potential competitors.
4444
class MATHS_EXPORT CClustererStateSerialiser {
4545
public:
46-
using TClusterer1dPtr = std::unique_ptr<CClusterer1d>;
46+
using TClusterer1dPtr = std::shared_ptr<CClusterer1d>;
4747

4848
public:
4949
//! Construct the appropriate CClusterer sub-class from its state
@@ -73,7 +73,7 @@ class MATHS_EXPORT CClustererStateSerialiser {
7373
//! \note Sets \p ptr to NULL on failure.
7474
template<typename T, std::size_t N>
7575
bool operator()(const SDistributionRestoreParams& params,
76-
std::unique_ptr<CClusterer<CVectorNx1<T, N>>>& ptr,
76+
std::shared_ptr<CClusterer<CVectorNx1<T, N>>>& ptr,
7777
core::CStateRestoreTraverser& traverser) {
7878
return this->operator()(params, CClustererTypes::CDoNothing(),
7979
CClustererTypes::CDoNothing(), ptr, traverser);
@@ -87,7 +87,7 @@ class MATHS_EXPORT CClustererStateSerialiser {
8787
bool operator()(const SDistributionRestoreParams& params,
8888
const CClustererTypes::TSplitFunc& splitFunc,
8989
const CClustererTypes::TMergeFunc& mergeFunc,
90-
std::unique_ptr<CClusterer<CVectorNx1<T, N>>>& ptr,
90+
std::shared_ptr<CClusterer<CVectorNx1<T, N>>>& ptr,
9191
core::CStateRestoreTraverser& traverser) {
9292
std::size_t numResults(0);
9393

include/maths/CModelStateSerialiser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct SModelRestoreParams;
3434
//! compatibility in the future as the classes evolve.
3535
class MATHS_EXPORT CModelStateSerialiser {
3636
public:
37-
using TModelPtr = std::unique_ptr<CModel>;
37+
using TModelPtr = std::shared_ptr<CModel>;
3838

3939
public:
4040
//! Construct the appropriate CPrior sub-class from its state

include/maths/CMultimodalPrior.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ namespace maths {
4949
//! point of view this is the composite pattern.
5050
class MATHS_EXPORT CMultimodalPrior : public CPrior {
5151
public:
52-
using TClustererPtr = std::unique_ptr<CClusterer1d>;
53-
using TPriorPtr = std::unique_ptr<CPrior>;
52+
using TClustererPtr = std::shared_ptr<CClusterer1d>;
53+
using TPriorPtr = std::shared_ptr<CPrior>;
5454
using TPriorPtrVec = std::vector<TPriorPtr>;
55+
using TPriorPtrVecItr = TPriorPtrVec::iterator;
56+
using TPriorPtrVecCItr = TPriorPtrVec::const_iterator;
5557
using TMeanVarAccumulator = CBasicStatistics::SSampleMeanVar<double>::TAccumulator;
5658
using TMeanVarAccumulatorVec = std::vector<TMeanVarAccumulator>;
5759

@@ -78,9 +80,6 @@ class MATHS_EXPORT CMultimodalPrior : public CPrior {
7880
double decayRate = 0.0);
7981

8082
//! Create from a collection of weights and priors.
81-
//!
82-
//! \note The priors are moved into place clearing the values in \p priors.
83-
//! \note This constructor doesn't support subsequent update of the prior.
8483
CMultimodalPrior(maths_t::EDataType dataType, double decayRate, TPriorPtrVec& priors);
8584

8685
//! Construct from part of a state document.
@@ -321,7 +320,7 @@ class MATHS_EXPORT CMultimodalPrior : public CPrior {
321320
CMultimodalPrior* m_Prior;
322321
};
323322

324-
using TMode = SMultimodalPriorMode<TPriorPtr>;
323+
using TMode = SMultimodalPriorMode<std::shared_ptr<CPrior>>;
325324
using TModeVec = std::vector<TMode>;
326325

327326
private:

include/maths/CMultimodalPriorMode.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ struct SMultimodalPriorMode {
3636
SMultimodalPriorMode() : s_Index(0), s_Prior() {}
3737
SMultimodalPriorMode(std::size_t index, const PRIOR_PTR& prior)
3838
: s_Index(index), s_Prior(prior->clone()) {}
39-
SMultimodalPriorMode(std::size_t index, PRIOR_PTR&& prior)
40-
: s_Index(index), s_Prior(std::move(prior)) {}
4139

4240
//! Get the weight of this sample.
4341
double weight() const { return s_Prior->numberSamples(); }

include/maths/CMultivariateMultimodalPrior.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <maths/MathsTypes.h>
3333

3434
#include <boost/bind.hpp>
35-
#include <boost/make_unique.hpp>
3635
#include <boost/numeric/conversion/bounds.hpp>
3736
#include <boost/ref.hpp>
3837

@@ -51,10 +50,10 @@ namespace multivariate_multimodal_prior_detail {
5150

5251
using TSizeDoublePr = std::pair<size_t, double>;
5352
using TSizeDoublePr3Vec = core::CSmallVector<TSizeDoublePr, 3>;
53+
using TPriorPtr = std::shared_ptr<CMultivariatePrior>;
5454
using TDouble10Vec1Vec = CMultivariatePrior::TDouble10Vec1Vec;
5555
using TDouble10VecWeightsAry1Vec = CMultivariatePrior::TDouble10VecWeightsAry1Vec;
56-
using TPriorPtr = std::unique_ptr<CMultivariatePrior>;
57-
using TMode = SMultimodalPriorMode<TPriorPtr>;
56+
using TMode = SMultimodalPriorMode<std::shared_ptr<CMultivariatePrior>>;
5857
using TModeVec = std::vector<TMode>;
5958

6059
//! Implementation of a sample joint log marginal likelihood calculation.
@@ -135,7 +134,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
135134
using TMatrix = CSymmetricMatrixNxN<double, N>;
136135
using TMatrixVec = std::vector<TMatrix>;
137136
using TClusterer = CClusterer<TFloatPoint>;
138-
using TClustererPtr = std::unique_ptr<TClusterer>;
137+
using TClustererPtr = std::shared_ptr<TClusterer>;
139138
using TPriorPtrVec = std::vector<TPriorPtr>;
140139

141140
// Lift all overloads of into scope.
@@ -163,13 +162,13 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
163162

164163
//! Create from a collection of priors.
165164
//!
166-
//! \note The priors are moved into place clearing the values in \p priors.
165+
//! \note The priors are shallow copied.
167166
//! \note This constructor doesn't support subsequent update of the prior.
168167
CMultivariateMultimodalPrior(maths_t::EDataType dataType, TPriorPtrVec& priors)
169168
: CMultivariatePrior(dataType, 0.0) {
170169
m_Modes.reserve(priors.size());
171170
for (std::size_t i = 0u; i < priors.size(); ++i) {
172-
m_Modes.emplace_back(i, std::move(priors[i]));
171+
m_Modes.emplace_back(i, priors[i]);
173172
}
174173
}
175174

@@ -426,12 +425,12 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
426425
for (const auto& mode : m_Modes) {
427426
TUnivariatePriorPtrDoublePr prior(mode.s_Prior->univariate(marginalize, condition));
428427
if (prior.first == nullptr) {
429-
return {};
428+
return TUnivariatePriorPtrDoublePr();
430429
}
431430
if (prior.first->isNonInformative()) {
432431
continue;
433432
}
434-
modes.push_back(std::move(prior.first));
433+
modes.push_back(prior.first);
435434
weights.push_back(prior.second);
436435
maxWeight.add(weights.back());
437436
}
@@ -445,8 +444,8 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
445444
modes[i]->numberSamples(weights[i] / Z * modes[i]->numberSamples());
446445
}
447446

448-
return {boost::make_unique<CMultimodalPrior>(this->dataType(),
449-
this->decayRate(), modes),
447+
return {TUnivariatePriorPtr(new CMultimodalPrior(this->dataType(),
448+
this->decayRate(), modes)),
450449
Z > 0.0 ? maxWeight[0] + std::log(Z) : 0.0};
451450
}
452451

@@ -466,7 +465,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
466465
const TSizeDoublePr10Vec& condition) const {
467466

468467
if (N == 2) {
469-
return {TPriorPtr(this->clone()), 0.0};
468+
return TPriorPtrDoublePr(TPriorPtr(this->clone()), 0.0);
470469
}
471470

472471
std::size_t n = m_Modes.size();
@@ -485,7 +484,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
485484
if (prior.first->isNonInformative()) {
486485
continue;
487486
}
488-
modes.push_back(std::move(prior.first));
487+
modes.push_back(prior.first);
489488
weights.push_back(prior.second);
490489
maxWeight.add(weights.back());
491490
}
@@ -499,7 +498,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
499498
modes[i]->numberSamples(weights[i] / Z * modes[i]->numberSamples());
500499
}
501500

502-
return {boost::make_unique<CMultivariateMultimodalPrior<2>>(this->dataType(), modes),
501+
return {TPriorPtr(new CMultivariateMultimodalPrior<2>(this->dataType(), modes)),
503502
Z > 0.0 ? maxWeight[0] + std::log(Z) : 0.0};
504503
}
505504

@@ -906,7 +905,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
906905

907906
// Create the child modes.
908907
LOG_TRACE(<< "Creating mode with index " << leftSplitIndex);
909-
modes.emplace_back(leftSplitIndex, TPriorPtr(m_Prior->m_SeedPrior->clone()));
908+
modes.emplace_back(leftSplitIndex, m_Prior->m_SeedPrior);
910909
{
911910
TPointVec samples;
912911
if (!m_Prior->m_Clusterer->sample(
@@ -936,7 +935,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
936935
}
937936

938937
LOG_TRACE(<< "Creating mode with index " << rightSplitIndex);
939-
modes.emplace_back(rightSplitIndex, TPriorPtr(m_Prior->m_SeedPrior->clone()));
938+
modes.emplace_back(rightSplitIndex, m_Prior->m_SeedPrior);
940939
{
941940
TPointVec samples;
942941
if (!m_Prior->m_Clusterer->sample(
@@ -1026,7 +1025,7 @@ class CMultivariateMultimodalPrior : public CMultivariatePrior {
10261025
MODE_TAG, TMode mode,
10271026
traverser.traverseSubLevel(boost::bind(
10281027
&TMode::acceptRestoreTraverser, &mode, boost::cref(params), _1)),
1029-
m_Modes.push_back(std::move(mode)))
1028+
m_Modes.push_back(mode))
10301029
RESTORE_SETUP_TEARDOWN(
10311030
NUMBER_SAMPLES_TAG, double numberSamples,
10321031
core::CStringUtils::stringToType(traverser.value(), numberSamples),

0 commit comments

Comments
 (0)