Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGXlz-0003Yo-0R for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 16:46:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UGXly-0001SU-7l for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 16:46:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGXlx-0001SP-JT for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 16:46:25 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGXlo-0006dE-LT for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 16:46:24 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 6BD4728115 for ; Fri, 15 Mar 2013 09:46:13 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 6909FB026E for ; Fri, 15 Mar 2013 09:46:13 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id C3DB2C15DC for ; Fri, 15 Mar 2013 09:46:11 -0700 (PDT) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3RG6Sd9XRwRN for ; Fri, 15 Mar 2013 09:46:11 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id A6D6AC153D for ; Fri, 15 Mar 2013 09:46:11 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA for ; Fri, 15 Mar 2013 09:46:10 -0700 (PDT) Message-ID: <51435052.5060304@vmware.com> Date: Fri, 15 Mar 2013 18:46:10 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: pgsql-odbc@postgresql.org Subject: Avoid O(n^2) behavior with large parameter arrays Content-Type: multipart/mixed; boundary="------------000701020406040800030303" X-Pg-Spam-Score: -9.3 (---------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------000701020406040800030303 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit While digging deeper into the array binding code, I noticed that a lot of time is spent chasing the end of the linked list of query result, to append a new result at the end. When executing a statement with array binding, SC_execute iterates through all the previous results, leading to O(n^2) behavior. With a large parameter array, a lot of CPU time is spent doing that. Attached patch fixes that by keeping track of the tail of the linked list, and the StatementClass that "owns" each QResultClass object (if any). By "owning", I mean that the QResultClass object is linked in the linked list of that StatementClass. That avoids having to traverse the list to check if a QResultClass is already in the chain. With this patch, I'm no longer seeing O(n^2) behavior, and it becomes feasible to use large parameter arrays with > 100000 elements. (as usual, this patch is also available in my github repository) - Heikki --------------000701020406040800030303 Content-Type: text/x-diff; name="0001-Eliminate-looping-through-chain-of-results-in-perfor.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Eliminate-looping-through-chain-of-results-in-perfor.pa"; filename*1="tch" From 7bfe31114c6ca02b3efa5f75f884423099ac370b Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 15 Mar 2013 15:26:01 +0200 Subject: [PATCH 1/1] Eliminate looping through chain of results in performance critical codepaths. This patch eliminates O(n^2) behavior on the number of chained results in SC_execute. This makes it feasible to deal with large parameter arrays, for example. The idea is to track the owner statement of each result object, so that when we need to check if a result object is already in the chain of results of a statement, we just need to look at the owner field, not loop through the whole chain. Also, keep track of the last result in the chain in StatementClass, so that we don't need to iterate through it when appending to the end. --- qresult.c | 2 ++ qresult.h | 1 + statement.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++------------ statement.h | 2 ++ 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/qresult.c b/qresult.c index 9ad0814..5bb17fc 100644 --- a/qresult.c +++ b/qresult.c @@ -163,6 +163,7 @@ QR_Constructor() rv->command = NULL; rv->notice = NULL; rv->conn = NULL; + rv->stmt = NULL; rv->next = NULL; rv->pstatus = 0; rv->count_backend_allocated = 0; @@ -1234,6 +1235,7 @@ inolog("id='%c' response_length=%d\n", id, response_length); CC_on_abort(conn, CONN_DEAD); return FALSE; } + self->next->stmt = self->stmt; QR_set_cache_size(self->next, self->cache_size); self = self->next; if (!QR_fetch_tuples(self, conn, NULL, LastMessageType)) diff --git a/qresult.h b/qresult.h index 3893bda..108f1c0 100644 --- a/qresult.h +++ b/qresult.h @@ -55,6 +55,7 @@ struct QResultClass_ ColumnInfoClass *fields; /* the Column information */ ConnectionClass *conn; /* the connection this result is using * (backend) */ + StatementClass *stmt; /* the statement this result belongs to. */ QResultClass *next; /* the following result class */ /* Stuff for declare/fetch tuples */ diff --git a/statement.c b/statement.c index 697d142..be21548 100644 --- a/statement.c +++ b/statement.c @@ -383,6 +383,7 @@ SC_Constructor(ConnectionClass *conn) rv->hdbc = conn; rv->phstmt = NULL; rv->result = NULL; + rv->lastres = NULL; rv->curres = NULL; rv->catalog_result = FALSE; rv->prepare = NON_PREPARE_STATEMENT; @@ -525,7 +526,7 @@ SC_Destructor(StatementClass *self) void SC_init_Result(StatementClass *self) { - self->result = self->curres = NULL; + self->result = self->lastres = self->curres = NULL; self->curr_param_result = 0; mylog("SC_init_Result(%x)", self); } @@ -537,9 +538,22 @@ SC_set_Result(StatementClass *self, QResultClass *res) { mylog("SC_set_Result(%x, %x)", self, res); QR_Destructor(self->result); - self->result = self->curres = res; + self->result = self->lastres = self->curres = res; if (NULL != res) + { + /* + * Set the owner of 'res', and all results chained + * to it. They all belong to this statement now. + */ + QResultClass *t; + for (t = res; t != NULL; t = t->next) + { + t->stmt = self; + /* also update lastres while we're at it */ + self->lastres = t; + } self->curr_param_result = 1; + } } } @@ -1573,6 +1587,28 @@ PGAPI_StmtError( SQLHSTMT hstmt, pcbErrorMsg, flag); } +/* + * Get the last result in the results chain. + */ +QResultClass * +SC_get_Lastres(StatementClass *self) +{ + QResultClass *res; + + res = self->lastres; + if (res == NULL) + res = NULL; + else + { + while (res->next != NULL) + res = res->next; + } + + /* update the cached value while we're at it */ + self->lastres = res; + return res; +} + time_t SC_get_time(StatementClass *stmt) { @@ -1978,7 +2014,7 @@ SC_execute(StatementClass *self) SC_set_error(self, STMT_EXEC_ERROR, "Execute request error", func); goto cleanup; } - for (res = SC_get_Result(self); NULL != res && NULL != res->next; res = res->next) ; + res = SC_get_Lastres(self); inolog("get_Result=%p %p %d\n", res, SC_get_Result(self), self->curr_param_result); if (!(res = SendSyncAndReceive(self, self->curr_param_result ? res : NULL, "bind_and_execute"))) { @@ -2193,20 +2229,28 @@ inolog("res->next=%p\n", tres); CC_abort(conn); #endif /* _LEGACY_MODE_ */ } - if (!SC_get_Result(self)) - SC_set_Result(self, res); - else - { - QResultClass *last; - for (last = SC_get_Result(self); NULL != last->next; last = last->next) + /* + * Add 'res' to the end of the result chain, if it's not in the chain + * already. + */ + if (res->stmt != self) + { + /* the result should not belong to any other statement */ + /* assert (res->stmt == NULL) */ + QResultClass *last = SC_get_Lastres(self); + if (last == NULL) + SC_set_Result(self, res); + else { - if (last == res) - break; - } - if (last != res) + QResultClass *t; + for (t = res; t != NULL; t = t->next) + t->stmt = self; + last->next = res; - self->curr_param_result = 1; + self->curr_param_result = 1; + res->stmt = self; + } } ipdopts = SC_get_IPDF(self); diff --git a/statement.h b/statement.h index 21785ae..110cf22 100644 --- a/statement.h +++ b/statement.h @@ -182,6 +182,7 @@ struct StatementClass_ * statement belongs to */ QResultClass *result; /* result of the current statement */ QResultClass *curres; /* the current result in the chain */ + QResultClass *lastres; /* last result in the result->next chain */ HSTMT FAR *phstmt; StatementOptions options; StatementOptions options_orig; @@ -290,6 +291,7 @@ void SC_set_Result(StatementClass *self, QResultClass *res); #define SC_get_Result(a) (a->result) #define SC_set_Curres(a, b) (a->curres = b) #define SC_get_Curres(a) (a->curres) +extern QResultClass *SC_get_Lastres(StatementClass *self); #define SC_get_ARD(a) (a->ard) #define SC_get_APD(a) (a->apd) #define SC_get_IRD(a) (a->ird) -- 1.7.10.4 --------------000701020406040800030303 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------000701020406040800030303--