Skip to content

Commit fe3b035

Browse files
committed
Update fast buffer benchmarks
1 parent 63012bb commit fe3b035

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

benchmark/buffer_creation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
SlowBuffer = require('buffer').SlowBuffer;
12

2-
3-
for (var i = 0; i < 9e7; i++) {
4-
b = new Buffer(10);
3+
for (var i = 0; i < 1e6; i++) {
4+
b = new SlowBuffer(10);
55
b[1] = 2
66
}

benchmark/fast_buffer2.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var SlowBuffer = require('buffer').SlowBuffer;
2+
var POOLSIZE = 8*1024;
3+
var pool;
4+
5+
function allocPool () {
6+
pool = new SlowBuffer(POOLSIZE);
7+
pool.used = 0;
8+
}
9+
10+
function FastBuffer (length) {
11+
this.length = length;
12+
13+
if (length > POOLSIZE) {
14+
// Big buffer, just alloc one.
15+
this.parent = new Buffer(length);
16+
this.offset = 0;
17+
} else {
18+
// Small buffer.
19+
if (!pool || pool.length - pool.used < length) allocPool();
20+
this.parent = pool;
21+
this.offset = pool.used;
22+
pool.used += length;
23+
}
24+
25+
// HERE HERE HERE
26+
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
27+
}
28+
29+
exports.FastBuffer = FastBuffer;
30+
31+
FastBuffer.prototype.get = function (i) {
32+
if (i < 0 || i >= this.length) throw new Error("oob");
33+
return this.parent[this.offset + i];
34+
};
35+
36+
FastBuffer.prototype.set = function (i, v) {
37+
if (i < 0 || i >= this.length) throw new Error("oob");
38+
return this.parent[this.offset + i] = v;
39+
};
40+
41+
// TODO define slice, toString, write, etc.
42+
// slice should not use c++

benchmark/fast_buffer2_creation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
FastBuffer = require('./fast_buffer2').FastBuffer;
3+
for (var i = 0; i < 1e6; i++) {
4+
b = new FastBuffer(10);
5+
b[1] = 2;
6+
}

benchmark/fast_buffer_creation.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for (var i = 0; i < 1e6; i++) {
2+
b = new Buffer(10);
3+
b[1] = 2;
4+
}

0 commit comments

Comments
 (0)