[llvm] 932ad99 - [Support] FoldingSetNodeID::AddString(): reserve memory
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 13:27:31 PDT 2020
Author: Roman Lebedev
Date: 2020-06-08T23:26:54+03:00
New Revision: 932ad9941deffeb540563973564514e4f8b3cb63
URL: https://github.com/llvm/llvm-project/commit/932ad9941deffeb540563973564514e4f8b3cb63
DIFF: https://github.com/llvm/llvm-project/commit/932ad9941deffeb540563973564514e4f8b3cb63.diff
LOG: [Support] FoldingSetNodeID::AddString(): reserve memory
Summary:
It is traditionally potentially very inefficient to not preallocate the memory,
but rely on reallocation every time you push something into vector.
For example, looking at unity build of RawSpeed
(`-O3 -g0 -emit-llvm -Xclang -disable-llvm-optzns`),
the memory story is as follows:
```
total runtime: 11.34s.
calls to allocation functions: 2694053 (237612/s)
temporary memory allocations: 645188 (56904/s)
peak heap memory consumption: 231.36MB
peak RSS (including heaptrack overhead): 397.39MB
```
Looking at details, `FoldingSetNodeID::AddString()` is noteworthy, frequently called and is allocation-heavy.
But it is quite obvious how many times we will push into `Bits` - we will push `String.size()` itself,
and then we will push once per every 4 bytes of `String` (padding last block).
And if we preallocate, we get:
```
total runtime: 11.20s.
calls to allocation functions: 2594704 (231669/s)
temporary memory allocations: 560004 (50000/s)
peak heap memory consumption: 231.36MB
peak RSS (including heaptrack overhead): 398.06MB
```
Which is a measurable win:
```
total runtime: -0.14s. # -1.23 %
calls to allocation functions: -99349 (719920/s) # -3.69 %
temporary memory allocations: -85184 (617275/s) # -13.2 % (!)
peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B
```
Reviewers: efriedma, nikic, bkramer
Reviewed By: bkramer
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D81342
Added:
Modified:
llvm/lib/Support/FoldingSet.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index e195089b667c..e3d7168305af 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -86,6 +86,10 @@ void FoldingSetNodeID::AddInteger(unsigned long long I) {
void FoldingSetNodeID::AddString(StringRef String) {
unsigned Size = String.size();
+
+ unsigned NumInserts = 1 + divideCeil(Size, 4);
+ Bits.reserve(Bits.size() + NumInserts);
+
Bits.push_back(Size);
if (!Size) return;
More information about the llvm-commits
mailing list