This is an archive of the discontinued LLVM Phabricator instance.

[SmallVector] Reallocate if assigned memory is right after the current vector, created with capacity 0
ClosedPublic

Authored by asbirlea on Aug 23 2022, 3:43 PM.

Diff Detail

Event Timeline

asbirlea created this revision.Aug 23 2022, 3:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2022, 3:43 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
asbirlea requested review of this revision.Aug 23 2022, 3:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2022, 3:43 PM

If possible, it'd be good to have some performance data on this since it adds an extra condition on every vector resize.

fhahn added a comment.Aug 30 2022, 5:35 AM

Can this be limited to instantiations where the original original size is 0, as IIUC this only applies to llvm::SmallVector<T, 0>?

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.

dblaikie accepted this revision.Sep 23 2022, 2:24 PM

Seems good to me.

This revision is now accepted and ready to land.Sep 23 2022, 2:24 PM
This revision was landed with ongoing or failed builds.Oct 6 2022, 3:38 PM
This revision was automatically updated to reflect the committed changes.