]> Savannah Git Hosting - gnulib.git/commitdiff
areadlink-with-size: guess a buffer size with 0 size
authorPádraig Brady <P@draigBrady.com>
Sat, 6 Jul 2019 18:43:11 +0000 (19:43 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 6 Jul 2019 18:53:41 +0000 (19:53 +0100)
The size is usually taken from st_size, which can be zero,
resulting in inefficient operation as seen with:

  $ strace -e readlink stat -c %N /proc/$$/cwd
  readlink("/proc/9036/cwd", "/", 1)      = 1
  readlink("/proc/9036/cwd", "/h", 2)     = 2
  readlink("/proc/9036/cwd", "/hom", 4)   = 4
  readlink("/proc/9036/cwd", "/home/pa", 8) = 8
  readlink("/proc/9036/cwd", "/home/padraig", 16) = 13

Instead let zero select an initial memory allocation
of 128 bytes, which most symlinks fit within.

* lib/areadlink-with-size.c (areadlink_with_size):
Start with a 128 byte buffer, for SIZE == 0.
* lib/areadlinkat-with-size.c (areadlinkat_with_size): Likewise.

ChangeLog
lib/areadlink-with-size.c
lib/areadlinkat-with-size.c

index 885907d5c6d680e000d3f99f12ce6f97399f06c2..0b061317cf62b479a3c2ff6f1111fae2cbcb85fa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2019-07-06  Pádraig Brady  <P@draigBrady.com>
+
+       areadlink-with-size: guess a buffer size with 0 size
+       The size is usually taken from st_size, which can be zero,
+       resulting in inefficient operation.
+       Instead let zero select an initial memory allocation
+       of 128 bytes, which most symlinks fit within.
+       * lib/areadlink-with-size.c (areadlink_with_size):
+       Start with a 128 byte buffer, for SIZE == 0.
+       * lib/areadlinkat-with-size.c (areadlinkat_with_size): Likewise.
+
 2019-07-06  Konstantin Kharlamov  <Hi-Angel@yandex.ru>
 
        Replace manually crafted hex regexes with [:xdigit:]
index 364cc085845f261dc9abf67e11efdc6cc34c45e8..b9cd05cba6fcf4a10c33a69f5c32e98ee26e0ea9 100644 (file)
@@ -61,7 +61,8 @@ areadlink_with_size (char const *file, size_t size)
                           : INITIAL_LIMIT_BOUND);
 
   /* The initial buffer size for the link value.  */
-  size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
+  size_t buf_size = (size == 0 ? 128
+                     : size < initial_limit ? size + 1 : initial_limit);
 
   while (1)
     {
index 5b2bccc4a5508b4ebcc5cf573b1019b5ad07169e..d39096f2bc04684c67a16eead8022612500ee1cc 100644 (file)
@@ -66,7 +66,8 @@ areadlinkat_with_size (int fd, char const *file, size_t size)
                           : INITIAL_LIMIT_BOUND);
 
   /* The initial buffer size for the link value.  */
-  size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
+  size_t buf_size = (size == 0 ? 128
+                     : size < initial_limit ? size + 1 : initial_limit);
 
   while (1)
     {