From f9374eeb778bcb000d6b0a717eba528bd691a51c Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 3 Mar 2009 02:15:26 +0100 Subject: [PATCH] avoid reallocating the same strings every request --- http_api.js | 2 - node_http.cc | 101 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/http_api.js b/http_api.js index 9fb80860f2..aaeb928be7 100644 --- a/http_api.js +++ b/http_api.js @@ -6,8 +6,6 @@ function encode(data) { var server = new HTTP.Server("localhost", 8000); server.onRequest = function (request) { - log( "path: " + request.path ); - log( "query string: " + request.query_string ); // onBody sends null on the last chunk. request.onBody = function (chunk) { diff --git a/node_http.cc b/node_http.cc index aaf85eba66..53eb50e6f3 100644 --- a/node_http.cc +++ b/node_http.cc @@ -14,6 +14,33 @@ using namespace std; static Persistent request_template; static struct ev_loop *loop; +// globals +static Persistent path_str; +static Persistent uri_str; +static Persistent query_string_str; +static Persistent fragment_str; +static Persistent method_str; +static Persistent http_version_str; + +static Persistent on_request_str; +static Persistent on_body_str; +static Persistent respond_str; + +static Persistent copy_str; +static Persistent delete_str; +static Persistent get_str; +static Persistent head_str; +static Persistent lock_str; +static Persistent mkcol_str; +static Persistent move_str; +static Persistent options_str; +static Persistent post_str; +static Persistent propfind_str; +static Persistent proppatch_str; +static Persistent put_str; +static Persistent trace_str; +static Persistent unlock_str; + class Server { public: Server (Handle _js_server); @@ -25,7 +52,7 @@ public: Handle Callback() { HandleScope scope; - Handle value = js_server->Get(String::New("onRequest")); + Handle value = js_server->Get(on_request_str); return scope.Close(value); } @@ -85,7 +112,7 @@ HttpRequest::MakeBodyCallback (const char *base, size_t length) // // XXX don't always allocate onBody strings // - Handle onBody_val = js_object->Get(String::NewSymbol("onBody")); + Handle onBody_val = js_object->Get(on_body_str); if (!onBody_val->IsFunction()) return; Handle onBody = Handle::Cast(onBody_val); @@ -114,20 +141,20 @@ static Handle GetMethodString ) { switch(method) { - case EBB_COPY: return String::New("COPY"); - case EBB_DELETE: return String::New("DELETE"); - case EBB_GET: return String::New("GET"); - case EBB_HEAD: return String::New("HEAD"); - case EBB_LOCK: return String::New("LOCK"); - case EBB_MKCOL: return String::New("MKCOL"); - case EBB_MOVE: return String::New("MOVE"); - case EBB_OPTIONS: return String::New("OPTIONS"); - case EBB_POST: return String::New("POST"); - case EBB_PROPFIND: return String::New("PROPFIND"); - case EBB_PROPPATCH: return String::New("PROPPATCH"); - case EBB_PUT: return String::New("PUT"); - case EBB_TRACE: return String::New("TRACE"); - case EBB_UNLOCK: return String::New("UNLOCK"); + case EBB_COPY: return copy_str; + case EBB_DELETE: return delete_str; + case EBB_GET: return get_str; + case EBB_HEAD: return head_str; + case EBB_LOCK: return lock_str; + case EBB_MKCOL: return mkcol_str; + case EBB_MOVE: return move_str; + case EBB_OPTIONS: return options_str; + case EBB_POST: return post_str; + case EBB_PROPFIND: return propfind_str; + case EBB_PROPPATCH: return proppatch_str; + case EBB_PUT: return put_str; + case EBB_TRACE: return trace_str; + case EBB_UNLOCK: return unlock_str; } return Null(); } @@ -175,7 +202,7 @@ HttpRequest::~HttpRequest () HandleScope scope; // delete a reference to the respond method - js_object->Delete(String::New("respond")); + js_object->Delete(respond_str); js_object.Dispose(); } @@ -267,7 +294,7 @@ static void on_headers_complete if (request_template.IsEmpty()) { Handle raw_template = ObjectTemplate::New(); raw_template->SetInternalFieldCount(1); - raw_template->Set(String::NewSymbol("respond"), FunctionTemplate::New(RespondCallback)); + raw_template->Set(respond_str, FunctionTemplate::New(RespondCallback)); request_template = Persistent::New(raw_template); } @@ -282,23 +309,23 @@ static void on_headers_complete // Store the request pointer in the JavaScript wrapper. result->SetInternalField(0, request_ptr); - result->Set ( String::NewSymbol("path") + result->Set ( path_str , String::New(request->path.c_str(), request->path.length()) ); - result->Set ( String::NewSymbol("uri") + result->Set ( uri_str , String::New(request->uri.c_str(), request->uri.length()) ); - result->Set ( String::NewSymbol("query_string") + result->Set ( query_string_str , String::New(request->query_string.c_str(), request->query_string.length()) ); - result->Set ( String::NewSymbol("fragment") + result->Set ( fragment_str , String::New(request->fragment.c_str(), request->fragment.length()) ); - result->Set ( String::NewSymbol("method") + result->Set ( method_str , GetMethodString(request->parser_info.method) ); @@ -309,7 +336,7 @@ static void on_headers_complete , request->parser_info.version_major , request->parser_info.version_minor ); - result->Set ( String::NewSymbol("http_version") + result->Set ( http_version_str , String::New(version) ); @@ -559,5 +586,31 @@ Handle node_http_initialize (struct ev_loop *_loop) http->Set(String::New("Server"), server_t->GetFunction()); + path_str = Persistent::New( String::NewSymbol("path") ); + uri_str = Persistent::New( String::NewSymbol("uri") ); + query_string_str = Persistent::New( String::NewSymbol("query_string") ); + fragment_str = Persistent::New( String::NewSymbol("fragment") ); + method_str = Persistent::New( String::NewSymbol("method") ); + http_version_str = Persistent::New( String::NewSymbol("http_version") ); + + on_request_str = Persistent::New( String::NewSymbol("onRequest") ); + on_body_str = Persistent::New( String::NewSymbol("onBody") ); + respond_str = Persistent::New( String::NewSymbol("respond") ); + + copy_str = Persistent::New( String::New("COPY") ); + delete_str = Persistent::New( String::New("DELETE") ); + get_str = Persistent::New( String::New("GET") ); + head_str = Persistent::New( String::New("HEAD") ); + lock_str = Persistent::New( String::New("LOCK") ); + mkcol_str = Persistent::New( String::New("MKCOL") ); + move_str = Persistent::New( String::New("MOVE") ); + options_str = Persistent::New( String::New("OPTIONS") ); + post_str = Persistent::New( String::New("POST") ); + propfind_str = Persistent::New( String::New("PROPFIND") ); + proppatch_str = Persistent::New( String::New("PROPPATCH") ); + put_str = Persistent::New( String::New("PUT") ); + trace_str = Persistent::New( String::New("TRACE") ); + unlock_str = Persistent::New( String::New("UNLOCK") ); + return scope.Close(http); }