Skip to content

Commit e15aae8

Browse files
committed
Avoid statically allocating statement cache in ecpglib/prepare.c.
This removes a megabyte of storage that isn't used at all in ecpglib's default operating mode --- you have to enable auto-prepare to get any use out of it. Seems well worth the trouble to allocate on demand. Discussion: https://postgr.es/m/20181015200754.7y7zfuzsoux2c4ya@alap3.anarazel.de
1 parent 92dff34 commit e15aae8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/interfaces/ecpg/ecpglib/prepare.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#define STMTID_SIZE 32
1515

16+
#define N_STMTCACHE_ENTRIES 16384
17+
1618
typedef struct
1719
{
1820
int lineno;
@@ -25,7 +27,7 @@ typedef struct
2527
static int nextStmtID = 1;
2628
static const int stmtCacheNBuckets = 2039; /* # buckets - a prime # */
2729
static const int stmtCacheEntPerBucket = 8; /* # entries/bucket */
28-
static stmtCacheEntry stmtCacheEntries[16384] = {{0, {0}, 0, 0, 0}};
30+
static stmtCacheEntry *stmtCacheEntries = NULL;
2931

3032
static bool deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con,
3133
struct prepared_statement *prev, struct prepared_statement *this);
@@ -362,6 +364,10 @@ SearchStmtCache(const char *ecpgQuery)
362364
int entNo,
363365
entIx;
364366

367+
/* quick failure if cache not set up */
368+
if (stmtCacheEntries == NULL)
369+
return 0;
370+
365371
/* hash the statement */
366372
entNo = HashStmt(ecpgQuery);
367373

@@ -397,6 +403,10 @@ ecpg_freeStmtCacheEntry(int lineno, int compat,
397403
struct prepared_statement *this,
398404
*prev;
399405

406+
/* fail if cache isn't set up */
407+
if (stmtCacheEntries == NULL)
408+
return -1;
409+
400410
entry = &stmtCacheEntries[entNo];
401411
if (!entry->stmtID[0]) /* return if the entry isn't in use */
402412
return 0;
@@ -437,6 +447,15 @@ AddStmtToCache(int lineno, /* line # of statement */
437447
entNo;
438448
stmtCacheEntry *entry;
439449

450+
/* allocate and zero cache array if we haven't already */
451+
if (stmtCacheEntries == NULL)
452+
{
453+
stmtCacheEntries = (stmtCacheEntry *)
454+
ecpg_alloc(sizeof(stmtCacheEntry) * N_STMTCACHE_ENTRIES, lineno);
455+
if (stmtCacheEntries == NULL)
456+
return -1;
457+
}
458+
440459
/* hash the statement */
441460
initEntNo = HashStmt(ecpgQuery);
442461

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy