Skip to content

Commit d9e5d49

Browse files
committed
remove slow std::isspace
1 parent d864fb5 commit d9e5d49

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

examples/baggage/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main() {
6767

6868
CinReader reader;
6969
std::cout << "Enter baggage (or 'CTRL+C' to quit): ";
70-
while (std::cin >> reader) {
70+
while (std::getline(std::cin, reader.input)) {
7171
auto baggage = tracer.extract_baggage(reader);
7272
if (!baggage) {
7373
std::cout << "Error parsing \"" << reader.input

src/datadog/baggage.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <datadog/baggage.h>
22

3-
#include <cctype>
4-
53
namespace datadog {
64
namespace tracing {
75

@@ -30,7 +28,7 @@ parse_baggage(StringView input, size_t max_capacity) {
3028
for (size_t i = 0; i < end; ++i) {
3129
switch (internal_state) {
3230
case state::leading_spaces_key: {
33-
if (!std::isspace(input[i])) {
31+
if (input[i] != ' ') {
3432
if (result.size() == max_capacity)
3533
return Baggage::Error::MAXIMUM_CAPACITY_REACHED;
3634

@@ -46,13 +44,13 @@ parse_baggage(StringView input, size_t max_capacity) {
4644
} else if (input[i] == '=') {
4745
key = StringView{input.data() + beg, tmp_end - beg + 1};
4846
internal_state = state::leading_spaces_value;
49-
} else if (!std::isspace(input[i])) {
47+
} else if (input[i] != ' ') {
5048
tmp_end = i;
5149
}
5250
} break;
5351

5452
case state::leading_spaces_value: {
55-
if (!std::isspace(input[i])) {
53+
if (input[i] != ' ') {
5654
beg = i;
5755
tmp_end = i;
5856
internal_state = state::value;
@@ -66,7 +64,7 @@ parse_baggage(StringView input, size_t max_capacity) {
6664
beg = i;
6765
tmp_end = i;
6866
internal_state = state::leading_spaces_key;
69-
} else if (!std::isspace(input[i])) {
67+
} else if (input[i] != ' ') {
7068
tmp_end = i;
7169
}
7270
} break;
@@ -165,6 +163,7 @@ Expected<Baggage, Baggage::Error> Baggage::extract(const DictReader& headers,
165163
return Error::MISSING_HEADER;
166164
}
167165

166+
// TODO(@dmehala): Avoid allocation
168167
auto bv = parse_baggage(*found, max_capacity);
169168
if (auto error = bv.if_error()) {
170169
return *error;

0 commit comments

Comments
 (0)