Skip to content

Commit 71e17da

Browse files
author
pfeatherstone
committed
optimization
1 parent e9d6885 commit 71e17da

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ Note, the example server is single threaded, uses C++20 coroutines and basic aut
4444

4545
| Transport | Connections | Requests / s |
4646
| --------- | ----------- | -------------|
47-
| TCP | 1 | 29268.81 |
48-
| TCP | 2 | 41123.15 |
49-
| TCP | 5 | 42906.12 |
50-
| TCP | 10 | 43582.05 |
51-
| TLS | 1 | 20317.42 |
52-
| TLS | 2 | 32633.12 |
53-
| TLS | 5 | 35227.99 |
54-
| TLS | 10 | 36615.81 |
47+
| TCP | 1 | 42654.66 |
48+
| TCP | 2 | 74901.81 |
49+
| TCP | 5 | 88949.07 |
50+
| TCP | 10 | 94381.10 |
51+
| TLS | 1 | 30400.27 |
52+
| TLS | 2 | 50593.61 |
53+
| TLS | 5 | 57112.41 |
54+
| TLS | 10 | 62193.85 |
5555

5656
Not bad.
5757

src/http.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,17 @@ namespace http
428428

429429
//----------------------------------------------------------------------------------------------------------------
430430

431-
std::string to_lower(std::string_view s)
431+
constexpr char fast_ascii_tolower(const char c)
432432
{
433-
std::string out(s.length(), '\0');
434-
std::transform(s.begin(), s.end(), out.begin(), [](unsigned char c) { return std::tolower(c); });
435-
return out;
433+
// The following is a tad faster than std::tolower(c)
434+
return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c;
435+
}
436+
437+
constexpr bool case_insenstive_equals(std::string_view a, std::string_view b)
438+
{
439+
return a.size() == b.size() && std::equal(begin(a), end(a), begin(b), [](char ac, char bc) {
440+
return fast_ascii_tolower(ac) == fast_ascii_tolower(bc);
441+
});
436442
}
437443

438444
std::string_view field_label(field f)
@@ -443,7 +449,7 @@ namespace http
443449
field field_enum(std::string_view f)
444450
{
445451
for (unsigned int i = 0 ; i < std::size(FIELDS) ; ++i)
446-
if (FIELDS[i] == to_lower(f))
452+
if (case_insenstive_equals(FIELDS[i], f))
447453
return (field)i;
448454
return unknown_field;
449455
}

0 commit comments

Comments
 (0)