Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r04li-0038K3-87 for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 18:56:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r04kh-000hCN-0D for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 18:55:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r04kg-000hCF-MV for pgsql-hackers@lists.postgresql.org; Mon, 06 Nov 2023 18:55:10 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r04kZ-004HJ6-69 for pgsql-hackers@lists.postgresql.org; Mon, 06 Nov 2023 18:55:08 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2c594196344so64814431fa.3 for ; Mon, 06 Nov 2023 10:55:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699296901; x=1699901701; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=FrCoeGOY66QgRMXJleKD4toqt4uxDD2OQ7GxqJwMvNU=; b=W2WiPq3Ff/e6mzMxqdcXqY5FALB+l2Zga6B6KyiMADSktLVuyqurjrxiQDqRr9lgMi eQnmPdwe6R9wNhp0Vk+usc6CdW8G36V0/nhmYM1Q4/AvyNE6HUGwQlpWDhOFUzRHxUe6 XtBRyIf3sg9pUT5/u1bNZtKifkg4wrYrfVCUyIB+7tAS1Ct1ymON5O56RlQa0GJXL7iX g8z329UteZWGdFcTGrZMu+HmW3NdgaYCQSjbBEHh4q5FOlNn4/3pzVOaovlMl1FizhfK pOV6tD6joeHXQNiDocCgCw8WNYZBkoL+p6cKUmNBiV9fj26c55yAY1yUtuWoEr+mXj1i 9ZzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699296901; x=1699901701; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FrCoeGOY66QgRMXJleKD4toqt4uxDD2OQ7GxqJwMvNU=; b=rSveOAuA51oOHZMiUAjH8/Vww3VtLKs4KtyH5jZST5b2XhNwrOtHEQGo0lkGpPW4TU 7QtyH5Te/RuH/0XuHHuR7CYoSqS4Q/Q/dbx+dvD3NhFixmR5ZZZFULG174zaxw/mHy5b 2CP2eIcuJhneikVyUm2Psk0ntvNdp03UIZd791jzpH8NPpt+q4jB/EFg/C61/WfDx05E 0xUCaQaKjMyjILb3zCnHyhvcksjNQ4Di00TRcc3n4IZol/bUlGz7a/hmelQtu3aEQfAZ +0GEDI9OE8S0vWb9dzeMxYEuDNfJjfj3QTnvZXyzC20HGwuAo/ZjtN87q/yY+fusM4yg Qsbw== X-Gm-Message-State: AOJu0YwpF1IlnGNpW1vpqE+bW156qlLQc46atts2lzc815Atvk3x1/zu K9M6ZLqHF6tVK8YnvB8NRzMpIK9fG897CQsOP40= X-Google-Smtp-Source: AGHT+IFMukNaQmnEDGkVfSF5reqZtaOEIM3ozoe7oOf/7Zqq4xw2Uy2GGPP9v0DvyF1FjqP+zDg01ZHzqxXWR3ToaOU= X-Received: by 2002:a2e:3c12:0:b0:2c5:1542:57e9 with SMTP id j18-20020a2e3c12000000b002c5154257e9mr26144566lja.41.1699296901340; Mon, 06 Nov 2023 10:55:01 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Matthias van de Meent Date: Mon, 6 Nov 2023 19:54:49 +0100 Message-ID: Subject: Re: Add bump memory context type and use it for tuplesorts To: David Rowley Cc: PostgreSQL Developers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 11 Jul 2023 at 01:51, David Rowley wrote: > > On Tue, 27 Jun 2023 at 21:19, David Rowley wrote: > > I've attached the bump allocator patch and also the script I used to > > gather the performance results in the first 2 tabs in the attached > > spreadsheet. > > I've attached a v2 patch which changes the BumpContext a little to > remove some of the fields that are not really required. There was no > need for the "keeper" field as the keeper block always comes at the > end of the BumpContext as these are allocated in a single malloc(). > The pointer to the "block" also isn't really needed. This is always > the same as the head element in the blocks dlist. Neat idea, +1. I think it would make sense to split the "add a bump allocator" changes from the "use the bump allocator in tuplesort" patches. Tangent: Do we have specific notes on worst-case memory usage of memory contexts with various allocation patterns? This new bump allocator seems to be quite efficient, but in a worst-case allocation pattern it can still waste about 1/3 of its allocated memory due to never using free space on previous blocks after an allocation didn't fit on that block. It probably isn't going to be a huge problem in general, but this seems like something that could be documented as a potential problem when you're looking for which allocator to use and compare it with other allocators that handle different allocation sizes more gracefully. > +++ b/src/backend/utils/mmgr/bump.c > +BumpBlockIsEmpty(BumpBlock *block) > +{ > + /* it's empty if the freeptr has not moved */ > + return (block->freeptr == (char *) block + Bump_BLOCKHDRSZ); > [...] > +static inline void > +BumpBlockMarkEmpty(BumpBlock *block) > +{ > +#if defined(USE_VALGRIND) || defined(CLOBBER_FREED_MEMORY) > + char *datastart = ((char *) block) + Bump_BLOCKHDRSZ; These two use different definitions of the start pointer. Is that deliberate? > +++ b/src/include/utils/tuplesort.h > @@ -109,7 +109,8 @@ typedef struct TuplesortInstrumentation > * a pointer to the tuple proper (might be a MinimalTuple or IndexTuple), > * which is a separate palloc chunk --- we assume it is just one chunk and > * can be freed by a simple pfree() (except during merge, when we use a > - * simple slab allocator). SortTuples also contain the tuple's first key > + * simple slab allocator and when performing a non-bounded sort where we > + * use a bump allocator). SortTuples also contain the tuple's first key I'd go with something like the following: + * ...(except during merge *where* we use a + * simple slab allocator, and during a non-bounded sort where we + * use a bump allocator). Kind regards, Matthias van de Meent Neon (https://neon.tech)