Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

micro optimization: do not serialize PLAINTEXT mimetype #1025

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ Body::ExtractedBody Body::extractBody(jsg::Lock& js, Initializer init) {
return kj::mv(stream);
}
KJ_CASE_ONEOF(text, kj::String) {
contentType = MimeType::PLAINTEXT.toString();
contentType = kj::str(MimeType::PLAINTEXT_STRING);
buffer = kj::mv(text);
}
KJ_CASE_ONEOF(bytes, kj::Array<byte>) {
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/kv.c++
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ jsg::Promise<void> KvNamespace::put(

KJ_SWITCH_ONEOF(supportedBody) {
KJ_CASE_ONEOF(text, kj::String) {
headers.set(kj::HttpHeaderId::CONTENT_TYPE, MimeType::PLAINTEXT.toString());
headers.set(kj::HttpHeaderId::CONTENT_TYPE, MimeType::PLAINTEXT_STRING);
expectedBodySize = uint64_t(text.size());
}
KJ_CASE_ONEOF(data, kj::Array<byte>) {
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3654,10 +3654,10 @@ kj::Promise<void> Worker::Isolate::SubrequestClient::request(
}

} else {
response.setMimeType(MimeType::PLAINTEXT.toString());
response.setMimeType(MimeType::PLAINTEXT_STRING);
}
} else {
response.setMimeType(MimeType::PLAINTEXT.toString());
response.setMimeType(MimeType::PLAINTEXT_STRING);
}
headersToCDP(headers, response.initHeaders());

Expand Down
3 changes: 2 additions & 1 deletion src/workerd/util/mimetype.c++
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ kj::String KJ_STRINGIFY(const MimeType& mimeType) {
return mimeType.toString();
}

const MimeType MimeType::PLAINTEXT = MimeType::parse("text/plain;charset=UTF-8"_kj);
const kj::StringPtr MimeType::PLAINTEXT_STRING = "text/plain;charset=UTF-8"_kj;
const MimeType MimeType::PLAINTEXT = MimeType::parse(PLAINTEXT_STRING);
const MimeType MimeType::CSS = MimeType("text"_kj, "css"_kj);
const MimeType MimeType::HTML = MimeType("text"_kj, "html"_kj);
const MimeType MimeType::TEXT_JAVASCRIPT = MimeType("text"_kj, "javascript"_kj);
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/util/mimetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class MimeType final {
static const MimeType VTT;
static const MimeType EVENT_STREAM;

// exposed directly for performance reasons
static const kj::StringPtr PLAINTEXT_STRING;

private:
kj::String type_;
kj::String subtype_;
Expand Down
Loading