Remove more copying of big structures in RsQThreadUtils::postToObject

This commit is contained in:
Gioacchino Mazzurco 2020-06-24 00:21:33 +02:00
parent d2b9b9f094
commit 3b37c1e9ad
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
6 changed files with 50 additions and 53 deletions

View File

@ -24,6 +24,7 @@
#include <QMenu>
#include <algorithm>
#include <memory>
#include <retroshare/rspeers.h>
#include <retroshare/rsidentity.h>
@ -700,30 +701,28 @@ void CreateCircleDialog::loadIdentities()
if(!rsIdentity->getIdentitiesSummaries(ids_meta))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve identities ids for all identities" << std::endl;
RS_ERR("failed to retrieve identities ids for all identities");
return;
}
std::set<RsGxsId> ids;
for(auto& meta:ids_meta) ids.insert(RsGxsId(meta.mGroupId));
for(auto& meta:ids_meta)
ids.insert(RsGxsId(meta.mGroupId)) ;
std::vector<RsGxsIdGroup> id_groups;
if(!rsIdentity->getIdentitiesInfo(ids,id_groups))
auto id_groups = std::make_unique<std::vector<RsGxsIdGroup>>();
if(!rsIdentity->getIdentitiesInfo(ids, *id_groups))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve identities group info for all identities" << std::endl;
RS_ERR("failed to retrieve identities group info for all identities");
return;
}
RsQThreadUtils::postToObject( [id_groups,this]()
RsQThreadUtils::postToObject(
[id_groups = std::move(id_groups), this]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete */
fillIdentitiesList(id_groups) ;
fillIdentitiesList(*id_groups);
}, this );
});

View File

@ -509,9 +509,7 @@ void IdDialog::updateCircles()
#endif
/* This can be big so use a smart pointer to just copy the pointer
* instead of copying the whole list accross the lambdas.
* Heaptrack reported 141MB of RAM where used to copy this around
* before this change. */
* instead of copying the whole list accross the lambdas */
auto circle_metas = std::make_unique<std::list<RsGroupMetaData>>();
if(!rsGxsCircles->getCirclesSummaries(*circle_metas))
@ -1312,19 +1310,17 @@ void IdDialog::updateIdList()
return;
}
std::map<RsGxsGroupId,RsGxsIdGroup> ids_set;
auto ids_set = std::make_unique<std::map<RsGxsGroupId,RsGxsIdGroup>>();
for(auto it(groups.begin()); it!=groups.end(); ++it)
(*ids_set)[(*it).mMeta.mGroupId] = *it;
for(auto it(groups.begin());it!=groups.end();++it)
ids_set[(*it).mMeta.mGroupId] = *it;
RsQThreadUtils::postToObject( [ids_set,this]()
RsQThreadUtils::postToObject(
[ids_set = std::move(ids_set), this] ()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete */
loadIdentities(ids_set);
loadIdentities(*ids_set);
}, this );
});

View File

@ -210,8 +210,8 @@ void IdEditDialog::setupExistingId(const RsGxsGroupId& keyId)
RsThread::async([this,keyId]()
{
std::vector<RsGxsIdGroup> datavector;
bool res = rsIdentity->getIdentitiesInfo(std::set<RsGxsId>({(RsGxsId)keyId}),datavector);
bool res = rsIdentity->getIdentitiesInfo(
std::set<RsGxsId>({(RsGxsId)keyId}), datavector );
RsQThreadUtils::postToObject( [this,keyId,res,datavector]()
{

View File

@ -36,6 +36,7 @@
#include <retroshare/rsstatus.h>
#include <algorithm>
#include <memory>
#define COLUMN_NAME 0
#define COLUMN_CHECK 0
@ -250,24 +251,21 @@ void FriendSelectionWidget::loadIdentities()
if(!rsIdentity->getIdentitiesSummaries(ids_meta))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve identities group info for all identities" << std::endl;
RS_ERR("failed to retrieve identities group info for all identities");
return;
}
std::vector<RsGxsGroupId> ids;
for(auto& meta:ids_meta)
ids.push_back(meta.mGroupId) ;
auto ids = std::make_unique<std::vector<RsGxsGroupId>>();
for(auto& meta: ids_meta) ids->push_back(meta.mGroupId);
RsQThreadUtils::postToObject( [ids,this]()
RsQThreadUtils::postToObject(
[ids = std::move(ids), this]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete */
gxsIds = ids; // we do that is the GUI thread. Dont try it on another thread!
fillList() ;
// We do that is the GUI thread. Dont try it on another thread!
gxsIds = *ids;
/* TODO: To furter optimize away a copy gxsIds could be a unique_ptr
* too */
fillList();
}, this );
});
}

View File

@ -20,6 +20,7 @@
#include <iostream>
#include <time.h>
#include <memory>
#include <QDateTime>
#include <QFontMetrics>
@ -463,24 +464,25 @@ void GxsTransportStatistics::loadGroups()
#ifdef DEBUG_FORUMS
std::cerr << "Retrieving post data for post " << mThreadId << std::endl;
#endif
std::map<RsGxsGroupId,RsGxsTransGroupStatistics> stats;
auto stats = std::make_unique<
std::map<RsGxsGroupId,RsGxsTransGroupStatistics> >();
if(!rsGxsTrans->getGroupStatistics(stats))
if(!rsGxsTrans->getGroupStatistics(*stats))
{
RsErr() << "Cannot retrieve group statistics in GxsTransportStatistics" << std::endl;
RS_ERR("Cannot retrieve group statistics in GxsTransportStatistics");
return;
}
RsQThreadUtils::postToObject( [stats,this]()
RsQThreadUtils::postToObject(
[stats = std::move(stats), this]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete */
mGroupStats = stats;
// TODO: consider making mGroupStats an unique_ptr to avoid copying
mGroupStats = *stats;
updateContent();
mStateHelper->setLoading(GXSTRANS_GROUP_META, false);
}, this );

View File

@ -1,7 +1,7 @@
/*******************************************************************************
* util/qthreadutils.h *
* *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
@ -27,7 +27,9 @@
#include <QtGlobal>
#include <QtCore>
#include <type_traits>
#include <utility>
namespace RsQThreadUtils {