[PATCH] D115374: [NFC][clang] Return std::strings built from raw_string_ostreams more efficiently
Logan Smith via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 8 12:26:10 PST 2021
logan-5 created this revision.
logan-5 added a reviewer: aaron.ballman.
logan-5 added a project: clang.
Herald added subscribers: abrachet, ctetreau, dexonsmith, martong.
logan-5 requested review of this revision.
Herald added a subscriber: cfe-commits.
Returning `OS.str()` is guaranteed to copy the underlying string, whereas `OS.flush(); return Underlying;` makes `Underlying` a candidate for NRVO, or at worst, implicit move.
To keep this kind of inefficiency at bay in the future, the fast code should probably be made easier to type than the slow code (as it's currently the opposite). Perhaps this could be solved by:
- making `raw_string_ostream` guarantee+document that it does not need to be `flush()`ed (similar to `raw_svector_ostream`), //and/or//
- making `raw_string_ostream::str()` return a `StringRef` rather than `std::string&`, to discourage using it to initialize `std::string`s
Implementing those two ideas would make simply `return Underlying;` the natural, correct, and efficient choice. They are, however, out of scope for this patch.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115374
Files:
clang/include/clang/StaticAnalyzer/Checkers/SValExplainer.h
clang/include/clang/Testing/TestClangConfig.h
clang/lib/AST/ASTDiagnostic.cpp
clang/lib/AST/AttrImpl.cpp
clang/lib/AST/Decl.cpp
clang/lib/AST/DeclarationName.cpp
clang/lib/AST/OpenMPClause.cpp
clang/lib/ASTMatchers/Dynamic/Diagnostics.cpp
clang/lib/Analysis/AnalysisDeclContext.cpp
clang/lib/Basic/SourceLocation.cpp
clang/lib/Basic/Version.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Frontend/TestModuleFileExtension.cpp
clang/lib/Rewrite/HTMLRewrite.cpp
clang/lib/Sema/CodeCompleteConsumer.cpp
clang/lib/Sema/SemaAttr.cpp
clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
clang/lib/StaticAnalyzer/Core/MemRegion.cpp
clang/lib/Tooling/Syntax/Tokens.cpp
clang/lib/Tooling/Syntax/Tree.cpp
clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
clang/unittests/AST/ASTTraverserTest.cpp
clang/unittests/Analysis/MacroExpansionContextTest.cpp
clang/unittests/Tooling/Syntax/TreeTest.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115374.392864.patch
Type: text/x-patch
Size: 14815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211208/0a3d3fc5/attachment-0001.bin>
More information about the cfe-commits
mailing list