Author: turnstep
Date: Wed Jul 23 22:32:31 2008
New Revision: 11584

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/META.yml
   DBD-Pg/trunk/Makefile.PL
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/README
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/dbdimp.h
   DBD-Pg/trunk/lib/Bundle/DBD/Pg.pm

Log:
Do the Newz() and Safefree() calls only once per statement handle by 
moving the structs out to the statement handle, which saves having 
to continually allocate and free space for them with multiple executes 
against the same statement handle.
Bump version up to 2.8.8


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes        (original)
+++ DBD-Pg/trunk/Changes        Wed Jul 23 22:32:31 2008
@@ -1,5 +1,10 @@
 ('GSM' is Greg Sabino Mullane, [EMAIL PROTECTED])
 
+2.8.8
+
+       - Move PQexec structures to statement handle, to prevent 
+               excessive malloc and free within execute function. [GSM]
+
 2.8.7 Released July 24, 2008 (subversion r11582)
 
        - Modify test scripts to work better on FreeBSD boxes.

Modified: DBD-Pg/trunk/META.yml
==============================================================================
--- DBD-Pg/trunk/META.yml       (original)
+++ DBD-Pg/trunk/META.yml       Wed Jul 23 22:32:31 2008
@@ -1,6 +1,6 @@
 --- #YAML:1.0
 name                        : DBD-Pg
-version                     : 2.8.7
+version                     : 2.8.8
 abstract                    : DBI PostgreSQL interface
 author:              
   - Greg Sabino Mullane <[EMAIL PROTECTED]>
@@ -38,10 +38,10 @@
 provides:
   DBD::Pg:
     file                    : Pg.pm
-    version                 : 2.8.7
+    version                 : 2.8.8
   Bundle::DBD::Pg:
     file                    : lib/Bundle/DBD/Pg.pm
-    version                 : 2.8.7
+    version                 : 2.8.8
 
 keywords:
   - Postgres

Modified: DBD-Pg/trunk/Makefile.PL
==============================================================================
--- DBD-Pg/trunk/Makefile.PL    (original)
+++ DBD-Pg/trunk/Makefile.PL    Wed Jul 23 22:32:31 2008
@@ -7,7 +7,7 @@
 use 5.006001;
 
 ## No version.pm for this one, as the prereqs are not loaded yet.
-my $VERSION = '2.8.7';
+my $VERSION = '2.8.8';
 
 my $lib;
 BEGIN {

Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm  (original)
+++ DBD-Pg/trunk/Pg.pm  Wed Jul 23 22:32:31 2008
@@ -17,7 +17,7 @@
 {
        package DBD::Pg;
 
-       use version; our $VERSION = qv('2.8.7');
+       use version; our $VERSION = qv('2.8.8');
 
        use DBI ();
        use DynaLoader ();
@@ -1696,7 +1696,7 @@
 
 =head1 VERSION
 
-This documents version 2.8.7 of the DBD::Pg module
+This documents version 2.8.8 of the DBD::Pg module
 
 =head1 DESCRIPTION
 

Modified: DBD-Pg/trunk/README
==============================================================================
--- DBD-Pg/trunk/README (original)
+++ DBD-Pg/trunk/README Wed Jul 23 22:32:31 2008
@@ -6,7 +6,7 @@
 DESCRIPTION:
 ------------
 
-This is version 2.8.7 of DBD::Pg.  The web site for this interface, and 
+This is version 2.8.8 of DBD::Pg.  The web site for this interface, and 
 the latest version, can be found at:
 
        http://search.cpan.org/dist/DBD-Pg/

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c       (original)
+++ DBD-Pg/trunk/dbdimp.c       Wed Jul 23 22:32:31 2008
@@ -1365,6 +1365,10 @@
        imp_sth->type_info        = NULL;
        imp_sth->seg              = NULL;
        imp_sth->ph               = NULL;
+       imp_sth->PQvals           = NULL;
+       imp_sth->PQlens           = NULL;
+       imp_sth->PQfmts           = NULL;
+       imp_sth->PQoids           = NULL;
        imp_sth->prepared_by_us   = DBDPG_FALSE; /* Set to 1 when actually done 
preparing */
        imp_sth->onetime          = DBDPG_FALSE; /* Allow internal shortcut */
        imp_sth->direct           = DBDPG_FALSE;
@@ -1958,7 +1962,6 @@
        int          status = -1;
        seg_t *      currseg;
        bool         oldprepare = DBDPG_TRUE;
-       Oid *        paramTypes = NULL;
        ph_t *       currph;
 
        if (TSTART) TRC(DBILOGFP, "%sBegin pg_st_prepare_statement\n", THEADER);
@@ -2053,17 +2056,18 @@
                int params = 0;
                if (imp_sth->numbound!=0) {
                        params = imp_sth->numphs;
-                       Newz(0, paramTypes, imp_sth->numphs, Oid);
+                       if (NULL == imp_sth->PQoids) {
+                               Newz(0, imp_sth->PQoids, imp_sth->numphs, Oid);
+                       }
                        for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph) {
-                               paramTypes[x++] = (currph->defaultval) ? 0 : 
(Oid)currph->bind_type->type_id;
+                               imp_sth->PQoids[x++] = (currph->defaultval) ? 0 
: (Oid)currph->bind_type->type_id;
                        }
                }
                if (TSQL)
                        TRC(DBILOGFP, "PREPARE %s AS %s;\n\n", 
imp_sth->prepare_name, statement);
 
                TRACE_PQPREPARE;
-               result = PQprepare(imp_dbh->conn, imp_sth->prepare_name, 
statement, params, paramTypes);
-               Safefree(paramTypes);
+               result = PQprepare(imp_dbh->conn, imp_sth->prepare_name, 
statement, params, imp_sth->PQoids);
                if (result) {
                        TRACE_PQRESULTSTATUS;
                        status = PQresultStatus(result);
@@ -2732,10 +2736,6 @@
        ph_t *        currph;
        int           status = -1;
        STRLEN        execsize, x;
-       const char ** paramValues = NULL;
-       int *         paramLengths = NULL;
-       int *         paramFormats = NULL;
-       Oid *         paramTypes = NULL;
        seg_t *       currseg;
        char *        statement = NULL;
        int           num_fields;
@@ -2847,24 +2847,28 @@
        }
        else { /* We are using a server that can handle 
PQexecParams/PQexecPrepared */
                /* Put all values into an array to pass to PQexecPrepared */
-               Newz(0, paramValues, imp_sth->numphs, const char *); /* freed 
below */
+               if (NULL == imp_sth->PQvals) {
+                       Newz(0, imp_sth->PQvals, imp_sth->numphs, const char 
*); /* freed in dbd_st_destroy */
+               }
                for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph) {
-                       paramValues[x++] = currph->value;
+                       imp_sth->PQvals[x++] = currph->value;
                }
 
                /* Binary or regular? */
 
                if (imp_sth->has_binary) {
-                       Newz(0, paramLengths, imp_sth->numphs, int); /* freed 
below */
-                       Newz(0, paramFormats, imp_sth->numphs, int); /* freed 
below */
+                       if (NULL == imp_sth->PQlens) {
+                               Newz(0, imp_sth->PQlens, imp_sth->numphs, int); 
/* freed in dbd_st_destroy */
+                               Newz(0, imp_sth->PQfmts, imp_sth->numphs, int); 
/* freed below */
+                       }
                        for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,x++) {
                                if (PG_BYTEA==currph->bind_type->type_id) {
-                                       paramLengths[x] = (int)currph->valuelen;
-                                       paramFormats[x] = 1;
+                                       imp_sth->PQlens[x] = 
(int)currph->valuelen;
+                                       imp_sth->PQfmts[x] = 1;
                                }
                                else {
-                                       paramLengths[x] = 0;
-                                       paramFormats[x] = 0;
+                                       imp_sth->PQlens[x] = 0;
+                                       imp_sth->PQfmts[x] = 0;
                                }
                        }
                }
@@ -2908,9 +2912,6 @@
                                if (TRACE5) TRC(DBILOGFP, "%sRe-preparing 
statement\n", THEADER);
                        }
                        if (pg_st_prepare_statement(aTHX_ sth, imp_sth)!=0) {
-                               Safefree(paramValues);
-                               Safefree(paramLengths);
-                               Safefree(paramFormats);
                                if (TEND) TRC(DBILOGFP, "%sEnd dbd_st_execute 
(error)\n", THEADER);
                                return -2;
                        }
@@ -2924,10 +2925,10 @@
                        for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,x++) {
                                TRC(DBILOGFP, "%sPQexecPrepared item #%d\n", 
THEADER, x);
                                TRC(DBILOGFP, "%s-> Value: (%s)\n",
-                                       THEADER, (paramFormats && 
paramFormats[x]==1) ? "(binary, not shown)" 
-                                                                       : 
paramValues[x]);
-                               TRC(DBILOGFP, "%s-> Length: (%d)\n", THEADER, 
paramLengths ? paramLengths[x] : 0);
-                               TRC(DBILOGFP, "%s-> Format: (%d)\n", THEADER, 
paramFormats ? paramFormats[x] : 0);
+                                       THEADER, (imp_sth->PQfmts && 
imp_sth->PQfmts[x]==1) ? "(binary, not shown)" 
+                                                                       : 
imp_sth->PQvals[x]);
+                               TRC(DBILOGFP, "%s-> Length: (%d)\n", THEADER, 
imp_sth->PQlens ? imp_sth->PQlens[x] : 0);
+                               TRC(DBILOGFP, "%s-> Format: (%d)\n", THEADER, 
imp_sth->PQfmts ? imp_sth->PQfmts[x] : 0);
                        }
                }
                
@@ -2936,7 +2937,7 @@
                if (TSQL) {
                        TRC(DBILOGFP, "EXECUTE %s (\n", imp_sth->prepare_name);
                        for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,x++) {
-                               TRC(DBILOGFP, "$%d: %s\n", x+1, paramValues[x]);
+                               TRC(DBILOGFP, "$%d: %s\n", x+1, 
imp_sth->PQvals[x]);
                        }
                        TRC(DBILOGFP, ");\n\n");
                }
@@ -2944,12 +2945,12 @@
                if (imp_sth->async_flag & PG_ASYNC) {
                        TRACE_PQSENDQUERYPREPARED;
                        ret = PQsendQueryPrepared
-                               (imp_dbh->conn, imp_sth->prepare_name, 
imp_sth->numphs, paramValues, paramLengths, paramFormats, 0);
+                               (imp_dbh->conn, imp_sth->prepare_name, 
imp_sth->numphs, imp_sth->PQvals, imp_sth->PQlens, imp_sth->PQfmts, 0);
                }
                else {
                        TRACE_PQEXECPREPARED;
                        imp_sth->result = PQexecPrepared
-                               (imp_dbh->conn, imp_sth->prepare_name, 
imp_sth->numphs, paramValues, paramLengths, paramFormats, 0);
+                               (imp_dbh->conn, imp_sth->prepare_name, 
imp_sth->numphs, imp_sth->PQvals, imp_sth->PQlens, imp_sth->PQfmts, 0);
                }
        } /* end new-style prepare */
        else {
@@ -2992,26 +2993,28 @@
                        }
                        statement[execsize] = '\0';
                        
-                       /* Populate paramTypes */
-                       Newz(0, paramTypes, imp_sth->numphs, Oid);
+                       /* Populate PQoids */
+                       if (NULL == imp_sth->PQoids) {
+                               Newz(0, imp_sth->PQoids, imp_sth->numphs, Oid);
+                       }
                        for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph) {
-                               paramTypes[x++] = (currph->defaultval) ? 0 : 
(Oid)currph->bind_type->type_id;
+                               imp_sth->PQoids[x++] = (currph->defaultval) ? 0 
: (Oid)currph->bind_type->type_id;
                        }
                
                        if (TRACE7) {
                                for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,x++) {
                                        TRC(DBILOGFP, "%sPQexecParams item 
#%d\n", THEADER, x);
-                                       TRC(DBILOGFP, "%s-> Type: (%d)\n", 
THEADER, paramTypes[x]);
-                                       TRC(DBILOGFP, "%s-> Value: (%s)\n", 
THEADER, paramValues[x]);
-                                       TRC(DBILOGFP, "%s-> Length: (%d)\n", 
THEADER, paramLengths ? paramLengths[x] : 0);
-                                       TRC(DBILOGFP, "%s-> Format: (%d)\n", 
THEADER, paramFormats ? paramFormats[x] : 0);
+                                       TRC(DBILOGFP, "%s-> Type: (%d)\n", 
THEADER, imp_sth->PQoids[x]);
+                                       TRC(DBILOGFP, "%s-> Value: (%s)\n", 
THEADER, imp_sth->PQvals[x]);
+                                       TRC(DBILOGFP, "%s-> Length: (%d)\n", 
THEADER, imp_sth->PQlens ? imp_sth->PQlens[x] : 0);
+                                       TRC(DBILOGFP, "%s-> Format: (%d)\n", 
THEADER, imp_sth->PQfmts ? imp_sth->PQfmts[x] : 0);
                                }
                        }
 
                        if (TSQL) {
                                TRC(DBILOGFP, "EXECUTE %s (\n", statement);
                                for (x=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,x++) {
-                                       TRC(DBILOGFP, "$%d: %s\n", x+1, 
paramValues[x]);
+                                       TRC(DBILOGFP, "$%d: %s\n", x+1, 
imp_sth->PQvals[x]);
                                }
                                TRC(DBILOGFP, ");\n\n");
                        }
@@ -3020,14 +3023,13 @@
                        if (imp_sth->async_flag & PG_ASYNC) {
                                TRACE_PQSENDQUERYPARAMS;
                                ret = PQsendQueryParams
-                                       (imp_dbh->conn, statement, 
imp_sth->numphs, paramTypes, paramValues, paramLengths, paramFormats, 0);
+                                       (imp_dbh->conn, statement, 
imp_sth->numphs, imp_sth->PQoids, imp_sth->PQvals, imp_sth->PQlens, 
imp_sth->PQfmts, 0);
                        }
                        else {
                                TRACE_PQEXECPARAMS;
                                imp_sth->result = PQexecParams
-                                       (imp_dbh->conn, statement, 
imp_sth->numphs, paramTypes, paramValues, paramLengths, paramFormats, 0);
+                                       (imp_dbh->conn, statement, 
imp_sth->numphs, imp_sth->PQoids, imp_sth->PQvals, imp_sth->PQlens, 
imp_sth->PQfmts, 0);
                        }
-                       Safefree(paramTypes);
                }
                
                /* PQexec */
@@ -3074,10 +3076,6 @@
 
        /* Some form of PQexec/PQsendQuery has been run at this point */
 
-       Safefree(paramValues);
-       Safefree(paramLengths);
-       Safefree(paramFormats);                 
-
        /* If running asynchronously, we don't stick around for the result */
        if (imp_sth->async_flag & PG_ASYNC) {
                if (TRACEWARN) TRC(DBILOGFP, "%sEarly return for async query", 
THEADER);
@@ -3519,6 +3517,10 @@
        Safefree(imp_sth->prepare_name);
        Safefree(imp_sth->type_info);
        Safefree(imp_sth->firstword);
+       Safefree(imp_sth->PQvals);
+       Safefree(imp_sth->PQlens);
+       Safefree(imp_sth->PQfmts);
+       Safefree(imp_sth->PQoids);
 
        if (imp_sth->result) {
                TRACE_PQCLEAR;

Modified: DBD-Pg/trunk/dbdimp.h
==============================================================================
--- DBD-Pg/trunk/dbdimp.h       (original)
+++ DBD-Pg/trunk/dbdimp.h       Wed Jul 23 22:32:31 2008
@@ -70,7 +70,7 @@
 
 /* Define sth implementor data structure */
 struct imp_sth_st {
-       dbih_stc_t com;         /* MUST be first element in structure */
+       dbih_stc_t com;          /* MUST be first element in structure */
 
        int    server_prepare;   /* inherited from dbh. 3 states: 0=no 1=yes 
2=smart */
        int    placeholder_type; /* which style is being used 1=? 2=$1 3=:foo */
@@ -84,6 +84,10 @@
 
        STRLEN totalsize;        /* total string length of the statement (with 
no placeholders)*/
 
+       const char ** PQvals;    /* List of values to pass to PQ* */
+       int         * PQlens;    /* List of lengths to pass to PQ* */
+       int         * PQfmts;    /* List of formats to pass to PQ* */
+       Oid         * PQoids;    /* List of types to pass to PQ* */
        char   *prepare_name;    /* name of the prepared query; NULL if not 
prepared */
        char   *firstword;       /* first word of the statement */
 

Modified: DBD-Pg/trunk/lib/Bundle/DBD/Pg.pm
==============================================================================
--- DBD-Pg/trunk/lib/Bundle/DBD/Pg.pm   (original)
+++ DBD-Pg/trunk/lib/Bundle/DBD/Pg.pm   Wed Jul 23 22:32:31 2008
@@ -4,7 +4,7 @@
 use strict;
 use warnings;
 
-$VERSION = '2.8.7';
+$VERSION = '2.8.8';
 
 1;
 

Reply via email to