Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsJIT-0003R1-HJ for pgsql-bugs@arkaria.postgresql.org; Tue, 30 Jul 2019 03:59:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsJIR-0003dL-MZ for pgsql-bugs@arkaria.postgresql.org; Tue, 30 Jul 2019 03:59:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsJIR-0003dC-CZ for pgsql-bugs@lists.postgresql.org; Tue, 30 Jul 2019 03:59:31 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsJIO-0008AM-CX for pgsql-bugs@lists.postgresql.org; Tue, 30 Jul 2019 03:59:30 +0000 Received: by mail-pg1-x541.google.com with SMTP id r22so1710948pgk.10 for ; Mon, 29 Jul 2019 20:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=KQtUlLNf/+nTsFC/95+i3v6oQz0jhITBh0IiAO+JQak=; b=nagCc8kZsplDwEndhPLC1GOajP70m+AB5SqKmbeLtvgnqqrt0xoqBk7FGxXOCPW9OV MibrZ0FcCuiQL447mZjMgrTgm5Co4GHMOVNTjG5Z3ViEONfWgTaHN5RTO6a7KySVMpjU kCVOF5kDQo2olpYdPAuWjS646iCUfR3x12kk0H+XE6HlXPlaWEn+sm5x/JAuaagRUVvD 8aOFJps35kgIc+yGcuUHRGCJNukITbKAEfhKsM2mMHcQwVqiVvOcHjdnMOR8hNQuc3Mf ab5TlE/rVRvTDIr0MPG1c9O9TD0P5hbgSiCjTlS6A8QMrbYLJZUoqI9pRcN5DvPYh1S1 +SXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=KQtUlLNf/+nTsFC/95+i3v6oQz0jhITBh0IiAO+JQak=; b=DJTQ7+ukfeiJnaH1c8qk2Ug5WKGHseNKHCczG/31gOuQ6IyVUidoDld5sbQTHIZjYZ PCV6Mo9K0kKZyQebtp8CagPWVB69iqcorFbwKfhXiu8OIJo//NMnyc1tAGqruNNL7HLx x2jCx/+u9v+yiJRCMhjObdAwrcE5zXBpaQEpUKwOopZKx+8nbFewDLZarxfIFGqeU5sA DxGiuXXLSZKnFBK+ZqEsAJV6CcziFfhqIrJipCwnPxS6JYddNjFIBzZkIL4b+oR896ex nhGvKszeauwZ78HjXqw/XPTX2bmkazUFJwNeEHOU9CLNzEiBMNYxrG7TSLt6QTOHGWL2 LoXw== X-Gm-Message-State: APjAAAVCpEXt3dJueCtoEHAPv+IF5KYht0euZhJ0EcG7VjmgAysyfw/M jyo1P85ZR4vfEfjeBqQ0ii8= X-Google-Smtp-Source: APXvYqw0kT2fPwWxkDR83Sh4EGAtWMihvbebsqqf4ZBTpB+oX5Gt7vQ2DNOM5bNYaMVDVHqyX5lJsA== X-Received: by 2002:a63:101b:: with SMTP id f27mr103195729pgl.291.1564459164619; Mon, 29 Jul 2019 20:59:24 -0700 (PDT) Received: from localhost (w204177.dynamic.ppp.asahi-net.or.jp. [121.1.204.177]) by smtp.gmail.com with ESMTPSA id a3sm65517029pfo.49.2019.07.29.20.59.21 (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 29 Jul 2019 20:59:24 -0700 (PDT) Date: Tue, 30 Jul 2019 12:59:11 +0900 (Tokyo Standard Time) Message-Id: <20190730.125911.127501233.horikyota.ntt@gmail.com> To: thomas.munro@gmail.com Cc: dmigowski@ikoffice.de, tgl@sss.pgh.pa.us, andres@anarazel.de, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15923: Prepared statements take way too much memory. From: Kyotaro Horiguchi In-Reply-To: References: <41ED3F5450C90F4D8381BC4D8DF6BBDCF02E19B4@EXCHANGESERVER.ikoffice.de> User-Agent: Mew version 6.5 on Emacs 22.2 / Mule 5.0 =?iso-2022-jp?B?KBskQjpnGyhCKQ==?= / Meadow-3.01-dev (TSUBO-SUMIRE) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk At Mon, 29 Jul 2019 18:11:04 +1200, Thomas Munro wrote in > On Sun, Jul 28, 2019 at 11:14 PM Daniel Migowski wrote: > > * Why are MemoryContextAllocZeroAligned and MemoryContextAllocZero the same function? > > It's not really relevant to the discussion about memory usage, but since > you asked about MemoryContextAllocZeroAligned: > > I noticed that stuff a while back when another project I follow was > tweaking related stuff. The idea behind those functions is that we > have our own specialised memset()-like macros. > MemoryContextAllocZeroAligned() uses MemSetLoop(), while > MemoryContextAllocZero() uses MemSetAligned(). Those date back nearly > two decades and were apparently based on experiments done at the time. > The comments talk about avoiding function call overhead and > specialising for constant lengths, so I suppose we assumed that > compilers at the time didn't automatically know how to inline plain > old memset() calls. > > These days, you'd think you could add "assume aligned" attributes to > all memory-allocation-like functions and then change a few key things > to be static inline so that the constants can be seen in the right > places, and get all of the same benefits (and probably more) for free > from the compiler's alignment analysis and inlining powers. > > As for what benefits might actually be available, for constants it's > clear but for alignment, I'm doubtful. When Clang 8 on amd64 inlines > a memset() with a known aligned destination (for various alignment > sizes) and constant size, it generates a bunch of movups instructions, > or with -march set to a modern arch, maybe vmovups, and some > variations depending on size. Note 'u' (unaligned) instructions, not > 'a' (aligned): that is, it doesn't even care that I said it the > destination was aligned! I found claims from a couple of Intel > sources that this sort of thing stopped making any difference to > performance in the Nehalem microarchitecture (2008). It still matters > whether the data actually is aligned, but not whether you use the > instructions that tolerate misalignment, so there is apparently no > point in generating different code, and therefore, as far as memset() > goes, apparently no gain from annotating allocation functions as > returning aligned pointers. As for other architectures or compilers, > I don't know. > > For curiosity, here's an experimental patch that gets rid of the > MemSetXXX() stuff, adds some (useless?) annotations about alignment > and makes palloc0() and MemoryContextAllocZero() inline so that they > can benefit from inlining with a visible constant size in eg > newNode(). It didn't seem to do anything very interesting apart from > remove a few hundred lines of code, so I didn't get around to digging > further or sharing it earlier. Or maybe I was just doing it wrong. > (In this patch it's using sizeof(long) which isn't enough to be > considered aligned for the wider move instructions, but I tried > various sizes when trying to trigger different codegen, without > success). FWIW, I saw gcc/x64 -O2 emit "rep stosq" for memset() having constant length and "not-aligned" pointer are passed. Of course aligned(8) didn't make difference at all. Emitted a seris of movex for smaller chunks. And always emitted "call memset" for non-constant length. That makes me think that the "aligned" attribute works some different way. Anyway I haven't find any further explantion about the hint than "compiler considers alignment on the varialbe/type/function"... regards. -- Kyotaro Horiguchi NTT Open Source Software Center