From: Trevor Norris Date: Fri, 16 Aug 2013 18:32:56 +0000 (-0700) Subject: buffer: don't call ByteLength for simple encodings X-Git-Tag: v0.11.6~22 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2dd4a745b0ec5b5d44ee884d865b5094c63fe39a;p=platform%2Fupstream%2Fnodejs.git buffer: don't call ByteLength for simple encodings For several encodings the byte length is simple arithmetic. Don't call into C++ in those cases. --- diff --git a/lib/buffer.js b/lib/buffer.js index b6dfc52..9bdf7c9 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -25,13 +25,14 @@ var util = require('util'); var alloc = smalloc.alloc; var sliceOnto = smalloc.sliceOnto; var kMaxLength = smalloc.kMaxLength; +var internal = {}; exports.Buffer = Buffer; exports.SlowBuffer = SlowBuffer; exports.INSPECT_MAX_BYTES = 50; // add methods to Buffer prototype -buffer.setupBufferJS(Buffer); +buffer.setupBufferJS(Buffer, internal); Buffer.poolSize = 8 * 1024; var poolSize = Buffer.poolSize; @@ -163,6 +164,31 @@ Buffer.concat = function(list, length) { }; +Buffer.byteLength = function(str, enc) { + var ret; + str = str + ''; + switch (enc) { + case 'ascii': + case 'binary': + case 'raw': + ret = str.length; + break; + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + ret = str.length * 2; + break; + case 'hex': + ret = str.length >>> 1; + break; + default: + ret = internal.byteLength(str, enc); + } + return ret; +} + + // pre-set for values that may exist in the future Buffer.prototype.length = undefined; Buffer.prototype.parent = undefined; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index b69ff63..1a9e128 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -566,9 +566,6 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { Local proto = proto_v.As(); - bv->Set(FIXED_ONE_BYTE_STRING(node_isolate, "byteLength"), - FunctionTemplate::New(ByteLength)->GetFunction()); - NODE_SET_METHOD(proto, "asciiSlice", AsciiSlice); NODE_SET_METHOD(proto, "base64Slice", Base64Slice); NODE_SET_METHOD(proto, "binarySlice", BinarySlice); @@ -600,6 +597,14 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { proto->Set(FIXED_ONE_BYTE_STRING(node_isolate, "offset"), Uint32::New(0, node_isolate), v8::ReadOnly); + + assert(args[1]->IsObject()); + + Local internal = args[1].As(); + + internal->Set(FIXED_ONE_BYTE_STRING(node_isolate, "byteLength"), + FunctionTemplate::New(ByteLength)->GetFunction()); + }