1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Add reset to temp memory contexts to save memory and processing time.

Processing large datasets in a memory context can lead to high memory usage and long allocation times.  Add a new MEM_CONTEXT_TEMP_RESET_BEGIN() macro that allows temp allocations to be automatically freed after N iterations.
This commit is contained in:
David Steele 2019-08-19 21:16:10 -04:00
parent 7d97d49f41
commit d411321d28
2 changed files with 36 additions and 0 deletions

View File

@ -170,6 +170,29 @@ MEM_CONTEXT_TEMP_END();
\
MEM_CONTEXT_BEGIN(MEM_CONTEXT_TEMP())
#define MEM_CONTEXT_TEMP_RESET_BEGIN() \
{ \
MemContext *MEM_CONTEXT_TEMP() = memContextNew("temporary"); \
unsigned int MEM_CONTEXT_TEMP_loopTotal = 0; \
\
MEM_CONTEXT_BEGIN(MEM_CONTEXT_TEMP())
#define MEM_CONTEXT_TEMP_RESET(resetTotal) \
do \
{ \
MEM_CONTEXT_TEMP_loopTotal++; \
\
if (MEM_CONTEXT_TEMP_loopTotal >= resetTotal) \
{ \
memContextSwitch(MEM_CONTEXT_OLD()); \
memContextFree(MEM_CONTEXT_TEMP()); \
MEM_CONTEXT_TEMP() = memContextNew("temporary"); \
memContextSwitch(MEM_CONTEXT_TEMP()); \
MEM_CONTEXT_TEMP_loopTotal = 0; \
} \
} \
while (0)
#define MEM_CONTEXT_TEMP_END() \
/* Switch back to the old context and free temp context */ \
FINALLY() \

View File

@ -293,6 +293,19 @@ testRun(void)
AssertError, "error in test block");
TEST_RESULT_STR(memContextName(memContextCurrent()), "TOP", "context is now top");
// Reset temp mem context after a single interation
// -------------------------------------------------------------------------------------------------------------------------
MEM_CONTEXT_TEMP_RESET_BEGIN()
{
TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, false, "nothing allocated");
memNew(99);
TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, true, "1 allocation");
MEM_CONTEXT_TEMP_RESET(1);
TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, false, "nothing allocated");
}
MEM_CONTEXT_TEMP_END();
}
// *****************************************************************************************************************************