瀏覽代碼

Speed up tor_strndup a lot: profiling suggests that our use of strlcpy here was a bad idea.

svn:r2821
Nick Mathewson 21 年之前
父節點
當前提交
c466b7e72f
共有 1 個文件被更改,包括 6 次插入1 次删除
  1. 6 1
      src/common/util.c

+ 6 - 1
src/common/util.c

@@ -167,7 +167,12 @@ char *tor_strndup(const char *s, size_t n) {
   char *dup;
   tor_assert(s);
   dup = tor_malloc(n+1);
-  strlcpy(dup, s, n+1);
+  /* Performance note: Ordinarly we prefer strlcpy to strncpy.  But
+   * this function gets called a whole lot, and platform strncpy is
+   * much faster than strlcpy when strlen(s) is much longer than n.
+   */
+  strncpy(dup, s, n+1);
+  dup[n]='\0';
   return dup;
 }