Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
35d54d0940 Add micro-optimization for node:http method name 2025-03-22 22:37:11 -07:00
2 changed files with 69 additions and 86 deletions

View File

@@ -34,7 +34,20 @@
macro(OperationWasAborted, "The operation was aborted.") \
macro(OperationTimedOut, "The operation timed out.") \
macro(ConnectionWasClosed, "The connection was closed.") \
macro(OperationFailed, "The operation failed.")
macro(OperationFailed, "The operation failed.") \
macro(GET, "GET") \
macro(PUT, "PUT") \
macro(POST, "POST") \
macro(HEAD, "HEAD") \
macro(COPY, "COPY") \
macro(PATCH, "PATCH") \
macro(MERGE, "MERGE") \
macro(TRACE, "TRACE") \
macro(FETCH, "FETCH") \
macro(PURGE, "PURGE") \
macro(DELETE, "DELETE") \
macro(CONNECT, "CONNECT") \
macro(OPTIONS, "OPTIONS")
// clang-format on

View File

@@ -21,7 +21,7 @@
#include <JavaScriptCore/LazyPropertyInlines.h>
#include <JavaScriptCore/VMTrapsInlines.h>
#include "JSSocketAddressDTO.h"
#include "BunCommonStrings.h"
extern "C" uint64_t uws_res_get_remote_address_info(void* res, const char** dest, int* port, bool* is_ipv6);
namespace Bun {
@@ -508,10 +508,62 @@ static EncodedJSValue assignHeadersFromFetchHeaders(FetchHeaders& impl, JSObject
return JSValue::encode(tuple);
}
static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, MarkedArgumentBuffer& args, JSC::JSGlobalObject* globalObject, JSC::VM& vm)
static JSString* getMethodString(JSC::VM& vm, Zig::GlobalObject* globalObject, Bun::CommonStrings& commonStrings, std::string_view method)
{
switch (method.length()) {
case 3: {
if (method == std::string_view("get", 3)) {
return commonStrings.GETString(globalObject);
}
if (method == std::string_view("put", 3)) {
return commonStrings.PUTString(globalObject);
}
break;
}
case 4: {
if (method == std::string_view("post", 4)) {
return commonStrings.POSTString(globalObject);
}
if (method == std::string_view("head", 4)) {
return commonStrings.HEADString(globalObject);
}
break;
}
case 5: {
if (method == std::string_view("patch", 5)) {
return commonStrings.PATCHString(globalObject);
}
break;
}
case 6: {
if (method == std::string_view("delete", 6)) {
return commonStrings.DELETEString(globalObject);
}
break;
}
case 8: {
if (method == std::string_view("options", 8)) {
return commonStrings.OPTIONSString(globalObject);
}
break;
}
default: {
break;
}
}
std::span<const LChar> data(reinterpret_cast<const LChar*>(method.data()), method.length());
auto str = String::fromUTF8ReplacingInvalidSequences(data);
return jsString(vm, str);
}
static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, MarkedArgumentBuffer& args, Zig::GlobalObject* globalObject, JSC::VM& vm)
{
auto scope = DECLARE_THROW_SCOPE(vm);
std::string_view fullURLStdStr = request->getFullUrl();
auto& commonStrings = globalObject->commonStrings();
String fullURL = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(fullURLStdStr.data()), fullURLStdStr.length() });
// Get the URL.
@@ -522,89 +574,7 @@ static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, Marke
// Get the method.
{
std::string_view methodView = request->getMethod();
WTF::String methodString;
switch (methodView.length()) {
case 3: {
if (methodView == std::string_view("get", 3)) {
methodString = "GET"_s;
break;
}
if (methodView == std::string_view("put", 3)) {
methodString = "PUT"_s;
break;
}
break;
}
case 4: {
if (methodView == std::string_view("post", 4)) {
methodString = "POST"_s;
break;
}
if (methodView == std::string_view("head", 4)) {
methodString = "HEAD"_s;
break;
}
if (methodView == std::string_view("copy", 4)) {
methodString = "COPY"_s;
break;
}
}
case 5: {
if (methodView == std::string_view("patch", 5)) {
methodString = "PATCH"_s;
break;
}
if (methodView == std::string_view("merge", 5)) {
methodString = "MERGE"_s;
break;
}
if (methodView == std::string_view("trace", 5)) {
methodString = "TRACE"_s;
break;
}
if (methodView == std::string_view("fetch", 5)) {
methodString = "FETCH"_s;
break;
}
if (methodView == std::string_view("purge", 5)) {
methodString = "PURGE"_s;
break;
}
break;
}
case 6: {
if (methodView == std::string_view("delete", 6)) {
methodString = "DELETE"_s;
break;
}
break;
}
case 7: {
if (methodView == std::string_view("connect", 7)) {
methodString = "CONNECT"_s;
break;
}
if (methodView == std::string_view("options", 7)) {
methodString = "OPTIONS"_s;
break;
}
break;
}
}
if (methodString.isNull()) {
methodString = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(methodView.data()), methodView.length() });
}
args.append(jsString(vm, methodString));
args.append(getMethodString(vm, globalObject, commonStrings, methodView));
}
size_t size = 0;