This is an archive of the discontinued LLVM Phabricator instance.

ADT: Avoid using SmallVector::set_size() in SmallString
ClosedPublic

Authored by dexonsmith on Dec 8 2021, 12:47 PM.

Details

Summary

Update SmallString::append() to use resize_for_overwrite() instead
of reserve() followed by set_size().

A step toward https://reviews.llvm.org/D115380.

Diff Detail

Event Timeline

dexonsmith requested review of this revision.Dec 8 2021, 12:47 PM
dexonsmith created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2021, 12:47 PM
dblaikie accepted this revision.Dec 9 2021, 12:09 PM
dblaikie added inline comments.
llvm/include/llvm/ADT/SmallString.h
79–80

Should this use copy, rather than uninitialized_copy? Technically the chars are already constructed, they're just uninitialized - this code would technically cause the same memory to be reconstructed? (I mean, we're only talking about rerunning pseudo constructors so I /think/ it's technically fine either way - but may be simpler to think about using copy?)

This revision is now accepted and ready to land.Dec 9 2021, 12:09 PM
dexonsmith added inline comments.Dec 9 2021, 2:38 PM
llvm/include/llvm/ADT/SmallString.h
79–80

Hmm... agreed this isn't great. this->uninitialized_copy() will call memcpy in this case, but you can't tell that from here. Would std::copy() call memcpy? Probably, but hard to tell, could depend on std library.

I considered switching to a reserve() plus a loop through append(StringRef), but git-blame told me that this function was added in cf8d19f4fb2ca0eb6b7f8169d1d7ff68ba95d9f5 specifically to avoid successive calls to reserve() that the optimizer can't tell are redundant.

I'm tempted at this point to leave it as-is and remove the "make it private" part of https://reviews.llvm.org/D115380, ultimately leaving set_size() available for subclasses to use. WDYT?

dblaikie added inline comments.Dec 9 2021, 3:29 PM
llvm/include/llvm/ADT/SmallString.h
79–80

I'd generally be happy with std::copy if that suits - I think it's really OK to rely on a reasonably well-performing standard library implementation.

This revision was landed with ongoing or failed builds.Jan 11 2022, 5:38 PM
This revision was automatically updated to reflect the committed changes.
dexonsmith marked 2 inline comments as done.

Thanks for the review! Landed in 4d04526bf48d982055fd625803734b2afded3afb.

llvm/include/llvm/ADT/SmallString.h
77

Moved this to precede the for loop before landing (must have moved it down to make the diff look nicer and failed to rerun tests...).

79–80

Yeah, I was overthinking this. Landed change uses std::copy().