[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

[PATCH] strlen() optimizations (1)

From: David Mankin <mankin_at_ants.com>
Date: 2002-12-06 04:06:44 CET

A couple weeks ago, someone was doing profiling on the code. It was
briefly claimed (and later discounted) that strlen() was causing a
noticeable slow-down. So, I went through the code base and looked at
each use of strlen() to find out if it was necessary. It turns out the
svn code is not doing very many excessive strlen calls. But there are
a few. Find below the first strlen optimization patches. Probably, in
the grand scheme of things, it's not worth doing this optimization.
However, since I've already done it, I say it should be reviewed and
applied. :-)

These all pass make check for ra_local, but I have no way to run
ra_dav. So please review the svn_ra_dav/fetch.c change extra carefully.

-David Mankin

A few STRLEN optimizations.

    * cmdline/feedback.c (notify): removed redundant strlen() call.

    * auth.c (get_password): removed unnecessary strlen() call.

    * svn_string.h, svn_string.c (svn_cstring_ensure_len,
      svn_cstring_strlencmp): new functions

    * fetch.c (svn_ra_dav__do_checkout): use new svn_cstring_strlencmp
      function for faster string length comparison.

Index: subversion/include/svn_string.h
===================================================================
--- subversion/include/svn_string.h (revision 3951)
+++ subversion/include/svn_string.h (working copy)
@@ -262,6 +262,24 @@
                                 svn_boolean_t chop_whitespace,
                                 apr_pool_t *pool);

+/* Checks to see if cstring S is at least LEN characters.
+ * Better than calling strlen because it only compares at most
+ * LEN characters.
+ *
+ * Returns TRUE when S is at least LEN characters, FALSE otherwise.
+ */
+svn_boolean_t svn_cstring_ensure_len (const char *s,
+ apr_size_t len);
+
+
+/* Returns 1 if A is longer than B, -1 if B is longer than A, and
+ * 0 if A and B are the same length.
+ *
+ * This is better than using (strlen(a) > strlen(b)) because only
+ * O(min(len(a), len(b))) chars are examined instead of O(len(a) +
len(b))
+ */
+int svn_cstring_strlencmp (const char* a,
+ const char* b);

  
  #ifdef __cplusplus
Index: subversion/libsvn_subr/svn_string.c
===================================================================
--- subversion/libsvn_subr/svn_string.c (revision 3951)
+++ subversion/libsvn_subr/svn_string.c (working copy)
@@ -579,3 +579,35 @@
    svn_cstring_split_append (a, input, sep_chars, chop_whitespace,
pool);
    return a;
  }
+
+
+svn_boolean_t
+svn_cstring_ensure_len (const char *s, apr_size_t len)
+{
+ apr_size_t i;
+ for (i = 0 ; i < len ; i ++)
+ {
+ if (s[i] == '\0')
+ return FALSE;
+ }
+ return TRUE;
+}
+
+int
+svn_cstring_strlencmp (const char* a, const char* b)
+{
+ apr_size_t i;
+
+ for (i = 0 ; ; i++)
+ {
+ if ((b[i] == '\0') && (a[i] != '\0')) /* B is shorter */
+ return 1;
+ if (a[i] == '\0')
+ {
+ if (b[i] != '\0') /* A is shorter */
+ return -1;
+ else /* must be they're equal */
+ return 0;
+ }
+ }
+}
Index: subversion/libsvn_client/auth.c
===================================================================
--- subversion/libsvn_client/auth.c (revision 3951)
+++ subversion/libsvn_client/auth.c (working copy)
@@ -149,7 +149,7 @@
    svn_client__callback_baton_t *cb = baton;
    svn_client_auth_baton_t *ab = cb->auth_baton;

- if (strlen(username) > 0)
+ if (*username)
      prompt = apr_psprintf (pool, "%s's password: ", username);
    else
      prompt = apr_psprintf (pool, "password: ");
Index: subversion/clients/cmdline/feedback.c
===================================================================
--- subversion/clients/cmdline/feedback.c (revision 3951)
+++ subversion/clients/cmdline/feedback.c (working copy)
@@ -191,7 +191,6 @@

      case svn_wc_notify_commit_added:
        if (mime_type
- && ((strlen (mime_type)) > 5)
            && ((strncmp (mime_type, "text/", 5)) != 0))
          printf ("Adding (bin) %s\n", path_native);
        else
Index: subversion/libsvn_ra_dav/fetch.c
===================================================================
--- subversion/libsvn_ra_dav/fetch.c (revision 3951)
+++ subversion/libsvn_ra_dav/fetch.c (working copy)
@@ -1224,7 +1224,7 @@
              }
          }

- if (strlen(url) > strlen(bc_root))
+ if (svn_cstring_strlencmp(url, bc_root) > 0)
          {
            const char *comp;
            comp = svn_path_uri_decode(svn_path_basename(url, subpool),

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Fri Dec 6 04:14:38 2002

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.