[libc-commits] [PATCH] D131993: [libc] add compile option for printf arg type array

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Aug 16 14:05:11 PDT 2022


michaelrj created this revision.
michaelrj added reviewers: sivachandra, lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
michaelrj requested review of this revision.

This patch allows for adjusting the size of the array that printf uses
to track the types of arguments in index mode. This is useful for
optimizing the tradeoff between memory usage and performance.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131993

Files:
  libc/src/stdio/printf_core/parser.h


Index: libc/src/stdio/printf_core/parser.h
===================================================================
--- libc/src/stdio/printf_core/parser.h
+++ libc/src/stdio/printf_core/parser.h
@@ -42,14 +42,23 @@
       return (size == other.size) && (primary_type == other.primary_type);
     }
   };
-  // TODO: Make this size configurable via a compile option.
-  static constexpr size_t DESC_ARR_LEN = 32;
+// This buffer is always initialized when printf is called. In cases where index
+// mode is necessary but memory is limited, or when index mode performance is
+// important and memory is available, this compile option provides a knob to
+// adjust memory usage to an appropriate level. 128 is picked as the default
+// size since that's big enough to handle even extreme cases and the runtime
+// penalty for not having enough space is severe.
+#ifndef LLVM_LIBC_PRINTF_INDEX_ARR_LEN
+  static constexpr size_t DESC_ARR_LEN = 128;
+#else
+  static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN;
+#endif
   // desc_arr stores the sizes of the variables in the ArgList. This is used in
   // index mode to reduce repeated string parsing. The sizes are stored as
   // TypeDesc objects, which store the size as well as minimal type information.
   // This is necessary because some systems separate the floating point and
   // integer values in va_args.
-  TypeDesc desc_arr[DESC_ARR_LEN];
+  TypeDesc desc_arr[DESC_ARR_LEN] = {{0, Integer}};
 
   // TODO: Look into object stores for optimization.
 
@@ -58,10 +67,7 @@
 public:
 #ifndef LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE
   Parser(const char *__restrict new_str, internal::ArgList &args)
-      : str(new_str), args_cur(args), args_start(args) {
-    inline_memset(reinterpret_cast<char *>(desc_arr), 0,
-                  DESC_ARR_LEN * sizeof(TypeDesc));
-  }
+      : str(new_str), args_cur(args), args_start(args) {}
 #else
   Parser(const char *__restrict new_str, internal::ArgList &args)
       : str(new_str), args_cur(args) {}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131993.453123.patch
Type: text/x-patch
Size: 2014 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220816/b1f5c828/attachment.bin>


More information about the libc-commits mailing list