Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit25c5392

Browse files
committed
Improve performance in freeing memory contexts
The single linked list of memory contexts could result in O(N^2)performance to free a set of contexts if they were not freed inreverse order of creation. In many cases the reverse order wasused, but there were some significant exceptions that caused real-world performance problems. Rather than requiring all callers tocare about the order in which contexts were freed, and hunting downand changing all existing cases where the wrong order was used, weadd one pointer per memory context so that the implementationdetails are not so visible.Jan Wieck
1 parent521f045 commit25c5392

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,34 +331,33 @@ MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
331331
{
332332
MemoryContextparent=context->parent;
333333

334-
if (context==parent->firstchild)
335-
parent->firstchild=context->nextchild;
334+
if (context->prevchild!=NULL)
335+
context->prevchild->nextchild=context->nextchild;
336336
else
337337
{
338-
MemoryContextchild;
339-
340-
for (child=parent->firstchild;child;child=child->nextchild)
341-
{
342-
if (context==child->nextchild)
343-
{
344-
child->nextchild=context->nextchild;
345-
break;
346-
}
347-
}
338+
Assert(parent->firstchild==context);
339+
parent->firstchild=context->nextchild;
348340
}
341+
342+
if (context->nextchild!=NULL)
343+
context->nextchild->prevchild=context->prevchild;
349344
}
350345

351346
/* And relink */
352347
if (new_parent)
353348
{
354349
AssertArg(MemoryContextIsValid(new_parent));
355350
context->parent=new_parent;
351+
context->prevchild=NULL;
356352
context->nextchild=new_parent->firstchild;
353+
if (new_parent->firstchild!=NULL)
354+
new_parent->firstchild->prevchild=context;
357355
new_parent->firstchild=context;
358356
}
359357
else
360358
{
361359
context->parent=NULL;
360+
context->prevchild=NULL;
362361
context->nextchild=NULL;
363362
}
364363
}
@@ -714,6 +713,7 @@ MemoryContextCreate(NodeTag tag, Size size,
714713
node->methods=methods;
715714
node->parent=NULL;/* for the moment */
716715
node->firstchild=NULL;
716+
node->prevchild=NULL;
717717
node->nextchild=NULL;
718718
node->isReset= true;
719719
node->name= ((char*)node)+size;
@@ -728,6 +728,8 @@ MemoryContextCreate(NodeTag tag, Size size,
728728
{
729729
node->parent=parent;
730730
node->nextchild=parent->firstchild;
731+
if (parent->firstchild!=NULL)
732+
parent->firstchild->prevchild=node;
731733
parent->firstchild=node;
732734
/* inherit allowInCritSection flag from parent */
733735
node->allowInCritSection=parent->allowInCritSection;

‎src/include/nodes/memnodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct MemoryContextData
7979
MemoryContextMethods*methods;/* virtual function table */
8080
MemoryContextparent;/* NULL if no parent (toplevel context) */
8181
MemoryContextfirstchild;/* head of linked list of children */
82+
MemoryContextprevchild;/* previous child of same parent */
8283
MemoryContextnextchild;/* next child of same parent */
8384
char*name;/* context name (just for debugging) */
8485
MemoryContextCallback*reset_cbs;/* list of reset/delete callbacks */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp