Skip to content

Commit ecca895

Browse files
[libc] add compile option for printf arg type array
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. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D131993
1 parent 35e2107 commit ecca895

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

libc/src/stdio/printf_core/parser.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "src/__support/arg_list.h"
1313
#include "src/stdio/printf_core/core_structs.h"
14+
#include "src/stdio/printf_core/printf_config.h"
1415

1516
#include <stddef.h>
1617

@@ -41,8 +42,10 @@ class Parser {
4142
return (size == other.size) && (primary_type == other.primary_type);
4243
}
4344
};
44-
// TODO: Make this size configurable via a compile option.
45-
static constexpr size_t DESC_ARR_LEN = 32;
45+
46+
// Defined in printf_config.h
47+
static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN;
48+
4649
// desc_arr stores the sizes of the variables in the ArgList. This is used in
4750
// index mode to reduce repeated string parsing. The sizes are stored as
4851
// TypeDesc objects, which store the size as well as minimal type information.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- Printf Configuration Handler ----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H
10+
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H
11+
12+
// The index array buffer is always initialized when printf is called. In cases
13+
// where index mode is necessary but memory is limited, or when index mode
14+
// performance is important and memory is available, this compile option
15+
// provides a knob to adjust memory usage to an appropriate level. 128 is picked
16+
// as the default size since that's big enough to handle even extreme cases and
17+
// the runtime penalty for not having enough space is severe.
18+
// When an index mode argument is requested, if its index is before the most
19+
// recently read index, then the arg list must be restarted from the beginning,
20+
// and all of the arguments before the new index must be requested with the
21+
// correct types. The index array caches the types of the values in the arg
22+
// list. For every number between the last index cached in the array and the
23+
// requested index, the format string must be parsed again to find the
24+
// type of that index. As an example, if the format string has 20 indexes, and
25+
// the index array is 10, then when the 20th index is requested the first 10
26+
// types can be found immediately, and then the format string must be parsed 10
27+
// times to find the types of the next 10 arguments.
28+
#ifndef LLVM_LIBC_PRINTF_INDEX_ARR_LEN
29+
#define LLVM_LIBC_PRINTF_INDEX_ARR_LEN 128
30+
#endif
31+
32+
// TODO(michaelrj): Move the other printf configuration options into this file.
33+
34+
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H

0 commit comments

Comments
 (0)