Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aaa3a0c

Browse files
committed
Split MemSet into three parts to constant comparisons can be optimized
away by the compiler; used by palloc0.
1 parent 266c367 commit aaa3a0c

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/backend/utils/mmgr/mcxt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.35 2002/11/10 02:17:25 momjian Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.36 2002/11/13 00:37:06 momjian Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -453,14 +453,14 @@ MemoryContextAlloc(MemoryContext context, Size size)
453453
}
454454

455455
/*
456-
* MemoryContextAllocZero
456+
* MemoryContextAllocPalloc0
457457
* Like MemoryContextAlloc, but clears allocated memory
458458
*
459459
* We could just call MemoryContextAlloc then clear the memory, but this
460460
* function is called too many times, so we have a separate version.
461461
*/
462462
void *
463-
MemoryContextAllocZero(MemoryContext context, Size size)
463+
MemoryContextAllocPalloc0(MemoryContext context, Size size)
464464
{
465465
void *ret;
466466

@@ -471,7 +471,7 @@ MemoryContextAllocZero(MemoryContext context, Size size)
471471
(unsigned long) size);
472472

473473
ret = (*context->methods->alloc) (context, size);
474-
MemSet(ret, 0, size);
474+
MemSetLoop(ret, 0, size);
475475
return ret;
476476
}
477477

src/include/c.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: c.h,v 1.131 2002/11/11 03:02:19 momjian Exp $
15+
* $Id: c.h,v 1.132 2002/11/13 00:37:06 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -577,26 +577,36 @@ typedef NameData *Name;
577577
* memset() functions. More research needs to be done, perhaps with
578578
* platform-specific MEMSET_LOOP_LIMIT values or tests in configure.
579579
*
580+
* MemSet has been split into two parts so MemSetTest can be optimized
581+
* away for constant 'val' and 'len'. This is used by palloc0().
582+
*
583+
* Note, arguments are evaluated more than once.
584+
*
580585
* bjm 2002-10-08
581586
*/
582-
#define MemSet(start, val, len) \
583-
do \
584-
{ \
585-
int32 * _start = (int32 *) (start); \
586-
int _val = (val); \
587-
Size _len = (len); \
587+
#define MemSetTest(val, len) \
588+
( ((len) & INT_ALIGN_MASK) == 0 && \
589+
(len) <= MEMSET_LOOP_LIMIT && \
590+
(val) == 0 )
591+
592+
#define MemSetLoop(start, val, len) \
593+
do \
594+
{ \
595+
int32 * _start = (int32 *) (start); \
596+
int32 * _stop = (int32 *) ((char *) _start + (len)); \
588597
\
589-
if ((( ((long) _start) | _len) & INT_ALIGN_MASK) == 0 && \
590-
_val == 0 && \
591-
_len <= MEMSET_LOOP_LIMIT) \
592-
{ \
593-
int32 * _stop = (int32 *) ((char *) _start + _len); \
594-
while (_start < _stop) \
595-
*_start++ = 0; \
596-
} \
597-
else \
598-
memset((char *) _start, _val, _len); \
599-
} while (0)
598+
while (_start < _stop) \
599+
*_start++ = 0; \
600+
} while (0)
601+
602+
#define MemSet(start, val, len) \
603+
do \
604+
{ \
605+
if (MemSetTest(val, len) && ((long)(start) & INT_ALIGN_MASK) == 0 ) \
606+
MemSetLoop(start, val, len); \
607+
else \
608+
memset((char *)(start), (val), (len)); \
609+
} while (0)
600610

601611
#define MEMSET_LOOP_LIMIT 1024
602612

src/include/utils/palloc.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
2222
* Portions Copyright (c) 1994, Regents of the University of California
2323
*
24-
* $Id: palloc.h,v 1.22 2002/11/10 02:17:25 momjian Exp $
24+
* $Id: palloc.h,v 1.23 2002/11/13 00:37:06 momjian Exp $
2525
*
2626
*-------------------------------------------------------------------------
2727
*/
@@ -46,11 +46,15 @@ extern DLLIMPORT MemoryContext CurrentMemoryContext;
4646
* Fundamental memory-allocation operations (more are in utils/memutils.h)
4747
*/
4848
extern void *MemoryContextAlloc(MemoryContext context, Size size);
49-
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
49+
extern void *MemoryContextAllocPalloc0(MemoryContext context, Size size);
5050

5151
#define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz))
5252

53-
#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz))
53+
/* We assume palloc() is already int-aligned */
54+
#define palloc0(sz) \
55+
( MemSetTest(0, (sz)) ? \
56+
MemoryContextAllocPalloc0(CurrentMemoryContext, (sz)) : \
57+
memset(palloc(sz), 0, (sz)))
5458

5559
extern void pfree(void *pointer);
5660

0 commit comments

Comments
 (0)