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

Switch getburninfo to use BufferPool #1799

Merged
merged 12 commits into from
Mar 6, 2023
44 changes: 7 additions & 37 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,53 +2009,23 @@ UniValue getburninfo(const JSONRPCRequest& request) {
}
}

class WorkerResultPool {
public:
explicit WorkerResultPool(size_t size) {
pool.reserve(size);
for (size_t i = 0; i < size; i++) {
pool.push_back(std::make_shared<CGetBurnInfoResult>());
}
}

std::shared_ptr<CGetBurnInfoResult> Acquire() {
CLockFreeGuard lock{syncFlag};
auto res = pool.back();
pool.pop_back();
return res;
}

void Release(std::shared_ptr<CGetBurnInfoResult> res) {
CLockFreeGuard lock{syncFlag};
pool.push_back(res);
}

std::vector<std::shared_ptr<CGetBurnInfoResult>> &GetContainer() {
return pool;
}

private:
std::atomic_bool syncFlag{};
std::vector<std::shared_ptr<CGetBurnInfoResult>> pool;
};

auto nWorkers = DfTxTaskPool->GetAvailableThreads();
if (static_cast<size_t>(height) < nWorkers) {
nWorkers = height;
}

const auto chunks = height / nWorkers;
const auto chunkSize = height / nWorkers;

TaskGroup g;
WorkerResultPool resultsPool{nWorkers};
BufferPool<CGetBurnInfoResult> resultsPool{nWorkers};

auto &pool = DfTxTaskPool->pool;
auto processedHeight = initialResult.height;
auto i = 0;
while (processedHeight < height)
{
auto startHeight = initialResult.height + (chunks * (i + 1));
auto stopHeight = initialResult.height + (chunks * (i));
auto startHeight = initialResult.height + (chunkSize * (i + 1));
auto stopHeight = initialResult.height + (chunkSize * (i));

g.AddTask();
boost::asio::post(pool, [startHeight, stopHeight, &g, &resultsPool] {
Expand Down Expand Up @@ -2138,14 +2108,14 @@ UniValue getburninfo(const JSONRPCRequest& request) {
g.RemoveTask();
});

// perfect accuracy: processedHeight += (startHeight > height) ? chunksRemainder : chunks;
processedHeight += chunks;
// perfect accuracy: processedHeight += (startHeight > height) ? chunksRemainder : chunkSize;
processedHeight += chunkSize;
i++;
}

g.WaitForCompletion();

for (const auto &r : resultsPool.GetContainer()) {
for (const auto &r : resultsPool.GetBuffer()) {
totalResult->burntDFI += r->burntDFI;
totalResult->burntFee += r->burntFee;
totalResult->auctionFee += r->auctionFee;
Expand Down
3 changes: 2 additions & 1 deletion src/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,10 @@ class CLockFreeGuard
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

~CLockFreeGuard()
{
lock.store(false, std::memory_order_release);
lock.store(false);
}
};

Expand Down