gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet] 02/02: -avoid grow-by-one in statement preparations


From: gnunet
Subject: [gnunet] 02/02: -avoid grow-by-one in statement preparations
Date: Wed, 12 Oct 2022 14:48:27 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository gnunet.

commit 0efa2d4d007cc24fcf087d8f61e2b879a89cf0a5
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Wed Oct 12 14:46:44 2022 +0200

    -avoid grow-by-one in statement preparations
---
 src/pq/pq.h         | 10 ++++++++++
 src/pq/pq_prepare.c | 30 +++++++++++++++++-------------
 src/pq/test_pq.c    |  6 ++----
 3 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/pq/pq.h b/src/pq/pq.h
index 9f7e45f62..85cfcc070 100644
--- a/src/pq/pq.h
+++ b/src/pq/pq.h
@@ -49,6 +49,16 @@ struct GNUNET_PQ_Context
    */
   struct GNUNET_PQ_PreparedStatement *ps;
 
+  /**
+   * Length of the @e ps array.
+   */
+  unsigned int ps_len;
+
+  /**
+   * Last used offset in the @e ps array.
+   */
+  unsigned int ps_off;
+
   /**
    * Configuration to use to connect to the DB.
    */
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c
index e5970144d..b4292dea3 100644
--- a/src/pq/pq_prepare.c
+++ b/src/pq/pq_prepare.c
@@ -91,26 +91,30 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
   if (db->ps != ps)
   {
     /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
-    unsigned int olen = 0; /* length of existing 'db->ps' array */
     unsigned int nlen = 0; /* length of 'ps' array */
+    unsigned int xlen;
     struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
 
-    if (NULL != db->ps)
-      while (NULL != db->ps[olen].name)
-        olen++;
     while (NULL != ps[nlen].name)
       nlen++;
-    rps = GNUNET_new_array (olen + nlen + 1,
-                            struct GNUNET_PQ_PreparedStatement);
-    if (NULL != db->ps)
-      memcpy (rps,
-              db->ps,
-              olen * sizeof (struct GNUNET_PQ_PreparedStatement));
-    memcpy (&rps[olen],
+    xlen = nlen + db->ps_off;
+    if (xlen > db->ps_len)
+    {
+      xlen = 2 * xlen + 1;
+      rps = GNUNET_new_array (xlen,
+                              struct GNUNET_PQ_PreparedStatement);
+      if (NULL != db->ps)
+        memcpy (rps,
+                db->ps,
+                db->ps_off * sizeof (struct GNUNET_PQ_PreparedStatement));
+      GNUNET_free (db->ps);
+      db->ps_len = xlen;
+      db->ps = rps;
+    }
+    memcpy (&db->ps[db->ps_off],
             ps,
             nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
-    GNUNET_free (db->ps);
-    db->ps = rps;
+    db->ps_off += nlen;
   }
 
   return GNUNET_PQ_prepare_once (db,
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 90b5c6489..ff453b210 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -70,8 +70,7 @@ postgres_prepare (struct GNUNET_PQ_Context *db)
                             ",unn"
                             ") VALUES "
                             "($1, $2, $3, $4, $5, $6,"
-                            "$7, $8, $9, $10);",
-                            10),
+                            "$7, $8, $9, $10);"),
     GNUNET_PQ_make_prepare ("test_select",
                             "SELECT"
                             " pub"
@@ -86,8 +85,7 @@ postgres_prepare (struct GNUNET_PQ_Context *db)
                             ",unn"
                             " FROM test_pq"
                             " ORDER BY abs_time DESC "
-                            " LIMIT 1;",
-                            0),
+                            " LIMIT 1;"),
     GNUNET_PQ_PREPARED_STATEMENT_END
   };
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]