[*] Optimize AuFuture memory usage

This commit is contained in:
Reece Wilson 2023-07-08 12:39:02 +01:00
parent fae92993be
commit edcc9efac3

View File

@ -47,7 +47,7 @@ public:
if (this->bComplete)
{
SysAssert(!AuExchange(this->bDoneCb, true), "Future has already called a completion callback");
ExchangeDoneCb();
if constexpr (AuIsVoid_v<T>)
{
@ -81,7 +81,7 @@ public:
if (this->bFailed)
{
SysAssert(!AuExchange(this->bDoneCb, true), "Future has already called a completion callback");
ExchangeDoneCb();
if constexpr (AuIsVoid_v<Error_t>)
{
@ -113,7 +113,7 @@ public:
void Complete(Move_t value)
{
AU_LOCK_GUARD(this->mutex);
SysAssert(!AuExchange(this->bDone, true), "Future has already finished");
ExchangeDone();
this->value = AuMove(value);
this->bComplete = true;
@ -125,7 +125,7 @@ public:
void Complete()
{
AU_LOCK_GUARD(this->mutex);
SysAssert(!AuExchange(this->bDone, true), "Future has already finished");
ExchangeDone();
this->bComplete = true;
@ -136,7 +136,7 @@ public:
void Fail(Move2_t error)
{
AU_LOCK_GUARD(this->mutex);
SysAssert(!AuExchange(this->bDone, true), "Future has already finished");
ExchangeDone();
this->errorValue = AuMove(error);
this->bFailed = true;
@ -148,7 +148,7 @@ public:
void Fail()
{
AU_LOCK_GUARD(this->mutex);
SysAssert(!AuExchange(this->bDone, true), "Future has already finished");
ExchangeDone();
this->bFailed = true;
@ -206,6 +206,26 @@ protected:
private:
void ExchangeDone()
{
#if 0
SysAssert(!AuExchange(this->bDone, true), "Future has already finished");
#else
SysAssert(!this->bDone, "Future has already finished");
this->bDone = true;
#endif
}
void ExchangeDoneCb()
{
#if 0
SysAssert(!AuExchange(this->bDoneCb, true), "Future has already called a completion callback");
#else
SysAssert(!this->bDoneCb, "Future has already called a completion callback");
this->bDoneCb = true;
#endif
}
void SubmitComplete()
{
if (AuAsync::GetCurrentWorkerPId() == this->pid)
@ -218,7 +238,7 @@ private:
return;
}
SysAssert(!AuExchange(this->bDoneCb, true), "Future has already called a completion callback");
ExchangeDoneCb();
if (this->bComplete)
{
@ -317,17 +337,22 @@ private:
CppFun<T>::B value;
ErrorStore_t errorValue;
AuThreadPrimitives::Mutex mutex;
AuThreadPrimitives::Event event;
CompleteCallback_f callback;
ErrorCallback_f onFailure;
AuOptionalEx<AuAsync::WorkerPId_t> pid; // todo: make weak?
bool bComplete {};
bool bFailed {};
bool bDone {};
bool bDoneCb {};
AuList<AuConsumer<bool, bool>> waterfall;
AuUInt8 bComplete {};
AuUInt8 bFailed {};
AuUInt8 bDone {};
AuUInt8 bDoneCb {};
friend struct AuWaterfall;
};
@ -464,11 +489,19 @@ private:
this->bFailed = true;
}
#if 0
if (AuExchange(this->bDone, true))
{
// Miss?
return;
}
#else
if (this->bDone)
{
return;
}
this->bDone = true;
#endif
if (bSendSuccess)
{
@ -482,10 +515,19 @@ private:
void Start()
{
if (AuExchange(this->bReady, true))
#if 0
if (AuExchange(this->bDone, true))
{
// Miss?
return;
}
#else
if (this->bDone)
{
return;
}
this->bDone = true;
#endif
this->pFuture->OnComplete([pThat = this->SharedFromThis()]()
{
@ -537,13 +579,22 @@ private:
AuList<AuVoidFunc> onFailure;
AuThreadPrimitives::CriticalSection mutex;
bool bFailOnAny;
AuUInt uCount {};
AuUInt uCountOfComplete {};
AuUInt uCountOfFailed {};
#if 0
bool bFailOnAny {};
bool bReady {};
bool bDone {};
bool bFailed {};
#else
AuUInt8 bFailOnAny : 1{};
AuUInt8 bReady : 1 {};
AuUInt8 bDone : 1 {};
AuUInt8 bFailed : 1 {};
#endif
};
using AuSharedWaterfall = AuSPtr<AuWaterfall>;