llvm-project/clang/lib
Alex Lorenz 0d9b91524e [Preprocessor] Reduce the memory overhead of `#define` directives
Recently we observed high memory pressure caused by clang during some parallel builds.
We discovered that we have several projects that have a large number of #define directives
in their TUs (on the order of millions), which caused huge memory consumption in clang due
to a lot of allocations for MacroInfo. We would like to reduce the memory overhead of
clang for a single #define to reduce the memory overhead for these files, to allow us to
reduce the memory pressure on the system during highly parallel builds. This change achieves
that by removing the SmallVector in MacroInfo and instead storing the tokens in an array
allocated using the bump pointer allocator, after all tokens are lexed.

The added unit test with 1000000 #define directives illustrates the problem. Prior to this
change, on arm64 macOS, clang's PP bump pointer allocator allocated 272007616 bytes, and
used roughly 272 bytes per #define. After this change, clang's PP bump pointer allocator
allocates 120002016 bytes, and uses only roughly 120 bytes per #define.

For an example test file that we have internally with 7.8 million #define directives, this
change produces the following improvement on arm64 macOS: Persistent allocation footprint for
this test case file as it's being compiled to LLVM IR went down 22% from 5.28 GB to 4.07 GB
and the total allocations went down 14% from 8.26 GB to 7.05 GB. Furthermore, this change
reduced the total number of allocations made by the system for this clang invocation from
1454853 to 133663, an order of magnitude improvement.

Differential Revision: https://reviews.llvm.org/D117348
2022-02-11 15:01:10 -08:00
..
APINotes [clang][APINotes] Fix -Wunused-function warning (NFC) 2021-04-01 09:52:43 +08:00
ARCMigrate [clang] Use true/false instead of 1/0 (NFC) 2022-01-09 00:19:47 -08:00
AST [clang][NFC] Standard substitution checking cleanup 2022-02-10 04:44:48 -08:00
ASTMatchers Add `isConstinit` matcher 2022-01-24 08:35:42 -05:00
Analysis [clang][dataflow] Include terminator statements in buildStmtToBasicBlockMap 2022-02-11 11:15:29 +00:00
Basic [OpenCL] Add support of language builtins for OpenCL C 3.0 2022-02-11 15:53:44 +03:00
CodeGen [NFC] clang-format one function. 2022-02-11 15:00:29 -08:00
CrossTU Revert "[analyzer][ctu] Fix wrong 'multiple definitions' errors caused by space characters in lookup names when parsing the ctu index file" 2021-12-16 20:46:51 -05:00
DirectoryWatcher [clang] [DirectoryWatcher] Remove leading \\?\ from GetFinalPathNameByHandleW 2021-11-08 22:21:30 +02:00
Driver Reland "[lld/coff] Make lld-link work in a non-MSVC shell, add /winsysroot:" 2022-02-11 17:07:33 -05:00
Edit [clang] Use true/false instead of 1/0 (NFC) 2022-01-09 00:19:47 -08:00
Format [clang-format] Improve require and concept handling 2022-02-11 22:42:37 +01:00
Frontend [clang] RewriteModernObjC::SynthBlockInitExpr - remove block nullptr check 2022-02-11 15:09:44 +00:00
FrontendTool [clang] Add an extract-api driver option 2022-01-26 11:31:12 -08:00
Headers [OpenCL] Add OpenCL 3.0 atomics to -fdeclare-opencl-builtins 2022-02-11 10:14:14 +00:00
Index [clang] Visit enum base specifiers in libIndex 2021-10-25 13:16:14 +02:00
IndexSerialization
Interpreter [clang] Remove unused forward declarations (NFC) 2022-01-08 11:56:40 -08:00
Lex [Preprocessor] Reduce the memory overhead of `#define` directives 2022-02-11 15:01:10 -08:00
Parse Allow parameter pack expansions and initializer lists in annotate attribute 2022-02-08 13:38:07 -05:00
Rewrite Add assert on End iteration distance to Rewriter::getRewrittenText. 2022-01-25 17:26:13 +00:00
Sema [clang][sema] - remove CodeCompleter nullptr checks 2022-02-11 15:09:44 +00:00
Serialization [Preprocessor] Reduce the memory overhead of `#define` directives 2022-02-11 15:01:10 -08:00
StaticAnalyzer [analyzer] Restrict CallDescription fuzzy builtin matching 2022-02-11 10:45:18 +01:00
Testing
Tooling [NFC] Fix sign-compare warning in GrammarBNF thanks to int promotion 2022-02-09 11:25:58 -08:00
CMakeLists.txt [clang-repl] Recommit "Land initial infrastructure for incremental parsing" 2021-05-13 06:30:29 +00:00