I wrote:
> I wonder whether this would be a net negative for many use-cases.
> Creating and dropping a memory context is hardly free. Have you
> done any performance measurements on cases that don't eat enough
> memory to be OOM hazards?
I did this simple test:
\timing on
do $$
declare arr text[] default array_fill(''::text, ARRAY[50]);
begin
for i in 1..1000000 loop
perform xmlelement(name a, arr);
end loop;
end$$;
With HEAD this takes about 6400ms for me, with the patch
about 6700ms. (These numbers are in an assert-enabled
build, so take them with a grain of salt.) That seems
like it's not really enough of a hit to justify contorting
the code, so consider this objection withdrawn.
Another thing I noticed is that we are doing the type-output
function lookup over again for each array element, though
with some refactoring we could do it just once. Since that
lookup is skipped for "bool" elements, I compared
regression=# select length(xmlelement(name a, array_fill(true,
ARRAY[16000000]))::text);
length
-----------
368000007
(1 row)
Time: 1196.610 ms (00:01.197)
regression=# select length(xmlelement(name a, array_fill(''::text,
ARRAY[16000000]))::text);
length
-----------
304000007
(1 row)
Time: 2005.366 ms (00:02.005)
Now the time taken by textout is not zero, but it still looks like
close to 50% of the runtime is going into those repeat lookups.
Maybe that's enough to worry about?
regards, tom lane