llvm / llvm/llvm-project

[clang-tidy] report inefficient `std::string::append()` usage with `char` in `performance-faster-string-find`

Open
#188,193 5 comments 0 reactions 0 assignees View on GitHub
clang-tidy enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I have been looking into providing helper for efficient string concatenation in https://github.com/danmar/cppcheck/pull/8344.

I initially had the following for `char`.
```cpp
#include

void concat1(std::string& str, char c)
{
str.append(1, c);
}
```

Turns out that is less efficient then either of the following (which behave exactly the same):
```cpp
void concat2(std::string& str, char c)
{
str += c;
}

void concat3(std::string& str, char c)
{
str.push_back(c);
}
```
https://godbolt.org/z/1P4E7Wdcc

Contributor guide

Open the contributing guide

Research direction

Start by locating the clang-tidy check named performance-faster-string-find and read how it currently identifies inefficient string operations. Confirm the existing check and test structure, then define done as reporting std::string::append(1, c) while leaving the equivalent += and push_back forms unreported.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.