pgbackrest.git / summary / log / commit / refs

commit    d72d4415a7c79296e3160aede9df59cd5d959694
Author:   David Steele <david@pgmasters.net>
Date:     Mon Aug 23 12:27:07 2021 +0000

    Use contextParentIdx instead of a loop when moving a context.
    
    contextParentIdx was introduced in 90709dfd to improve the performance of mem context frees. memContextMove() did not get the message, however, and continued to use a loop to find the mem context in the old parent.
    
    Use contextParentIdx to find the mem context in the old parent to avoid a loop.


src/common/memContext.c | 27 ++++++--------------------- test/src/module/common/memContextTest.c | 4 +++- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/common/memContext.c b/src/common/memContext.c index 06fc1f7e7..ec502cba8 100644 --- a/src/common/memContext.c +++ b/src/common/memContext.c @@ -537,31 +537,16 @@ memContextMove(MemContext *this, MemContext *parentNew) // Only move if a valid mem context is provided and the old and new parents are not the same if (this != NULL && this->contextParent != parentNew) { - // Find context in the old parent and NULL it out - MemContext *parentOld = this->contextParent; - unsigned int contextIdx; - - for (contextIdx = 0; contextIdx < parentOld->contextChildListSize; contextIdx++) - { - if (parentOld->contextChildList[contextIdx] == this) - { - parentOld->contextChildList[contextIdx] = NULL; - break; - } - } - - // The memory must be found - if (contextIdx == parentOld->contextChildListSize) - THROW(AssertError, "unable to find mem context in old parent"); + // Null out the context in the old parent + ASSERT(this->contextParent->contextChildList[this->contextParentIdx] == this); + this->contextParent->contextChildList[this->contextParentIdx] = NULL; // Find a place in the new parent context and assign it. The child list may be moved while finding a new index so store the // index and use it with (what might be) the new pointer. - contextIdx = memContextNewIndex(parentNew, false); - ASSERT(parentNew->contextChildList[contextIdx] == NULL); - parentNew->contextChildList[contextIdx] = this; - - // Assign new parent this->contextParent = parentNew; + this->contextParentIdx = memContextNewIndex(parentNew, false); + ASSERT(parentNew->contextChildList[this->contextParentIdx] == NULL); + parentNew->contextChildList[this->contextParentIdx] = this; } FUNCTION_TEST_RETURN_VOID(); diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index a1b179931..bd323fb32 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -400,7 +400,9 @@ testRun(void) // Null out the mem context in the parent so the move will fail memContextCurrent()->contextChildList[1] = NULL; - TEST_ERROR(memContextMove(memContext, memContextPrior()), AssertError, "unable to find mem context in old parent"); + TEST_ERROR( + memContextMove(memContext, memContextPrior()), AssertError, + "assertion 'this->contextParent->contextChildList[this->contextParentIdx] == this' failed"); // Set it back so the move will succeed memContextCurrent()->contextChildList[1] = memContext; [parent: 8a9887f27cb4]