This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Remove APInt/APSInt toString() std::string variants
ClosedPublic

Authored by RKSimon on Jun 8 2021, 5:48 AM.

Details

Summary

<string> is currently the highest impact header in a clang+llvm build:

https://commondatastorage.googleapis.com/chromium-browser-clang/llvm-include-analysis.html

One of the most common places this is being included is the APInt.h header, which needs it for an old toString() implementation that returns std::string - an inefficient method compared to the SmallString versions that it actually wraps.

This patch replaces these APInt/APSInt methods with a pair of llvm::toString() helpers inside StringExtras.h, adjusts users accordingly and removes the <string> from APInt.h - I was hoping that more of these users could be converted to use the SmallString methods, but it appears that most end up creating a std::string anyhow. I avoided trying to use the raw_ostream << operators as well as I didn't want to lose having the integer radix explicit in the code.

Diff Detail

Event Timeline

RKSimon created this revision.Jun 8 2021, 5:48 AM
RKSimon requested review of this revision.Jun 8 2021, 5:48 AM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJun 8 2021, 5:48 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ormris removed a subscriber: ormris.Jun 9 2021, 10:29 AM
dblaikie accepted this revision.Jun 10 2021, 12:50 PM

Sounds OK.

I wouldn't mind the places that can use op<< to use that - not sure preserving the explicit radix argument is super high value. (I think people would generally assume that's the default)
Possible we could call it to_string, is std::to_string meant to be an ADL extension point, so that other types expose a to_string in their own associated namespace, etc?

llvm/unittests/ADT/APIntTest.cpp
1458–1468

Perhaps this test should move to the StringExtras unit test?

This revision is now accepted and ready to land.Jun 10 2021, 12:50 PM
This revision was landed with ongoing or failed builds.Jun 11 2021, 5:20 AM
This revision was automatically updated to reflect the committed changes.

Sounds OK.

I wouldn't mind the places that can use op<< to use that - not sure preserving the explicit radix argument is super high value. (I think people would generally assume that's the default)
Possible we could call it to_string, is std::to_string meant to be an ADL extension point, so that other types expose a to_string in their own associated namespace, etc?

We already have a llvm::to_string(V) wrapper inside ScopedPrinter.h that uses a temp llvm::raw_string_ostream to create the string - do you think that'd be good enough?

Sounds OK.

I wouldn't mind the places that can use op<< to use that - not sure preserving the explicit radix argument is super high value. (I think people would generally assume that's the default)
Possible we could call it to_string, is std::to_string meant to be an ADL extension point, so that other types expose a to_string in their own associated namespace, etc?

We already have a llvm::to_string(V) wrapper inside ScopedPrinter.h that uses a temp llvm::raw_string_ostream to create the string - do you think that'd be good enough?

That's a weird place for such a general purpose/name template to live... but sort of.

What I mean is you could overload to_string to do the efficient thing - rather than having to stream it out, then come back to a string again. That way generic code would get the efficient behavior without intermediate streams.