Re: Add bump memory context type and use it for tuplesorts

2024-04-16 Thread Amul Sul
On Tue, Apr 16, 2024 at 3:44 PM David Rowley wrote: > On Tue, 16 Apr 2024 at 17:13, Amul Sul wrote: > > Attached is a small patch adding the missing BumpContext description to > the > > README. > > Thanks for noticing and working on the patch. > > There were a few things that were not quite

Re: Add bump memory context type and use it for tuplesorts

2024-04-16 Thread David Rowley
On Mon, 8 Apr 2024 at 00:37, David Rowley wrote: > I've now pushed all 3 patches. Thank you for all the reviews on > these and for the extra MemoryContextMethodID bit, Matthias. I realised earlier today when working on [1] that bump makes a pretty brain-dead move when adding dedicated blocks

Re: Add bump memory context type and use it for tuplesorts

2024-04-16 Thread David Rowley
On Tue, 16 Apr 2024 at 17:13, Amul Sul wrote: > Attached is a small patch adding the missing BumpContext description to the > README. Thanks for noticing and working on the patch. There were a few things that were not quite accurate or are misleading: 1. > +These three memory contexts aim to

Re: Add bump memory context type and use it for tuplesorts

2024-04-16 Thread Daniel Gustafsson
> On 16 Apr 2024, at 07:12, Amul Sul wrote: > > Attached is a small patch adding the missing BumpContext description to the > README. Nice catch, we should add it to the README. + pfree'd or realloc'd. I think it's best to avoid mixing API:s, "pfree'd or repalloc'd" keeps it to functions in

Re: Add bump memory context type and use it for tuplesorts

2024-04-15 Thread Amul Sul
Attached is a small patch adding the missing BumpContext description to the README. Regards, Amul 0001-Add-BumpContext-description-to-mmgr-README.patch Description: Binary data

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Daniel Gustafsson
> On 8 Apr 2024, at 01:04, Andres Freund wrote: > What makes you think that's unrelated? To me that looks like the same issue? Nvm, I misread the assert, ETOOLITTLESLEEP. Sorry for the noise. -- Daniel Gustafsson

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread David Rowley
On Mon, 8 Apr 2024 at 09:09, Andres Freund wrote: > I suspect that KeeperBlock() isn't returning true, because IsKeeperBlock > misses > the MAXALIGN(). I think that about fits with: Thanks for investigating that. I've just pushed a fix for the macro and also adjusted a location which was

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Andres Freund
Hi, On 2024-04-08 00:55:42 +0200, Daniel Gustafsson wrote: > > On 8 Apr 2024, at 00:41, Tom Lane wrote: > > > > Tomas Vondra writes: > >> Yup, changing it to this: > > > >> #define KeeperBlock(set) ((BumpBlock *) ((char *) (set) + > >> MAXALIGN(sizeof(BumpContext > > > >> fixes the issue

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Daniel Gustafsson
> On 8 Apr 2024, at 00:41, Tom Lane wrote: > > Tomas Vondra writes: >> Yup, changing it to this: > >> #define KeeperBlock(set) ((BumpBlock *) ((char *) (set) + >> MAXALIGN(sizeof(BumpContext > >> fixes the issue for me. > > Mamba is happy with that change, too. Unrelated to that one,

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Tom Lane
Tomas Vondra writes: > Yup, changing it to this: > #define KeeperBlock(set) ((BumpBlock *) ((char *) (set) + > MAXALIGN(sizeof(BumpContext > fixes the issue for me. Mamba is happy with that change, too. regards, tom lane

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Tomas Vondra
On 4/7/24 23:09, Andres Freund wrote: > Hi, > > On 2024-04-07 22:35:47 +0200, Tomas Vondra wrote: >> I haven't investigated, but I'd considering it works on 64-bit, I guess >> it's not considering alignment somewhere. I can dig more if needed. > > I think I may the problem: > > > #define

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Andres Freund
Hi, On 2024-04-07 22:35:47 +0200, Tomas Vondra wrote: > I haven't investigated, but I'd considering it works on 64-bit, I guess > it's not considering alignment somewhere. I can dig more if needed. I think I may the problem: #define KeeperBlock(set) ((BumpBlock *) ((char *) (set) +

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Tomas Vondra
On 4/7/24 22:35, Tomas Vondra wrote: > On 4/7/24 14:37, David Rowley wrote: >> On Sun, 7 Apr 2024 at 22:05, John Naylor wrote: >>> >>> On Sat, Apr 6, 2024 at 7:37 PM David Rowley wrote: >>> I'm planning on pushing these, pending a final look at 0002 and 0003 on Sunday morning NZ

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread Tomas Vondra
On 4/7/24 14:37, David Rowley wrote: > On Sun, 7 Apr 2024 at 22:05, John Naylor wrote: >> >> On Sat, Apr 6, 2024 at 7:37 PM David Rowley wrote: >>> >> I'm planning on pushing these, pending a final look at 0002 and 0003 >>> on Sunday morning NZ time (UTC+12), likely in about 10 hours time. >>

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread David Rowley
On Sun, 7 Apr 2024 at 22:05, John Naylor wrote: > > On Sat, Apr 6, 2024 at 7:37 PM David Rowley wrote: > > > I'm planning on pushing these, pending a final look at 0002 and 0003 > > on Sunday morning NZ time (UTC+12), likely in about 10 hours time. > > +1 I've now pushed all 3 patches. Thank

Re: Add bump memory context type and use it for tuplesorts

2024-04-07 Thread John Naylor
On Sat, Apr 6, 2024 at 7:37 PM David Rowley wrote: > I'm planning on pushing these, pending a final look at 0002 and 0003 > on Sunday morning NZ time (UTC+12), likely in about 10 hours time. +1 I haven't looked at v6, but I've tried using it in situ, and it seems to work as well as hoped:

Re: Add bump memory context type and use it for tuplesorts

2024-04-06 Thread Matthias van de Meent
On Sun, 7 Apr 2024, 01:59 David Rowley, wrote: > On Sun, 7 Apr 2024 at 05:45, Matthias van de Meent > wrote: > > Malloc's docs specify the minimum chunk size at 4*sizeof(void*) and > itself uses , so using powers of 2 for chunks would indeed fail to detect > 1s in the 4th bit. I suspect you'll

Re: Add bump memory context type and use it for tuplesorts

2024-04-06 Thread David Rowley
On Sun, 7 Apr 2024 at 05:45, Matthias van de Meent wrote: > Malloc's docs specify the minimum chunk size at 4*sizeof(void*) and itself > uses , so using powers of 2 for chunks would indeed fail to detect 1s in the > 4th bit. I suspect you'll get different results when you check the allocation

Re: Add bump memory context type and use it for tuplesorts

2024-04-06 Thread Matthias van de Meent
On Sat, 6 Apr 2024, 14:36 David Rowley, wrote: > On Sat, 6 Apr 2024 at 02:30, Matthias van de Meent > wrote: > > > > On Thu, 4 Apr 2024 at 22:43, Tom Lane wrote: > > > > > > Matthias van de Meent writes: > > > > It extends memory context IDs to 5 bits (32 values), of which > > > > - 8 have

Re: Add bump memory context type and use it for tuplesorts

2024-04-05 Thread David Rowley
On Sat, 6 Apr 2024 at 03:24, Tom Lane wrote: > OK. I did not read the patch very closely, but at least in principle > I have no further objections. David, are you planning to take point > on getting this in? Yes. I'll be looking soon. David

Re: Add bump memory context type and use it for tuplesorts

2024-04-05 Thread Tom Lane
Matthias van de Meent writes: > On Thu, 4 Apr 2024 at 22:43, Tom Lane wrote: >> The only objection I can think of is that perhaps this would slow >> things down a tad by requiring more complicated shifting/masking. >> I wonder if we could redo the performance checks that were done >> on the way

Re: Add bump memory context type and use it for tuplesorts

2024-04-05 Thread Matthias van de Meent
On Thu, 4 Apr 2024 at 22:43, Tom Lane wrote: > > Matthias van de Meent writes: > > On Mon, 25 Mar 2024 at 22:44, Tom Lane wrote: > >> Basically, I'm not happy with consuming the last reasonably-available > >> pattern for a memory context type that has little claim to being the > >> Last Context

Re: Add bump memory context type and use it for tuplesorts

2024-04-04 Thread Tom Lane
Matthias van de Meent writes: > On Mon, 25 Mar 2024 at 22:44, Tom Lane wrote: >> Basically, I'm not happy with consuming the last reasonably-available >> pattern for a memory context type that has little claim to being the >> Last Context Type We Will Ever Want. Rather than making a further >>

Re: Add bump memory context type and use it for tuplesorts

2024-04-04 Thread Matthias van de Meent
On Mon, 25 Mar 2024 at 22:44, Tom Lane wrote: > > David Rowley writes: > > On Tue, 26 Mar 2024 at 03:53, Tom Lane wrote: > >> Could we move the knowledge of exactly which context type it is out > >> of the per-chunk header and keep it in the block header? > > > I wasn't 100% clear on your

Re: Add bump memory context type and use it for tuplesorts

2024-03-25 Thread Tom Lane
David Rowley writes: > On Tue, 26 Mar 2024 at 03:53, Tom Lane wrote: >> Could we move the knowledge of exactly which context type it is out >> of the per-chunk header and keep it in the block header? > I wasn't 100% clear on your opinion about using 010 vs expanding the > bit-space. Based on

Re: Add bump memory context type and use it for tuplesorts

2024-03-25 Thread David Rowley
On Tue, 26 Mar 2024 at 03:53, Tom Lane wrote: > I agree with this completely. However, the current design for chunk > headers is mighty restrictive about how many kinds of contexts we can > have. We need to open that back up. Andres mentioned how we could do this in [1]. One possible issue

Re: Add bump memory context type and use it for tuplesorts

2024-03-25 Thread Tom Lane
Tomas Vondra writes: > IMHO the idea of having a general purpose memory context and then also > specialized memory contexts for particular allocation patterns is great, > and we should embrace it. Adding more and more special cases into > AllocSet seems to go directly against that idea, makes the

Re: Add bump memory context type and use it for tuplesorts

2024-03-25 Thread Tomas Vondra
On 3/25/24 12:41, David Rowley wrote: > On Tue, 5 Mar 2024 at 15:42, David Rowley wrote: >> The query I ran was: >> >> select chksz,mtype,pg_allocate_memory_test_reset(chksz, >> 1024*1024,1024*1024*1024, mtype) from (values(8),(16),(32),(64)) >>

Re: Add bump memory context type and use it for tuplesorts

2024-03-25 Thread David Rowley
On Tue, 5 Mar 2024 at 15:42, David Rowley wrote: > The query I ran was: > > select chksz,mtype,pg_allocate_memory_test_reset(chksz, > 1024*1024,1024*1024*1024, mtype) from (values(8),(16),(32),(64)) > sizes(chksz),(values('aset'),('generation'),('slab'),('bump')) > cxt(mtype) order by

Re: Add bump memory context type and use it for tuplesorts

2024-03-15 Thread Tomas Vondra
On 3/15/24 03:21, David Rowley wrote: > On Tue, 12 Mar 2024 at 23:57, Tomas Vondra > wrote: >> Attached is an updated version of the mempool patch, modifying all the >> memory contexts (not just AllocSet), including the bump context. And >> then also PDF with results from the two machines,

Re: Add bump memory context type and use it for tuplesorts

2024-03-14 Thread David Rowley
On Tue, 12 Mar 2024 at 23:57, Tomas Vondra wrote: > Attached is an updated version of the mempool patch, modifying all the > memory contexts (not just AllocSet), including the bump context. And > then also PDF with results from the two machines, comparing results > without and with the mempool.

Re: Add bump memory context type and use it for tuplesorts

2024-03-12 Thread John Naylor
On Tue, Mar 12, 2024 at 6:41 AM David Rowley wrote: > Thanks for trying this out. I didn't check if the performance was > susceptible to the memory size before the reset. It certainly would > be once the allocation crosses some critical threshold of CPU cache > size, but probably it will also

Re: Add bump memory context type and use it for tuplesorts

2024-03-11 Thread David Rowley
On Mon, 11 Mar 2024 at 22:09, John Naylor wrote: > I ran the test function, but using 256kB and 3MB for the reset > frequency, and with 8,16,24,32 byte sizes (patched against a commit > after the recent hot/cold path separation). Images attached. I also > get a decent speedup with the bump

Re: Add bump memory context type and use it for tuplesorts

2024-03-11 Thread David Rowley
On Tue, 12 Mar 2024 at 12:25, Tomas Vondra wrote: > (b) slab is considerably slower It would be interesting to modify SlabReset() to, instead of free()ing the blocks, push the first SLAB_MAXIMUM_EMPTY_BLOCKS of them onto the emptyblocks list. That might give us an idea of how much overhead

Re: Add bump memory context type and use it for tuplesorts

2024-03-11 Thread John Naylor
On Tue, Mar 5, 2024 at 9:42 AM David Rowley wrote: > performance against the recently optimised aset, generation and slab > contexts. The attached graph shows the time it took in seconds to > allocate 1GB of memory performing a context reset after 1MB. The > function I ran the test on is in the

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
On Wed, 21 Feb 2024 at 00:02, Matthias van de Meent wrote: > > On Tue, 20 Feb 2024 at 11:02, David Rowley wrote: > > On Fri, 26 Jan 2024 at 01:29, Matthias van de Meent > > wrote: > > > >> +allocSize = MAXALIGN(sizeof(BumpContext)) + Bump_BLOCKHDRSZ + > > > >> +if (minContextSize != 0)

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
On Tue, 20 Feb 2024 at 23:52, Matthias van de Meent wrote: > What I meant was that > > > (char *) block + Bump_BLOCKHDRSZ > vs > > ((char *) block) + Bump_BLOCKHDRSZ > > , when combined with my little experience with pointer addition and > precedence, and a lack of compiler at the ready at that

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread Matthias van de Meent
On Tue, 20 Feb 2024 at 11:02, David Rowley wrote: > On Fri, 26 Jan 2024 at 01:29, Matthias van de Meent > wrote: > > >> +allocSize = MAXALIGN(sizeof(BumpContext)) + Bump_BLOCKHDRSZ + > > >> +if (minContextSize != 0) > > >> +allocSize = Max(allocSize, minContextSize); > > >> +

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread Matthias van de Meent
On Tue, 20 Feb 2024 at 10:41, David Rowley wrote: > On Tue, 7 Nov 2023 at 07:55, Matthias van de Meent > wrote: > > > +++ b/src/backend/utils/mmgr/bump.c > > > +BumpBlockIsEmpty(BumpBlock *block) > > > +{ > > > +/* it's empty if the freeptr has not moved */ > > > +return (block->freeptr

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
Thanks for taking an interest in this. On Sat, 17 Feb 2024 at 11:46, Tomas Vondra wrote: > I wasn't paying much attention to these memcontext reworks in 2022, so > my instinct was simply to use one of those "UNUSED" IDs. But after > looking at the 80ef92675823 a bit more, are those IDs really

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
On Fri, 26 Jan 2024 at 01:29, Matthias van de Meent wrote: > >> +allocSize = MAXALIGN(sizeof(BumpContext)) + Bump_BLOCKHDRSZ + > >> +if (minContextSize != 0) > >> +allocSize = Max(allocSize, minContextSize); > >> +else > >> +allocSize = Max(allocSize, initBlockSize); >

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
On Wed, 26 Jul 2023 at 12:11, Nathan Bossart wrote: > I think it'd be okay to steal those bits. AFAICT it'd complicate the > macros in memutils_memorychunk.h a bit, but that doesn't seem like such a > terrible price to pay to allow us to keep avoiding the glibc bit patterns. I've not adjusted

Re: Add bump memory context type and use it for tuplesorts

2024-02-20 Thread David Rowley
Thanks for having a look at this. On Tue, 7 Nov 2023 at 07:55, Matthias van de Meent wrote: > I think it would make sense to split the "add a bump allocator" > changes from the "use the bump allocator in tuplesort" patches. I've done this and will post updated patches after replying to the

Re: Add bump memory context type and use it for tuplesorts

2024-02-17 Thread Andres Freund
Hi, On 2024-02-16 20:10:48 -0500, Tom Lane wrote: > Tomas Vondra writes: > > On 2/17/24 00:14, Tom Lane wrote: > >> The conclusion was that the specific invalid values didn't matter as > >> much on the other platforms as they do with glibc. But right now you > >> have a fifty-fifty chance that

Re: Add bump memory context type and use it for tuplesorts

2024-02-16 Thread Tom Lane
Tomas Vondra writes: > On 2/17/24 00:14, Tom Lane wrote: >> The conclusion was that the specific invalid values didn't matter as >> much on the other platforms as they do with glibc. But right now you >> have a fifty-fifty chance that a pointer to garbage will look valid. >> Do we want to

Re: Add bump memory context type and use it for tuplesorts

2024-02-16 Thread Tomas Vondra
On 2/17/24 00:14, Tom Lane wrote: > Tomas Vondra writes: >> Maybe I'm completely misunderstanding the implication of those limits, >> but doesn't this mean the claim that we can support 8 memory context >> types is not quite true, and the limit is 4, because the 4 IDs are >> already used for

Re: Add bump memory context type and use it for tuplesorts

2024-02-16 Thread Tom Lane
Tomas Vondra writes: > Maybe I'm completely misunderstanding the implication of those limits, > but doesn't this mean the claim that we can support 8 memory context > types is not quite true, and the limit is 4, because the 4 IDs are > already used for malloc stuff? Well, correct code would

Re: Add bump memory context type and use it for tuplesorts

2024-02-16 Thread Tomas Vondra
On 11/6/23 19:54, Matthias van de Meent wrote: > > ... > > 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

Re: Add bump memory context type and use it for tuplesorts

2024-02-16 Thread Tomas Vondra
Hi, I wanted to take a look at this patch but it seems to need a rebase, because of a seemingly trivial conflict in MemoryContextMethodID: --- src/include/utils/memutils_internal.h +++ src/include/utils/memutils_internal.h @@ -123,12 +140,13 @@ typedef enum MemoryContextMethodID {

Re: Add bump memory context type and use it for tuplesorts

2024-01-25 Thread Matthias van de Meent
On Mon, 6 Nov 2023 at 19:54, Matthias van de Meent wrote: > > 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

Re: Add bump memory context type and use it for tuplesorts

2023-11-06 Thread Matthias van de Meent
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

Re: Add bump memory context type and use it for tuplesorts

2023-07-25 Thread Nathan Bossart
On Tue, Jun 27, 2023 at 09:19:26PM +1200, David Rowley wrote: > Because of all of what is mentioned above about the current state of > tuplesort, there does not really seem to be much need to have chunk > headers in memory we allocate for tuples at all. Not having these > saves us a further 8

Re: Add bump memory context type and use it for tuplesorts

2023-07-10 Thread David Rowley
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