Permalink
Browse files

buffer: zero-length buffers shouldn't be slab-backed

  • Loading branch information...
1 parent 872cb0d commit 5c94ae11a20a5192e73239e50e5edbbb33e1dc80 @piscisaureus piscisaureus committed Jan 5, 2013
Showing with 8 additions and 2 deletions.
  1. +8 −2 lib/buffer.js
View
@@ -199,8 +199,9 @@ function coerce(length) {
}
-// Buffer
+var zeroBuffer = new SlowBuffer(0);
bnoordhuis
bnoordhuis Jan 14, 2013

Maybe lazily allocate this?

piscisaureus
piscisaureus Jan 14, 2013

Yeah I considered that but it is fairly small and all that. If you care much I will do a followup patch (this one landed already).

bnoordhuis
bnoordhuis Jan 14, 2013

I'll leave it up to you, it's a really minor thing.

+// Buffer
function Buffer(subject, encoding, offset) {
if (!(this instanceof Buffer)) {
return new Buffer(subject, encoding, offset);
@@ -242,13 +243,18 @@ function Buffer(subject, encoding, offset) {
this.parent = new SlowBuffer(this.length);
this.offset = 0;
- } else {
+ } else if (this.length > 0) {
// Small buffer.
if (!pool || pool.length - pool.used < this.length) allocPool();
this.parent = pool;
this.offset = pool.used;
pool.used += this.length;
if (pool.used & 7) pool.used = (pool.used + 8) & ~7;
+
+ } else {
+ // Zero-length buffer
+ this.parent = zeroBuffer;
+ this.offset = 0;
}
// Treat array-ish objects as a byte array.

0 comments on commit 5c94ae1

Please sign in to comment.