1
0
Fork 0

fix: refactor callback batching and optimize memory

- Remove DbCallbackBatcher from exported functions
- Fix memory overhead during clear operations
- Fix memory overhead due to nested batcher stack objects

Co-authored-by: Pedro Filipe Llussa Tavares <pedro.llussa.tavares@tecnico.ulisboa.pt>
This commit is contained in:
Rafael Ribassin Gonçalves Ferreira de Freitas 2026-06-05 21:51:00 +01:00
parent da02660d69
commit 62c08fdaad
11 changed files with 76 additions and 68 deletions

View File

@ -1330,7 +1330,6 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
case DBG_DELETE_AUTO_COMMENT_RANGE:
{
DbCallbackBatcher batcher;
CommentDelRange((duint)param1, (duint)param2, false);
}
break;
@ -1343,7 +1342,6 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
case DBG_DELETE_AUTO_LABEL_RANGE:
{
DbCallbackBatcher batcher;
LabelDelRange((duint)param1, (duint)param2, false);
}
break;
@ -1356,7 +1354,6 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
case DBG_DELETE_AUTO_BOOKMARK_RANGE:
{
DbCallbackBatcher batcher;
BookmarkDelRange((duint)param1, (duint)param2, false);
}
break;
@ -1369,7 +1366,6 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
case DBG_DELETE_AUTO_FUNCTION_RANGE:
{
DbCallbackBatcher batcher;
FunctionDelRange((duint)param1, (duint)param2, false);
}
break;
@ -1496,21 +1492,18 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
case DBG_DELETE_COMMENT_RANGE:
{
DbCallbackBatcher batcher;
CommentDelRange((duint)param1, (duint)param2, true);
}
break;
case DBG_DELETE_LABEL_RANGE:
{
DbCallbackBatcher batcher;
LabelDelRange((duint)param1, (duint)param2, true);
}
break;
case DBG_DELETE_BOOKMARK_RANGE:
{
DbCallbackBatcher batcher;
BookmarkDelRange((duint)param1, (duint)param2, true);
}
break;

View File

@ -143,6 +143,8 @@ void ArgumentDelRange(duint Start, duint End, bool DeleteManual)
Start -= moduleBase;
End -= moduleBase;
DbCallbackBatcher batcher;
arguments.DeleteWhere([ = ](const ARGUMENTSINFO & value)
{
if(!DeleteManual && value.manual)
@ -167,15 +169,11 @@ void ArgumentCacheLoad(JSON Root)
void ArgumentClear()
{
std::vector<ARGUMENTSINFO> allArguments;
ArgumentGetList(allArguments);
arguments.Clear();
for(const auto& argument : allArguments)
DbCallbackBatcher batcher;
arguments.Clear([](const ARGUMENTSINFO & argument)
{
DbCbNotifyArgument(DbOperationType::Remove, argument.modhash, argument.start);
}
});
}
void ArgumentGetList(std::vector<ARGUMENTSINFO> & list)

View File

@ -54,6 +54,7 @@ bool BookmarkDelete(duint Address)
void BookmarkDelRange(duint Start, duint End, bool Manual)
{
DbCallbackBatcher batcher;
bookmarks.DeleteRange(Start, End, Manual, [](const BOOKMARKSINFO & bookmark)
{
DbCbNotifyBookmark(DbOperationType::Remove, bookmark.modhash, bookmark.addr);
@ -78,15 +79,11 @@ bool BookmarkEnum(BOOKMARKSINFO* List, size_t* Size)
void BookmarkClear()
{
std::vector<BOOKMARKSINFO> allBookmarks;
BookmarkGetList(allBookmarks);
bookmarks.Clear();
for(const auto& bookmark : allBookmarks)
DbCallbackBatcher batcher;
bookmarks.Clear([](const BOOKMARKSINFO & bookmark)
{
DbCbNotifyBookmark(DbOperationType::Remove, bookmark.modhash, bookmark.addr);
}
});
}
void BookmarkGetList(std::vector<BOOKMARKSINFO> & list)

View File

@ -118,7 +118,6 @@ bool cbInstrCommentList(int argc, char* argv[])
bool cbInstrCommentClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
CommentClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All comments deleted!"));
@ -195,7 +194,6 @@ bool cbInstrLabelList(int argc, char* argv[])
bool cbInstrLabelClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
LabelClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All labels deleted!"));
@ -279,7 +277,6 @@ bool cbInstrBookmarkList(int argc, char* argv[])
bool cbInstrBookmarkClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
BookmarkClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All bookmarks deleted!"));
@ -374,7 +371,6 @@ bool cbInstrFunctionList(int argc, char* argv[])
bool cbInstrFunctionClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
FunctionClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All functions deleted!"));
@ -466,7 +462,6 @@ bool cbInstrArgumentList(int argc, char* argv[])
bool cbInstrArgumentClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
ArgumentClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All arguments deleted!"));
@ -567,7 +562,6 @@ bool cbInstrLoopList(int argc, char* argv[])
bool cbInstrLoopClear(int argc, char* argv[])
{
DbCallbackBatcher batcher;
LoopClear();
GuiUpdateAllViews();
dputs(QT_TRANSLATE_NOOP("DBG", "All loops deleted!"));

View File

@ -81,6 +81,7 @@ bool CommentDelete(duint Address)
void CommentDelRange(duint Start, duint End, bool Manual)
{
DbCallbackBatcher batcher;
comments.DeleteRange(Start, End, Manual, [](const COMMENTSINFO & comment)
{
DbCbNotifyComment(DbOperationType::Remove, comment.modhash, comment.addr);
@ -105,15 +106,11 @@ bool CommentEnum(COMMENTSINFO* List, size_t* Size)
void CommentClear()
{
std::vector<COMMENTSINFO> allComments;
CommentGetList(allComments);
comments.Clear();
for(const auto& comment : allComments)
DbCallbackBatcher batcher;
comments.Clear([](const COMMENTSINFO & comment)
{
DbCbNotifyComment(DbOperationType::Remove, comment.modhash, comment.addr);
}
});
}
void CommentGetList(std::vector<COMMENTSINFO> & list)

View File

@ -4,21 +4,34 @@ thread_local DbCallbackBatcher* DbCallbackBatcher::tActiveBatcher = nullptr;
DbCallbackBatcher::DbCallbackBatcher()
{
mPrevious = tActiveBatcher;
tActiveBatcher = this;
mPrevious = DbCallbackBatcher::tActiveBatcher;
mOwner = DbCallbackBatcher::tActiveBatcher == nullptr;
mOperations.reserve(DB_MAX_CB_BATCH_SIZE);
mStrings.reserve(DB_MAX_CB_BATCH_SIZE);
if(mOwner)
{
DbCallbackBatcher::tActiveBatcher = this;
mOperations.reserve(DB_MAX_CB_BATCH_SIZE);
mStrings.reserve(DB_MAX_CB_BATCH_SIZE);
}
}
DbCallbackBatcher::~DbCallbackBatcher()
{
this->Flush();
tActiveBatcher = mPrevious;
if(mOwner)
{
this->Flush();
DbCallbackBatcher::tActiveBatcher = mPrevious;
}
}
void DbCallbackBatcher::Add(DbOperation op, const char* text)
{
if(!mOwner && DbCallbackBatcher::tActiveBatcher != nullptr)
{
return DbCallbackBatcher::tActiveBatcher->Add(op, text);
}
if(text != nullptr) // save c string in temporary string vector which will be cleared on flush
{
mStrings.emplace_back(text);
@ -48,7 +61,7 @@ void DbCallbackBatcher::Flush()
DbCallbackBatcher* DbCallbackBatcher::Get()
{
return tActiveBatcher;
return DbCallbackBatcher::tActiveBatcher;
}
void DbCbNotifyLabel(DbOperationType opType, duint modhash, duint address, const char* text, bool manual)
@ -67,7 +80,7 @@ void DbCbNotifyLabel(DbOperationType opType, duint modhash, duint address, const
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -76,6 +89,7 @@ void DbCbNotifyLabel(DbOperationType opType, duint modhash, duint address, const
}
batcher->Add(op, text);
if(manual) batcher->Flush();
}
void DbCbNotifyComment(DbOperationType opType, duint modhash, duint address, const char* text, bool manual)
@ -94,7 +108,7 @@ void DbCbNotifyComment(DbOperationType opType, duint modhash, duint address, con
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -103,6 +117,7 @@ void DbCbNotifyComment(DbOperationType opType, duint modhash, duint address, con
}
batcher->Add(op, text);
if(manual) batcher->Flush();
}
void DbCbNotifyBookmark(DbOperationType opType, duint modhash, duint address, bool manual)
@ -120,7 +135,7 @@ void DbCbNotifyBookmark(DbOperationType opType, duint modhash, duint address, bo
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -129,6 +144,7 @@ void DbCbNotifyBookmark(DbOperationType opType, duint modhash, duint address, bo
}
batcher->Add(op);
if(manual) batcher->Flush();
}
void DbCbNotifyFunction(DbOperationType opType, duint modhash, duint start, duint end, duint instructioncount, duint parent, bool manual)
@ -149,7 +165,7 @@ void DbCbNotifyFunction(DbOperationType opType, duint modhash, duint start, duin
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -158,6 +174,7 @@ void DbCbNotifyFunction(DbOperationType opType, duint modhash, duint start, duin
}
batcher->Add(op);
if(manual) batcher->Flush();
}
void DbCbNotifyArgument(DbOperationType opType, duint modhash, duint start, duint end, duint instructioncount, bool manual)
@ -177,7 +194,7 @@ void DbCbNotifyArgument(DbOperationType opType, duint modhash, duint start, duin
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -186,6 +203,7 @@ void DbCbNotifyArgument(DbOperationType opType, duint modhash, duint start, duin
}
batcher->Add(op);
if(manual) batcher->Flush();
}
void DbCbNotifyLoop(DbOperationType opType, duint modhash, duint start, duint end, duint instructioncount, duint parent, int depth, bool manual)
@ -207,7 +225,7 @@ void DbCbNotifyLoop(DbOperationType opType, duint modhash, duint start, duint en
op.manual = manual;
}
if(batcher == nullptr || manual)
if(batcher == nullptr)
{
PLUG_CB_DBOPERATION callbackInfo = { & op, 1 };
plugincbcall(CB_DBOPERATION, &callbackInfo);
@ -216,4 +234,5 @@ void DbCbNotifyLoop(DbOperationType opType, duint modhash, duint start, duint en
}
batcher->Add(op);
if(manual) batcher->Flush();
}

View File

@ -22,6 +22,7 @@ private:
std::vector<DbOperation> mOperations;
std::vector<std::string> mStrings;
DbCallbackBatcher* mPrevious;
bool mOwner; // if a batcher is defined in a thread where there's already a batcher on the stack, this is set to false
};
void DbCbNotifyLabel(DbOperationType opType, duint modhash, duint address, const char* text = nullptr, bool manual = false);

View File

@ -153,6 +153,8 @@ void FunctionDelRange(duint Start, duint End, bool DeleteManual)
Start -= moduleBase;
End -= moduleBase;
DbCallbackBatcher batcher;
functions.DeleteWhere([ = ](const FUNCTIONSINFO & value)
{
if(!DeleteManual && value.manual)
@ -183,15 +185,11 @@ bool FunctionEnum(FUNCTIONSINFO* List, size_t* Size)
void FunctionClear()
{
std::vector<FUNCTIONSINFO> allFunctions;
FunctionGetList(allFunctions);
functions.Clear();
for(const auto& function : allFunctions)
DbCallbackBatcher batcher;
functions.Clear([](const FUNCTIONSINFO & function)
{
DbCbNotifyFunction(DbOperationType::Remove, function.modhash, function.start);
}
});
}
void FunctionGetList(std::vector<FUNCTIONSINFO> & list)

View File

@ -125,6 +125,7 @@ bool LabelDelete(duint Address)
void LabelDelRange(duint Start, duint End, bool Manual)
{
DbCallbackBatcher batcher;
labels.DeleteRange(Start, End, Manual, [](const LABELSINFO & label)
{
DbCbNotifyLabel(DbOperationType::Remove, label.modhash, label.addr);
@ -158,16 +159,12 @@ void LabelCacheLoad(JSON Root)
void LabelClear()
{
std::vector<LABELSINFO> allLabels;
LabelGetList(allLabels);
labels.Clear();
tempLabels.clear();
for(const auto& label : allLabels)
DbCallbackBatcher batcher;
labels.Clear([](const LABELSINFO & label)
{
DbCbNotifyLabel(DbOperationType::Remove, label.modhash, label.addr);
}
});
tempLabels.clear();
}
void LabelGetList(std::vector<LABELSINFO> & list)

View File

@ -167,6 +167,7 @@ bool LoopOverlaps(int Depth, duint Start, duint End, int* FinalDepth, duint* Fin
static bool LoopDeleteAllRange(const DepthModuleRange & range)
{
DbCallbackBatcher batcher;
auto erased = 0;
for(auto found = loops.find(range); found != loops.end(); found = loops.find(range), erased++)
{
@ -332,9 +333,13 @@ bool LoopEnum(LOOPSINFO* List, size_t* Size)
void LoopClear()
{
EXCLUSIVE_ACQUIRE(LockLoops);
DbCallbackBatcher batcher;
std::map<DepthModuleRange, LOOPSINFO, DepthModuleRangeCompare> empty;
std::swap(loops, empty);
{
EXCLUSIVE_ACQUIRE(LockLoops);
std::swap(loops, empty);
}
for(auto & itr : empty)
{

View File

@ -165,11 +165,20 @@ public:
return getWhere(predicate, nullptr);
}
void Clear()
void Clear(std::function<void(const TValue&)> onEvict = nullptr)
{
EXCLUSIVE_ACQUIRE(TLock);
TMap empty;
std::swap(mMap, empty);
{
EXCLUSIVE_ACQUIRE(TLock);
std::swap(mMap, empty);
}
if(onEvict)
{
for(const auto& kv : empty)
onEvict(kv.second);
}
}
void CacheSave(JSON root) const
@ -312,7 +321,7 @@ struct SerializableModuleHashMap : SerializableUnorderedMap<TLock, duint, TValue
// 0x00000000 - 0xFFFFFFFF
if(start == 0 && end == ~0)
{
this->Clear();
this->Clear(onEvict);
}
else
{