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 22, 2024
1 parent 3114d6d commit 1ab71cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
17 changes: 12 additions & 5 deletions src/workerd/api/data-url.c++
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ kj::Maybe<DataUrl> DataUrl::tryParse(kj::StringPtr url) {
return kj::none;
}

static constexpr kj::FixedArray<uint8_t, 256> ascii_whitespace_table = []() consteval {
kj::FixedArray<uint8_t, 256> result{};
for (uint8_t c: {0x09, 0x0a, 0x0c, 0x0d, 0x20}) {
result[c] = true;
}
return result;
}();

constexpr bool isAsciiWhitespace(uint8_t c) noexcept {
return ascii_whitespace_table[c];
}

kj::Maybe<DataUrl> DataUrl::from(const jsg::Url& url) {
if (url.getProtocol() != "data:"_kj) return kj::none;
auto clone = url.clone(jsg::Url::EquivalenceOption::IGNORE_FRAGMENTS);
Expand All @@ -21,11 +33,6 @@ 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 const auto trim = [](auto label) {
size_t start = 0;
auto end = label.size();
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 @@ -86,11 +86,11 @@ void parseFormData(kj::Maybe<jsg::Lock&> js,
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
9 changes: 4 additions & 5 deletions src/workerd/util/mimetype.c++
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <kj/debug.h>
#include <kj/string-tree.h>
#include <workerd/util/string-buffer.h>
#include <array>

namespace workerd {

Expand All @@ -16,8 +15,8 @@ constexpr bool isWhitespace(const char c) noexcept {
return (c == '\r' || c == '\n' || c == '\t' || c == ' ');
}

constexpr std::array<uint8_t, 256> token_table = []() consteval {
std::array<uint8_t, 256> result{};
static constexpr kj::FixedArray<uint8_t, 256> token_table = []() consteval {
kj::FixedArray<uint8_t, 256> result{};

for (uint8_t c:
{'!', '#', '$', '%', '&', '\'', '*', '+', '\\', '-', '.', '^', '_', '`', '|', '~'}) {
Expand Down Expand Up @@ -46,8 +45,8 @@ constexpr bool isTokenChar(const uint8_t c) noexcept {
return token_table[c];
}

constexpr std::array<uint8_t, 256> quoted_string_token_table = []() consteval {
std::array<uint8_t, 256> result{};
static constexpr kj::FixedArray<uint8_t, 256> quoted_string_token_table = []() consteval {
kj::FixedArray<uint8_t, 256> result{};
result['\t'] = true;

for (uint8_t c = 0x20; c <= 0x7e; c++) {
Expand Down

0 comments on commit 1ab71cd

Please sign in to comment.