Browse Source

buffer: optimize Buffer.prototype.toString('hex')

Move the implementation to C++ land. The old JS implementation used
string concatenation, was dog slow and consumed copious amounts of
memory for large buffers. Example:

  var buf = Buffer(0x1000000);  // 16 MB
  buf.toString('hex')           // Used 3+ GB of memory.

The new implementation operates in O(n) time and space.

Fixes #4700.
v0.9.9-release
Ben Noordhuis 12 years ago
parent
commit
3f65916fa9
  1. 15
      lib/buffer.js
  2. 21
      src/node_buffer.cc
  3. 1
      src/node_buffer.h

15
lib/buffer.js

@ -35,21 +35,6 @@ function toHex(n) {
}
SlowBuffer.prototype.hexSlice = function(start, end) {
var len = this.length;
if (!start || start < 0) start = 0;
if (!end || end < 0 || end > len) end = len;
var out = '';
for (var i = start; i < end; i++) {
out += toHex(this[i]);
}
return out;
};
SlowBuffer.prototype.toString = function(encoding, start, end) {
encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0;

21
src/node_buffer.cc

@ -277,6 +277,26 @@ Handle<Value> Buffer::Ucs2Slice(const Arguments &args) {
}
Handle<Value> Buffer::HexSlice(const Arguments &args) {
HandleScope scope;
Buffer* parent = ObjectWrap::Unwrap<Buffer>(args.This());
SLICE_ARGS(args[0], args[1])
char* src = parent->data_ + start;
uint32_t dstlen = (end - start) * 2;
if (dstlen == 0) return scope.Close(String::Empty(node_isolate));
char* dst = new char[dstlen];
for (uint32_t i = 0, k = 0; k < dstlen; i += 1, k += 2) {
static const char hex[] = "0123456789abcdef";
uint8_t val = static_cast<uint8_t>(src[i]);
dst[k + 0] = hex[val >> 4];
dst[k + 1] = hex[val & 15];
}
Local<String> string = String::New(dst, dstlen);
delete[] dst;
return scope.Close(string);
}
// supports regular and URL-safe base64
static const int unbase64_table[] =
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-1,-1,-2,-1,-1
@ -920,6 +940,7 @@ void Buffer::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", Buffer::AsciiSlice);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Slice", Buffer::Base64Slice);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "ucs2Slice", Buffer::Ucs2Slice);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "hexSlice", Buffer::HexSlice);
// TODO NODE_SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice);
// copy
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Slice", Buffer::Utf8Slice);

1
src/node_buffer.h

@ -118,6 +118,7 @@ class NODE_EXTERN Buffer: public ObjectWrap {
static v8::Handle<v8::Value> Base64Slice(const v8::Arguments &args);
static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
static v8::Handle<v8::Value> Ucs2Slice(const v8::Arguments &args);
static v8::Handle<v8::Value> HexSlice(const v8::Arguments &args);
static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
static v8::Handle<v8::Value> Base64Write(const v8::Arguments &args);
static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);

Loading…
Cancel
Save