Potential solution for
https://github.com/llvm/llvm-project/issues/57324.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
If possible, it'd be good to have some performance data on this since it adds an extra condition on every vector resize.
Can this be limited to instantiations where the original original size is 0, as IIUC this only applies to llvm::SmallVector<T, 0>?
I don't know how to properly check for that.
Say the original instantiation is llvm::SmallVector<T, 0>, so initially capacity is set to 0, but after the vector is used, the size and capacity have increased, and the heap allocated memory is in a completely different region (safe). Now say that we're at a point where we want to further increase capacity, and it just so happens that the memory right after (at FirstEl) is available and, this time, *that's* what safe_realloc returns. This is the case at line:153.
The case at line :145, we're already in a case where the vector has no heap allocated since BeginX == FirstEl (if we entered this condition and there's heap allocated, we failed to fix the problem this patch is trying to fix...). So the only scenario in which NewElts can be equal to FirstEl is when the vector was created with capacity 0. The check would be redundant here.
The third case, when mallocForGrow is called (line :132), it's also possible the capacity was already increased and this is called to increase it further. So checking for capacity 0 here wouldn't stop allocating right at FirstEl.
Please let me know if I missed something in the above reasoning.