Browse Source

Add sendUtf8 method to socket objects.

Encoding UTF-16 (the native string representation) to UTF-8 is rather
expensive, however just chopping off the second bit to convert UTF-16 to
ASCII is rather fast. I've noticed major performance issues with
String::WriteUtf8 and thus I'm going to explicitly separate in the API.

Still need interfaces to this for the web server.
v0.7.4-release
Ryan 16 years ago
parent
commit
91bd3124ad
  1. 34
      src/net.cc
  2. 1
      src/net.h

34
src/net.cc

@ -56,6 +56,7 @@ Connection::Initialize (v8::Handle<v8::Object> target)
NODE_SET_PROTOTYPE_METHOD(constructor_template, "connect", v8Connect); NODE_SET_PROTOTYPE_METHOD(constructor_template, "connect", v8Connect);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "send", v8Send); NODE_SET_PROTOTYPE_METHOD(constructor_template, "send", v8Send);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "sendUtf8", v8SendUtf8);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", v8Close); NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", v8Close);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", v8FullClose); NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", v8FullClose);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", v8ForceClose); NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", v8ForceClose);
@ -293,6 +294,25 @@ new_buf (size_t size)
return b; return b;
} }
Handle<Value>
Connection::v8SendUtf8 (const Arguments& args)
{
HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
if (!args[0]->IsString())
return ThrowException(String::New("Must have string argument"));
// utf8 encoding
Local<String> s = args[0]->ToString();
size_t length = s->Utf8Length();
oi_buf *buf = new_buf(length);
s->WriteUtf8(buf->base, length);
connection->Send(buf);
return Undefined();
}
Handle<Value> Handle<Value>
Connection::v8Send (const Arguments& args) Connection::v8Send (const Arguments& args)
@ -304,19 +324,17 @@ Connection::v8Send (const Arguments& args)
// XXX // XXX
// A lot of improvement can be made here. First of all we're allocating // A lot of improvement can be made here. First of all we're allocating
// oi_bufs for every send which is clearly inefficent - it should use a // oi_bufs for every send which is clearly inefficent - it should use a
// memory pool or ring buffer. In either case, v8 needs to be informed // memory pool or ring buffer. Of course, expressing binary data as an
// about our allocations deallocations via // array of integers is extremely inefficent. This can improved when v8
// V8::AdjustAmountOfExternalAllocatedMemory to give the GC hints about // bug 270 (http://code.google.com/p/v8/issues/detail?id=270) has been
// what we're doing here. Of course, expressing binary data as an array // addressed.
// of integers is extremely inefficent. This can improved when v8 bug 270
// (http://code.google.com/p/v8/issues/detail?id=270) has been addressed.
if (args[0]->IsString()) { if (args[0]->IsString()) {
// utf8 encoding // ASCII encoding
Local<String> s = args[0]->ToString(); Local<String> s = args[0]->ToString();
size_t length = s->Utf8Length(); size_t length = s->Utf8Length();
oi_buf *buf = new_buf(length); oi_buf *buf = new_buf(length);
s->WriteUtf8(buf->base, length); s->WriteAscii(buf->base, 0, length);
connection->Send(buf); connection->Send(buf);
} else if (args[0]->IsArray()) { } else if (args[0]->IsArray()) {

1
src/net.h

@ -21,6 +21,7 @@ protected:
static v8::Handle<v8::Value> v8New (const v8::Arguments& args); static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Connect (const v8::Arguments& args); static v8::Handle<v8::Value> v8Connect (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Send (const v8::Arguments& args); static v8::Handle<v8::Value> v8Send (const v8::Arguments& args);
static v8::Handle<v8::Value> v8SendUtf8 (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Close (const v8::Arguments& args); static v8::Handle<v8::Value> v8Close (const v8::Arguments& args);
static v8::Handle<v8::Value> v8FullClose (const v8::Arguments& args); static v8::Handle<v8::Value> v8FullClose (const v8::Arguments& args);
static v8::Handle<v8::Value> v8ForceClose (const v8::Arguments& args); static v8::Handle<v8::Value> v8ForceClose (const v8::Arguments& args);

Loading…
Cancel
Save