8000 Minor additional improvements for ecpglib/prepare.c. · postgrespro/postgres@d8cc161 · GitHub
[go: up one dir, main page]

Skip to content
  • Commit d8cc161

    Browse files
    committed
    Minor additional improvements for ecpglib/prepare.c.
    Avoid allocating never-used entries in stmtCacheEntries[], other than the intentionally-unused zero'th entry. Tie the array size directly to the bucket count and size, rather than having undocumented dependencies between three magic constants. Fix the hash calculation to be p 8000 latform-independent --- notably, it was sensitive to the signed'ness of "char" before, not to mention having an unnecessary hard-wired dependency on the existence and size of type "long long". (The lack of complaints says it's been a long time since anybody tried to build PG on a compiler without "long long", and certainly with the requirement for C99 this isn't a live bug anymore. But it's still not per project coding style.) Fix ecpg_auto_prepare's new-cache-entry path so that it increments the exec count for the new cache entry not the dummy zero'th entry. The last of those is an actual bug, though one of little consequence; the rest is mostly future-proofing and neatnik-ism. Doesn't seem necessary to back-patch.
    1 parent e7eb07f commit d8cc161

    File tree

    2 files changed

    +27
    -16
    lines changed

    2 files changed

    +27
    -16
    lines changed

    src/interfaces/ecpg/ecpglib/prepare.c

    Lines changed: 22 additions & 11 deletions
    Original file line numberDiff line numberDiff line change
    @@ -13,7 +13,17 @@
    1313

    1414
    #define STMTID_SIZE 32
    1515

    16-
    #define N_STMTCACHE_ENTRIES 16384
    16+
    /*
    17+
    * The statement cache contains stmtCacheNBuckets hash buckets, each
    18+
    * having stmtCacheEntPerBucket entries, which we recycle as needed,
    19+
    * giving up the least-executed entry in the bucket.
    20+
    * stmtCacheEntries[0] is never used, so that zero can be a "not found"
    21+
    * indicator.
    22+
    */
    23+
    #define stmtCacheNBuckets 2039 /* should be a prime number */
    24+
    #define stmtCacheEntPerBucket 8
    25+
    26+
    #define stmtCacheArraySize (stmtCacheNBuckets * stmtCacheEntPerBucket + 1)
    1727

    1828
    typedef struct
    1929
    {
    @@ -25,8 +35,6 @@ typedef struct
    2535
    } stmtCacheEntry;
    2636

    2737
    static int nextStmtID = 1;
    28-
    static const int stmtCacheNBuckets = 2039; /* # buckets - a prime # */
    29-
    static const int stmtCacheEntPerBucket = 8; /* # entries/bucket */
    3038
    static stmtCacheEntry *stmtCacheEntries = NULL;
    3139

    3240
    static bool deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con,
    @@ -330,7 +338,7 @@ HashStmt(const char *ecpgQuery)
    330338
    bucketNo,
    331339
    hashLeng,
    332340
    stmtLeng;
    333-
    long long hashVal,
    341+
    uint64 hashVal,
    334342
    rotVal;
    335343

    336344
    stmtLeng = strlen(ecpgQuery);
    @@ -341,16 +349,17 @@ HashStmt(const char *ecpgQuery)
    341349
    hashVal = 0;
    342350
    for (stmtIx = 0; stmtIx < hashLeng; ++stmtIx)
    343351
    {
    344-
    hashVal = hashVal + (int) ecpgQuery[stmtIx];
    352+
    hashVal = hashVal + (unsigned char) ecpgQuery[stmtIx];
    353+
    /* rotate 32-bit hash value left 13 bits */
    345354
    hashVal = hashVal << 13;
    346-
    rotVal = (hashVal & 0x1fff00000000LL) >> 32;
    347-
    hashVal = (hashVal & 0xffffffffLL) | rotVal;
    355+
    rotVal = (hashVal & UINT64CONST(0x1fff00000000)) >> 32;
    356+
    hashVal = (hashVal & UINT64CONST(0xffffffff)) | rotVal;
    348357
    }
    349358

    350359
    bucketNo = hashVal % stmtCacheNBuckets;
    351-
    bucketNo += 1; /* don't use bucket # 0 */
    352360

    353-
    return (bucketNo * stmtCacheEntPerBucket);
    361+
    /* Add 1 so that array entry 0 is never used */
    362+
    return bucketNo * stmtCacheEntPerBucket + 1;
    354363
    }
    355364

    356365
    /*
    @@ -451,7 +460,7 @@ AddStmtToCache(int lineno, /* line # of statement */
    451460
    if (stmtCacheEntries == NULL)
    452461
    {
    453462
    stmtCacheEntries = (stmtCacheEntry *)
    454-
    ecpg_alloc(sizeof(stmtCacheEntry) * N_STMTCACHE_ENTRIES, lineno);
    463+
    ecpg_alloc(sizeof(stmtCacheEntry) * stmtCacheArraySize, lineno);
    455464
    if (stmtCacheEntries == NULL)
    456465
    return -1;
    457466
    }
    @@ -534,7 +543,9 @@ ecpg_auto_prepare(int lineno, const char *connection_name, const int compat, cha
    534543

    535544
    if (!ECPGprepare(lineno, connection_name, 0, stmtID, query))
    536545
    return false;
    537-
    if (AddStmtToCache(lineno, stmtID, connection_name, compat, query) < 0)
    546+
    547+
    entNo = AddStmtToCache(lineno, stmtID, connection_name, compat, query);
    548+
    if (entNo < 0)
    538549
    return false;
    539550

    540551
    *name = ecpg_strdup(stmtID, lineno);

    src/interfaces/ecpg/test/expected/preproc-autoprep.stderr

    Lines changed: 5 additions & 5 deletions
    Original file line numberDiff line numberDiff line change
    @@ -30,7 +30,7 @@
    3030
    [NO_PID]: sqlca: code: 0, state: 00000
    3131
    [NO_PID]: ecpg_process_output on line 24: OK: INSERT 0 1
    3232
    [NO_PID]: sqlca: code: 0, state: 00000
    33-
    [NO_PID]: ecpg_auto_prepare on line 26: statement found in cache; entry 1640
    33+
    [NO_PID]: ecpg_auto_prepare on line 26: statement found in cache; entry 1633
    3434
    [NO_PID]: sqlca: code: 0, state: 00000
    3535
    [NO_PID]: ecpg_execute on line 26: query: insert into T values ( 1 , $1 ); with 1 parameter(s) on connection ecpg1_regression
    3636
    [NO_PID]: sqlca: code: 0, state: 00000
    @@ -168,7 +168,7 @@
    168168
    [NO_PID]: sqlca: code: 0, state: 00000
    169169
    [NO_PID]: ecpg_process_output on line 21: OK: CREATE TABLE
    170170
    [NO_PID]: sqlca: code: 0, state: 00000
    171-
    [NO_PID]: ecpg_auto_prepare on line 23: statement found in cache; entry 15328
    171+
    [NO_PID]: ecpg_auto_prepare on line 23: statement found in cache; entry 15321
    172172
    [NO_PID]: sqlca: code: 0, state: 00000
    173173
    [NO_PID]: prepare_common on line 23: name ecpg1; query: "insert into T values ( 1 , null )"
    174174
    [NO_PID]: sqlca: code: 0, state: 00000
    @@ -178,7 +178,7 @@
    178178
    [NO_PID]: sqlca: code: 0, state: 00000
    179179
    [NO_PID]: ecpg_process_output on line 23: OK: INSERT 0 1
    180180
    [NO_PID]: sqlca: code: 0, state: 00000
    181-
    [NO_PID]: ecpg_auto_prepare on line 24: statement found in cache; entry 1640
    181+
    [NO_PID]: ecpg_auto_prepare on line 24: statement found in cache; entry 1633
    182182
    [NO_PID]: sqlca: code: 0, state: 00000
    183183
    [NO_PID]: prepare_common on line 24: name ecpg2; query: "insert into T values ( 1 , $1 )"
    184184
    [NO_PID]: sqlca: code: 0, state: 00000
    @@ -190,7 +190,7 @@
    190190
    [NO_PID]: sqlca: code: 0, state: 00000
    191191
    [NO_PID]: ecpg_process_output on line 24: OK: INSERT 0 1
    192192
    [NO_PID]: sqlca: code: 0, state: 00000
    193-
    [NO_PID]: ecpg_auto_prepare on line 26: statement found in cache; entry 1640
    193+
    [NO_PID]: ecpg_auto_prepare on line 26: statement found in cache; entry 1633
    194194
    [NO_PID]: sqlca: code: 0, state: 00000
    195195
    [NO_PID]: ecpg_execute on line 26: query: insert into T values ( 1 , $1 ); with 1 parameter(s) on connection ecpg1_regression
    196196
    [NO_PID]: sqlca: code: 0, state: 00000
    @@ -208,7 +208,7 @@
    208208
    [NO_PID]: sqlca: code: 0, state: 00000
    209209
    [NO_PID]: ecpg_process_output on line 28: OK: INSERT 0 1
    210210
    [NO_PID]: sqlca: code: 0, state: 00000
    211-
    [NO_PID]: ecpg_auto_prepare on line 30: statement found in cache; entry 13056
    211+
    [NO_PID]: ecpg_auto_prepare on line 30: statement found in cache; entry 13049
    212212
    [NO_PID]: sqlca: code: 0, state: 00000
    213213
    [NO_PID]: prepare_common on line 30: name ecpg3; query: "select Item2 from T order by Item2 nulls last"
    214214
    [NO_PID]: sqlca: code: 0, state: 00000

    0 commit comments

    Comments
     (0)
    0