Обсуждение: Should CreateExprContext() be using ALLOCSET_DEFAULT_SIZES?

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

Should CreateExprContext() be using ALLOCSET_DEFAULT_SIZES?

От
Andres Freund
Дата:
Hi,

The thread around https://postgr.es/m/CADUqk8Uqw5QaUqLdd-0SBCvZVncrE3JMJB9+yDwO_uMv_hTYCg@mail.gmail.com
reminded me of the following:

ISTM that we really shouldn't use ALLOCSET_DEFAULT_SIZES for expression
contexts, as they most commonly see only a few small, or no, allocations.

That's true for OLTPish queries, but is is very often true even for analytics
queries.

Just because I had it loaded, here's the executor state for TPCH-Q01, which is
pretty expression heavy:

ExecutorState: 65536 total in 4 blocks; 42512 free (11 chunks); 23024 used
  TupleSort main: 32832 total in 2 blocks; 7320 free (7 chunks); 25512 used
    TupleSort sort: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
      Caller tuples: 8192 total in 1 blocks (9 chunks); 6488 free (0 chunks); 1704 used
  ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
  ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
  ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
Grand total: 139328 bytes in 11 blocks; 88032 free (18 chunks); 51296 used

As you can see very little was allocated in the ExprContext's.


ISTM that we could save a reasonable amount of memory by using a smaller
initial size.

Not so sure if a smaller max size should be used though.

Greetings,

Andres Freund



Re: Should CreateExprContext() be using ALLOCSET_DEFAULT_SIZES?

От
Daniel Gustafsson
Дата:
> On 17 Feb 2023, at 05:01, Andres Freund <andres@anarazel.de> wrote:

> ISTM that we really shouldn't use ALLOCSET_DEFAULT_SIZES for expression
> contexts, as they most commonly see only a few small, or no, allocations.

Looking into this I think you are correct.

> ISTM that we could save a reasonable amount of memory by using a smaller
> initial size.

I experimented with the below trivial patch in CreateExprContext:

-       return CreateExprContextInternal(estate, ALLOCSET_DEFAULT_SIZES);
+       return CreateExprContextInternal(estate, ALLOCSET_START_SMALL_SIZES);

Across various (unscientific) benchmarks, including expression heavy TPC-H
queries, I can see consistent reductions in memory use and tiny (within the
margin of error) increases in performance.  More importantly, I didn't see a
case of slowdowns with this applied or any adverse effects in terms of memory
use.  Whenever the initial size isn't big enough the expr runtime is likely
exceeding the overhead from growing the allocation?

--
Daniel Gustafsson