Обсуждение: palloc() vs static define?

Поиск
Список
Период
Сортировка

palloc() vs static define?

От
The Hermit Hacker
Дата:
in commands/cluster.c, in function cluster, we define NewIndexName as:

char    NewIndexName[NAMEDATALEN]; /* line 93 */

in function copy_index, we define it as:

char          *NewIndexName; /* line 246 */

And then palloc(NAMEDATALEN) before it gets used...

Now, which we use doesn't much matter to me, but I would think some sort
of consistency would be in order...or am I missing something as far as
each are concerned?  Is one method inheriently faster then another, or do
they have about the same performance characteristics?

Marc G. Fournier                                
Systems Administrator @ hub.org 
primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org 



Re: [HACKERS] palloc() vs static define?

От
Bruce Momjian
Дата:
> 
> in commands/cluster.c, in function cluster, we define NewIndexName as:
> 
> char    NewIndexName[NAMEDATALEN]; /* line 93 */
> 
> in function copy_index, we define it as:
> 
> char          *NewIndexName; /* line 246 */
> 
> And then palloc(NAMEDATALEN) before it gets used...
> 
> Now, which we use doesn't much matter to me, but I would think some sort
> of consistency would be in order...or am I missing something as far as
> each are concerned?  Is one method inheriently faster then another, or do
> they have about the same performance characteristics?

Uh, O, Marc is coding.  :-)

cluster.c was written by a hack, and has been cleaned up over time.  You
can use either method.  

> char    NewIndexName[NAMEDATALEN]; /* line 93 */

Allocated stack space on function entry, and releases it on function
exit.

> And then palloc(NAMEDATALEN) before it gets used...

palloc() allocates at function call time, and you have to pfree it, or
wait for transaction to pfree it.  If you are starting/stopping
transactions between palloc() and pfree() you could loose the memory
unless you change to the 'cache' memory context before doing the palloc
and pfree.  See other command/*.c files for examples of this.

--  Bruce Momjian                        |  http://www.op.net/~candle maillist@candle.pha.pa.us            |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026
 


Re: [HACKERS] palloc() vs static define?

От
Tom Lane
Дата:
The Hermit Hacker <scrappy@hub.org> writes:
> in commands/cluster.c, in function cluster, we define NewIndexName as:
> char    NewIndexName[NAMEDATALEN]; /* line 93 */
> in function copy_index, we define it as:
> char          *NewIndexName; /* line 246 */
> And then palloc(NAMEDATALEN) before it gets used...

> Now, which we use doesn't much matter to me, but I would think some sort
> of consistency would be in order...or am I missing something as far as
> each are concerned?  Is one method inheriently faster then another, or do
> they have about the same performance characteristics?

The first method (char name[SIZE]) is certainly far faster, since it is
just allocating local variable space during function entry.  In fact,
it's probably effectively *free*, zero cycles.  If you allocate any
local variable space in a function, then allocating more just means
subtracting a larger constant from the stack pointer.  palloc() is going
to go through the memory management code which is certainly not cheap
by comparison.

On the other hand, palloc easily supports asking for a variable amount
of space, whereas local array variables have to have a compile-time-
constant size.  (We ignore GNU-only language extensions here ;-).)
For this particular situation that's not a concern, but other places
you might have to use palloc.

The really critical issue is what lifetime the allocated space needs
to have.  A local array var will go away automatically at function
exit; palloc'd space lives until you pfree it or it gets cleaned up
during transaction exit.

In this particular situation, the local array var approach looks better
to me, but I wonder whether index_create is expecting to be handed a
pointer to persistent storage...
        regards, tom lane