Skip to content

Commit d0caa2e

Browse files
committed
UseSpanToReduceAllocations
1 parent c7ea861 commit d0caa2e

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

core/io/http_client_tcp.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ Error HTTPClientTCP::poll() {
482482
(rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') ||
483483
(rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) {
484484
// End of response, parse.
485-
response_str.push_back(0);
486485
String response = String::utf8((const char *)response_str.ptr(), response_str.size());
487486
Vector<String> responses = response.split("\n");
488487
body_size = -1;
@@ -506,13 +505,20 @@ Error HTTPClientTCP::poll() {
506505
if (s.length() == 0) {
507506
continue;
508507
}
509-
if (s.begins_with("content-length:")) {
510-
body_size = s.substr(s.find_char(':') + 1).strip_edges().to_int();
508+
const char content_length_label[] = "content-length:";
509+
const char transfer_encoding_label[] = "transfer-encoding:";
510+
if (s.begins_with(content_length_label)) {
511+
const int index = std::size(content_length_label) - 1;
512+
Span<char32_t> sp = Span<char32_t>(s.ptr() + index, s.length() - index);
513+
sp = String::strip_edges_span(sp, true, true);
514+
body_size = String::to_int(sp.begin(), sp.size());
511515
body_left = body_size;
512516

513-
} else if (s.begins_with("transfer-encoding:")) {
514-
String encoding = header.substr(header.find_char(':') + 1).strip_edges();
515-
if (encoding == "chunked") {
517+
} else if (s.begins_with(transfer_encoding_label)) {
518+
const int index = std::size(transfer_encoding_label) - 1;
519+
Span<char32_t> sp = Span<char32_t>(s.ptr() + index, s.length() - index);
520+
sp = String::strip_edges_span(sp, true, true);
521+
if (String("chunked") == sp) {
516522
chunked = true;
517523
}
518524
} else if (s.begins_with("connection: close")) {

core/string/ustring.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,35 +4345,38 @@ String String::dedent() const {
43454345
return new_string;
43464346
}
43474347

4348-
String String::strip_edges(bool left, bool right) const {
4349-
int len = length();
4350-
int beg = 0, end = len;
4348+
Span<char32_t> String::strip_edges_span(Span<char32_t> p_span, bool p_left, bool p_right) {
4349+
uint64_t beg = 0;
4350+
uint64_t end = p_span.size();
43514351

4352-
if (left) {
4353-
for (int i = 0; i < len; i++) {
4354-
if (operator[](i) <= 32) {
4352+
if (p_left) {
4353+
for (uint64_t i = 0; i < end; i++) {
4354+
if (p_span[i] <= 32) {
43554355
beg++;
43564356
} else {
43574357
break;
43584358
}
43594359
}
43604360
}
43614361

4362-
if (right) {
4363-
for (int i = len - 1; i >= 0; i--) {
4364-
if (operator[](i) <= 32) {
4362+
if (p_right && (end > 0)) {
4363+
for (uint64_t i = end - 1; i > beg; i--) {
4364+
if (p_span[i] <= 32) {
43654365
end--;
43664366
} else {
43674367
break;
43684368
}
43694369
}
43704370
}
43714371

4372-
if (beg == 0 && end == len) {
4373-
return *this;
4374-
}
4372+
return Span(p_span.ptr() + beg, static_cast<uint64_t>(end - beg));
4373+
}
4374+
4375+
String String::strip_edges(bool left, bool right) const {
4376+
const char32_t *p = ptr();
4377+
Span<char32_t> sp = strip_edges_span(span(), left, right);
43754378

4376-
return substr(beg, end - beg);
4379+
return substr(sp.begin() - p, sp.size());
43774380
}
43784381

43794382
String String::strip_escapes() const {

core/string/ustring.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ class String {
293293
_FORCE_INLINE_ operator Span<char32_t>() const { return Span(ptr(), length()); }
294294
_FORCE_INLINE_ Span<char32_t> span() const { return Span(ptr(), length()); }
295295

296+
static Span<char32_t> strip_edges_span(Span<char32_t> p_span, bool p_left, bool p_right);
297+
296298
void remove_at(int p_index) { _cowdata.remove_at(p_index); }
297299

298300
_FORCE_INLINE_ void clear() { resize(0); }

0 commit comments

Comments
 (0)