Skip to content

Commit 98317fc

Browse files
committed
UseSpanToReduceAllocations
1 parent c7ea861 commit 98317fc

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
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 contentLengthLabel[] = "content-length:";
509+
const char transferEncodingLabel[] = "transfer-encoding:";
510+
if (s.begins_with(contentLengthLabel)) {
511+
const int index = (sizeof(contentLengthLabel) / sizeof(contentLengthLabel[0])) - 1;
512+
Span<char32_t> sp = Span<char32_t>(s.ptr() + index, s.length() - index);
513+
sp = String::strip_edges_span(sp.begin(), sp.size(), 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(transferEncodingLabel)) {
518+
const int index = (sizeof(transferEncodingLabel) / sizeof(transferEncodingLabel[0])) - 1;
519+
Span<char32_t> sp = Span<char32_t>(s.ptr() + index, s.length() - index);
520+
sp = String::strip_edges_span(sp.begin(), sp.size(), 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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,37 +4345,41 @@ 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(const char32_t *ptr, uint32_t len, bool left, bool right) {
4349+
int beg = 0, end = static_cast<int>(len);
43514350

43524351
if (left) {
4353-
for (int i = 0; i < len; i++) {
4354-
if (operator[](i) <= 32) {
4352+
for (int i = 0; i < end; i++) {
4353+
if (ptr[i] <= 32) {
43554354
beg++;
43564355
} else {
43574356
break;
43584357
}
43594358
}
43604359
}
43614360

4362-
if (right) {
4363-
for (int i = len - 1; i >= 0; i--) {
4364-
if (operator[](i) <= 32) {
4361+
if (right && len) {
4362+
for (int i = end - 1; i >= beg; i--) {
4363+
if (ptr[i] <= 32) {
43654364
end--;
43664365
} else {
43674366
break;
43684367
}
43694368
}
43704369
}
43714370

4372-
if (beg == 0 && end == len) {
4373-
return *this;
4374-
}
4371+
return Span(ptr + beg, static_cast<uint64_t>(end - beg));
4372+
}
43754373

4376-
return substr(beg, end - beg);
4374+
String String::strip_edges(bool left, bool right) const {
4375+
const char32_t *p = ptr();
4376+
Span<char32_t> sp = strip_edges_span(p, length(), left, right);
4377+
4378+
return substr(sp.begin() - p, sp.size());
43774379
}
43784380

4381+
4382+
43794383
String String::strip_escapes() const {
43804384
String new_string;
43814385
for (int i = 0; i < length(); i++) {

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(const char32_t *sp, uint32_t len, bool left, bool 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)