Skip to content

Commit

Permalink
small perf improvements on workerd/api
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 20, 2024
1 parent 249464f commit 384ee26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/workerd/api/data-url.c++
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "data-url.h"
#include <kj/encoding.h>
#include <array>

namespace workerd::api {

Expand All @@ -21,12 +22,16 @@ kj::Maybe<DataUrl> DataUrl::from(const jsg::Url& url) {
// Per the fetch spec, it doesn't matter if the comma is within a quoted
// string value in the MIME type... which is fun.

static const auto isAsciiWhitespace = [](auto c) {
return c == 0x09 /* tab */ ||
c == 0x0a /* lf */ ||
c == 0x0c /* ff */ ||
c == 0x0d /* cr */ ||
c == 0x20 /* sp */;
static constexpr std::array<uint8_t, 256> ascii_whitespace_table = []() consteval {
std::array<uint8_t, 256> result{};
for (uint8_t c : { 0x09, 0x0a, 0x0c, 0x0d, 0x20 }) {
result[c] = true;
}
return result;
}();

static constexpr auto isAsciiWhitespace = [](uint8_t c) {
return ascii_whitespace_table[c];
};

static const auto trim = [](auto label) {
Expand Down
6 changes: 3 additions & 3 deletions src/workerd/api/form-data.c++
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ void parseFormData(kj::Maybe<jsg::Lock&> js, kj::Vector<FormData::Entry>& data,
const auto done = [](kj::ArrayPtr<const char>& body) {
// Consume any (CR)LF characters that trailed the boundary and indicate continuation, or consume
// the terminal "--" characters and indicate termination, or throw an error.
if (startsWith(body, "\n")) {
if (startsWith(body, "\n"_kj)) {
body = body.slice(1, body.size());
} else if (startsWith(body, "\r\n")) {
} else if (startsWith(body, "\r\n"_kj)) {
body = body.slice(2, body.size());
} else if (startsWith(body, "--")) {
} else if (startsWith(body, "--"_kj)) {
// We're done!
return true;
} else {
Expand Down

0 comments on commit 384ee26

Please sign in to comment.