]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Save a few bytes per CatCTup.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 15 Mar 2026 22:05:38 +0000 (18:05 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 15 Mar 2026 22:05:38 +0000 (18:05 -0400)
CatalogCacheCreateEntry() computed the space needed for a CatCTup
as sizeof(CatCTup) + MAXIMUM_ALIGNOF.  That's not our usual style,
and it wastes memory by allocating more padding than necessary.
On 64-bit machines sizeof(CatCTup) would be maxaligned already
since it contains pointer fields, therefore this code is wasting
8 bytes compared to the more usual MAXALIGN(sizeof(CatCTup)).

While at it, we don't really need to do MemoryContextSwitchTo()
when we're only allocating one block.

Author: ChangAo Chen <cca5507@qq.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/tencent_A42E0544C6184FE940CD8E3B14A3F0A39605@qq.com

src/backend/utils/cache/catcache.c

index 519089322f407765ece5c4836ec667b3bef9f68b..87ed55064604a0b2fa579a83399d4914ad9b833e 100644 (file)
@@ -2221,20 +2221,18 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, Datum *arguments,
                        dtp = ntp;
 
                /* Allocate memory for CatCTup and the cached tuple in one go */
-               oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
-
-               ct = (CatCTup *) palloc(sizeof(CatCTup) +
-                                                               MAXIMUM_ALIGNOF + dtp->t_len);
+               ct = (CatCTup *)
+                       MemoryContextAlloc(CacheMemoryContext,
+                                                          MAXALIGN(sizeof(CatCTup)) + dtp->t_len);
                ct->tuple.t_len = dtp->t_len;
                ct->tuple.t_self = dtp->t_self;
                ct->tuple.t_tableOid = dtp->t_tableOid;
                ct->tuple.t_data = (HeapTupleHeader)
-                       MAXALIGN(((char *) ct) + sizeof(CatCTup));
+                       (((char *) ct) + MAXALIGN(sizeof(CatCTup)));
                /* copy tuple contents */
                memcpy((char *) ct->tuple.t_data,
                           (const char *) dtp->t_data,
                           dtp->t_len);
-               MemoryContextSwitchTo(oldcxt);
 
                if (dtp != ntp)
                        heap_freetuple(dtp);