[PATCH] D119177: [demangler] Fix buffer growth
Nathan Sidwell via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 7 12:17:51 PST 2022
urnathan created this revision.
urnathan added reviewers: ChuanqiXu, bruno, iains, aaron.ballman.
urnathan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
The output buffer growth algorithm had a couple of issues:
a) An off-by-one error in the initial size check, which uses '>='. This error was safe, but could cause us to reallocate when there was no need.
b) An inconsistency between the initial size check (>=) and the post-doubling check (>). The latter was somewhat obscured by the swapped operands.
c) There would be many reallocs with an initially-small buffer. Add a little initialization hysteresis.
https://reviews.llvm.org/D119177
Files:
libcxxabi/src/demangle/Utility.h
llvm/include/llvm/Demangle/Utility.h
Index: llvm/include/llvm/Demangle/Utility.h
===================================================================
--- llvm/include/llvm/Demangle/Utility.h
+++ llvm/include/llvm/Demangle/Utility.h
@@ -33,12 +33,17 @@
size_t CurrentPosition = 0;
size_t BufferCapacity = 0;
- // Ensure there is at least n more positions in buffer.
+ // Ensure there is at least N more positions in the buffer.
void grow(size_t N) {
- if (N + CurrentPosition >= BufferCapacity) {
+ size_t Need = N + CurrentPosition;
+ if (Need > BufferCapacity) {
BufferCapacity *= 2;
- if (BufferCapacity < N + CurrentPosition)
- BufferCapacity = N + CurrentPosition;
+ if (Need > BufferCapacity) {
+ if (!BufferCapacity && Need < 100)
+ // Avoid many reallocations during startup
+ Need = 100;
+ BufferCapacity = Need;
+ }
Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
if (Buffer == nullptr)
std::terminate();
Index: libcxxabi/src/demangle/Utility.h
===================================================================
--- libcxxabi/src/demangle/Utility.h
+++ libcxxabi/src/demangle/Utility.h
@@ -33,12 +33,17 @@
size_t CurrentPosition = 0;
size_t BufferCapacity = 0;
- // Ensure there is at least n more positions in buffer.
+ // Ensure there is at least N more positions in the buffer.
void grow(size_t N) {
- if (N + CurrentPosition >= BufferCapacity) {
+ size_t Need = N + CurrentPosition;
+ if (Need > BufferCapacity) {
BufferCapacity *= 2;
- if (BufferCapacity < N + CurrentPosition)
- BufferCapacity = N + CurrentPosition;
+ if (Need > BufferCapacity) {
+ if (!BufferCapacity && Need < 100)
+ // Avoid many reallocations during startup
+ Need = 100;
+ BufferCapacity = Need;
+ }
Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
if (Buffer == nullptr)
std::terminate();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119177.406559.patch
Type: text/x-patch
Size: 1992 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220207/3ba7f26d/attachment.bin>
More information about the llvm-commits
mailing list