Reduce memory usage due to copying in IdDialog::updateCircles

Heaptrack reported 141MB of RAM where used by this method most proably
  due to the group metadata list being copied accross lambdas and
  threads more then necessary.
Use an unique_ptr to safely avoid copying of big structure around.
This commit is contained in:
Gioacchino Mazzurco 2020-06-23 23:38:22 +02:00
parent 947b2a3f51
commit d2b9b9f094
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
2 changed files with 14 additions and 7 deletions

View File

@ -47,6 +47,7 @@
#include "util/misc.h"
#include "util/QtVersion.h"
#include "util/rstime.h"
#include "util/rsdebug.h"
#include "retroshare/rsgxsflags.h"
#include "retroshare/rsmsgs.h"
@ -55,6 +56,7 @@
#include <iostream>
#include <algorithm>
#include <memory>
/******
* #define ID_DEBUG 1
@ -506,21 +508,26 @@ void IdDialog::updateCircles()
std::cerr << "Retrieving post data for post " << mThreadId << std::endl;
#endif
std::list<RsGroupMetaData> circle_metas ;
/* 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. */
auto circle_metas = std::make_unique<std::list<RsGroupMetaData>>();
if(!rsGxsCircles->getCirclesSummaries(circle_metas))
if(!rsGxsCircles->getCirclesSummaries(*circle_metas))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve circles group info list" << std::endl;
RS_ERR("failed to retrieve circles group info list");
return;
}
}
RsQThreadUtils::postToObject( [circle_metas,this]()
RsQThreadUtils::postToObject(
[circle_metas = std::move(circle_metas), 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 */
loadCircles(circle_metas);
loadCircles(*circle_metas);
}, this );

View File

@ -44,7 +44,7 @@ void postToObject(F &&fun, QObject *obj = qApp)
QObject src;
auto type = obj->metaObject();
QObject::connect( &src, &QObject::destroyed, obj,
[fun, type, obj]
[fun = std::move(fun), type, obj]
{
// ensure that the object is not being destructed
if (obj->metaObject()->inherits(type)) fun();