Re: JDBC patch reduces memory consumption

Поиск
Список
Период
Сортировка
От Barry Lind
Тема Re: JDBC patch reduces memory consumption
Дата
Msg-id 3DAD95A5.2010408@xythos.com
обсуждение исходный текст
Ответ на JDBC patch reduces memory consumption  (Denis Perchine <dyp@perchine.com>)
Список pgsql-patches
Denis,

Please provide a patch against current CVS.  Some of the code below is
probably no longer needed given other changes that have been made in
current sources.

thanks,
--Barry



Denis Perchine wrote:
> Hello,
>
> This patch reduces JDBC memory usage for setString(),
> setBytes(), executeUpdate()
>
> --
> Denis
>
>
>
>
> ------------------------------------------------------------------------
>
> diff -Nur postgresql-7.2-orig/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java
postgresql-7.2/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java
> --- postgresql-7.2-orig/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java    Tue Jan 15 01:37:33 2002
> +++ postgresql-7.2/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java    Wed Oct 16 19:44:35 2002
> @@ -118,20 +118,34 @@
>       * This is identical to toString() except it throws an exception if a
>       * parameter is unused.
>       */
> -    private synchronized String compileQuery()
> -    throws SQLException
> +    private synchronized String compileQuery() throws SQLException
>      {
> -        sbuf.setLength(0);
> -        int i;
> -
> -        for (i = 0 ; i < inStrings.length ; ++i)
> -        {
> -            if (inStrings[i] == null)
> -                throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
> -            sbuf.append (templateStrings[i]).append (inStrings[i]);
> -        }
> -        sbuf.append(templateStrings[inStrings.length]);
> -        return sbuf.toString();
> +                synchronized (sbuf) {
> +                        try {
> +                          sbuf.setLength(0);
> +                        int i;
> +
> +                                // calculate future buffer size
> +                                int size = templateStrings[inStrings.length].length();
> +                                for (i = 0 ; i < inStrings.length ; ++i)
> +                                  if (inStrings[i] == null)
> +                                    throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
> +                                  else
> +                                    size += templateStrings[i].length() + inStrings[i].length();
> +                                sbuf.ensureCapacity(size);
> +
> +                        for (i = 0 ; i < inStrings.length ; ++i)
> +                        {
> +                            if (inStrings[i] == null)
> +                                throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
> +                            sbuf.append (templateStrings[i]).append (inStrings[i]);
> +                        }
> +                        sbuf.append(templateStrings[inStrings.length]);
> +                        return sbuf.toString();
> +                        } finally {
> +                          sbuf.setLength(0); // free some memory used by buffer
> +                        }
> +               }
>      }
>
>      /*
> @@ -276,18 +290,31 @@
>              synchronized (sbuf)
>              {
>                  sbuf.setLength(0);
> -                int i;
> +
> +                                int i;
> +                                char c;
> +                                if (x.length() > 10000) {
> +                                  // calculate buffer size
> +                                  int size = 2 + x.length();
> +                                  for (i = 0 ; i < x.length() ; ++i) {
> +                                    c = x.charAt(i);
> +                                    if (c == '\\' || c == '\'')
> +                                      size ++;
> +                                  }
> +                                  sbuf.ensureCapacity(size);
> +                                }
>
>                  sbuf.append('\'');
>                  for (i = 0 ; i < x.length() ; ++i)
>                  {
> -                    char c = x.charAt(i);
> +                    c = x.charAt(i);
>                      if (c == '\\' || c == '\'')
>                          sbuf.append((char)'\\');
>                      sbuf.append(c);
>                  }
>                  sbuf.append('\'');
>                  set(parameterIndex, sbuf.toString());
> +                                sbuf.setLength(0); // free some memory used by buffer
>              }
>          }
>      }
> diff -Nur postgresql-7.2-orig/src/interfaces/jdbc/org/postgresql/util/PGbytea.java
postgresql-7.2/src/interfaces/jdbc/org/postgresql/util/PGbytea.java
> --- postgresql-7.2-orig/src/interfaces/jdbc/org/postgresql/util/PGbytea.java    Sat Jan  5 16:26:23 2002
> +++ postgresql-7.2/src/interfaces/jdbc/org/postgresql/util/PGbytea.java    Wed Oct 16 19:44:55 2002
> @@ -62,10 +62,33 @@
>      {
>          if (p_buf == null)
>              return null;
> -        StringBuffer l_strbuf = new StringBuffer();
> +
> +                int l_int;
> +                StringBuffer l_strbuf;
> +
> +                if (p_buf.length > 10000) {
> +                  // calculate buffer size
> +                  int size = 0;
> +                  for (int i = 0; i < p_buf.length; i++) {
> +                    l_int = (int)p_buf[i];
> +                    if (l_int < 0)
> +                      l_int = 256 + l_int;
> +
> +                    if (l_int < 040 || l_int > 0176)
> +                      size += 5;
> +                    else if (p_buf[i] == (byte)'\\')
> +                      size += 4;
> +                    else
> +                      size += 1;
> +                  }
> +                  l_strbuf = new StringBuffer(size);
> +                }
> +                else
> +                  l_strbuf = new StringBuffer();
> +
>          for (int i = 0; i < p_buf.length; i++)
>          {
> -            int l_int = (int)p_buf[i];
> +            l_int = (int)p_buf[i];
>              if (l_int < 0)
>              {
>                  l_int = 256 + l_int;
>
>
> ------------------------------------------------------------------------
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html




В списке pgsql-patches по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Fixes for autocommit = off
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Large file support on AIX