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 1qTerr-00AGIQ-Vq for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Aug 2023 08:48:36 +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 1qTerq-003tTJ-Gf for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Aug 2023 08:48:34 +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 1qTeoj-003q4V-Nc for pgsql-hackers@lists.postgresql.org; Wed, 09 Aug 2023 08:45:22 +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 1qTeoN-001MNw-9U for pgsql-hackers@postgresql.org; Wed, 09 Aug 2023 08:45:20 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2b9e6cc93d8so104462451fa.0 for ; Wed, 09 Aug 2023 01:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691570697; x=1692175497; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Ynm972AS/xqIelzSwIagdBI7KYTKnd6ADhSo1t65Hs0=; b=gpw1obdtdrkUSzCvbykMMBjWUu7grIPefqetv4RcKiFRTy9sLWgDEF3u8SgawAkI4s qQWRR9XeV4sugOuMGk9JEybJqT+cocBwDg7UaRDT0RqAkXY+9blsYfJ+K/+WWMA8EChn 2Lv8LddZEEoIGzNisEeBqkDHqtveN8fXjXsc04m/lrUB22eRIUQBWLkO+Dw+Xdagy+0G u82xhJ+q80OEUqkjPDbGZjn9iFWkb1syiq4VV/KIPS2sOplv7Vf4xbKn/Hq0psKBDRdA 132BtA4zNDt3Y35OIbDZdQXJIUgmpwg+GBPDFlkEdvBC0JIOkXopvAgA9YQOwLiQ5qiQ jE/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691570697; x=1692175497; 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=Ynm972AS/xqIelzSwIagdBI7KYTKnd6ADhSo1t65Hs0=; b=WckHbtGWujC5PIZZGYeHJdZxKQxkLx75PSFMXpl6Vv4Zo0EiB8XC7i/dvEi+jEep8f DSWmZlTYkfsCddvEqXAhr9GQ7e2xqUiWGLQcGSykOYHYo/JaZXDpX57WxfFIpQjwJl1/ xl36hMhBJAXKQgOgdllUu/A3PN8buTw0ELSkz7LvC0Xw0IJnlw4u5GAUxnrVR70zaoz/ CQstUThAXr9cLEc/E+SZutv4S1OfwDqkV/Xl2g1IjXAPZqNxDBgS8rtgFxUMouSf5fNf Zk+eYtY6PvNg2U4odG1NcCcXEWY1P7YhG4OEs2IRo2Kv8vcXORZdXOLd4U69sd/HSovH eu/Q== X-Gm-Message-State: AOJu0YzNojSwT63KsQmLtcbSbX2v2N2SpZGGelkkh4T08p9D44CPaGRU TYgdogMnmKJ9JX6gDbR1w1diGj4jDnZIyV18ruYOU298jyY= X-Google-Smtp-Source: AGHT+IH6r+5QorHcUqvQpY30JDX1bamViBCqxwc4mrkBPZGdCdcZZ26/GGHChpz5IyemsnRmd2ZmU75aqaSig9Mj4aw= X-Received: by 2002:a2e:3a03:0:b0:2b4:83c3:d285 with SMTP id h3-20020a2e3a03000000b002b483c3d285mr1220395lja.38.1691570697095; Wed, 09 Aug 2023 01:44:57 -0700 (PDT) MIME-Version: 1.0 References: <20210719195950.gavgs6ujzmjfaiig@alap3.anarazel.de> <20230719085236.jltxal2eztqrprfg@awork3.anarazel.de> In-Reply-To: From: David Rowley Date: Wed, 9 Aug 2023 20:44:43 +1200 Message-ID: Subject: Re: Avoid stack frame setup in performance critical routines using tail calls To: Andres Freund Cc: pgsql-hackers@postgresql.org, Robert Haas , Michael Paquier , Tomas Vondra Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 21 Jul 2023 at 14:03, David Rowley wrote: > I'll reply back with a more detailed review next week. Here's a review of v2-0001: 1. /* * XXX: Should this also be moved into alloc()? We could possibly avoid * zeroing in some cases (e.g. if we used mmap() ourselves. */ MemSetAligned(ret, 0, size); Maybe this should be moved to the alloc function. It would allow us to get rid of this: #define palloc0fast(sz) \ ( MemSetTest(0, sz) ? \ MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \ MemoryContextAllocZero(CurrentMemoryContext, sz) ) If we do the zeroing inside the alloc function then it can always use the MemoryContextAllocZeroAligned version providing we zero before setting the sentinel byte. It would allow the tail call in the palloc0() case, but the drawback would be having to check for the MCXT_ALLOC_ZERO flag in the alloc function. I wonder if that branch would be predictable in most cases, e.g. the parser will be making lots of nodes and want to zero all allocations, but the executor won't be doing much of that. There will be a mix of zeroing and not zeroing in the planner, mostly not, I think. 2. Why do you need to add the NULL check here? #ifdef USE_VALGRIND - if (method != MCTX_ALIGNED_REDIRECT_ID) + if (ret != NULL && method != MCTX_ALIGNED_REDIRECT_ID) VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size); #endif I know it's just valgrind code and performance does not matter, but the realloc flags are being passed as 0, so allocation failures won't return. 3. /* * XXX: Probably no need to check for huge allocations, we only support * one size? Which could theoretically be huge, but that'd not make * sense... */ They can't be huge per Assert(fullChunkSize <= MEMORYCHUNK_MAX_VALUE) in SlabContextCreate(). 4. It would be good to see some API documentation in the MemoryContextMethods struct. This adds a lot of responsibility onto the context implementation without any extra documentation to explain what, for example, palloc is responsible for and what the alloc function needs to do itself. David