Skip to content

Commit

Permalink
Generalized buffer pool and moved it to util
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 6, 2016
1 parent 7c28483 commit 2a2f6dc
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 128 deletions.
212 changes: 108 additions & 104 deletions dist/protobuf.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ protobuf.inherits = require("./inherits");
// Utility
protobuf.types = require("./types");
protobuf.common = require("./common");
protobuf.pool = require("./pool");
protobuf.util = require("./util");

// Be nice to AMD
Expand Down
13 changes: 8 additions & 5 deletions src/pool.js → src/util/pool.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
"use strict";
module.exports = pool;

/**
* A drop-in buffer pool for Uint8Array, just like node uses for buffers.
* @exports pool
* A drop-in buffer pool, similar in functionality to what node uses for buffers.
* @memberof util
* @function
* @param {function(number):Uint8Array} alloc Allocator
* @param {function(number, number):Uint8Array} slice Slicer
* @param {number} [size=8192] Slab size
* @returns {function(number):Uint8Array} Pooled allocator
*/
module.exports = function create_pool(alloc, size) {
function pool(alloc, slice, size) {
var SIZE = size || 8192;
var MAX = SIZE >>> 1;
var slab = null;
var offset = SIZE;
return function alloc_from_pool(size) {
return function pool_alloc(size) {
if (size > MAX)
return alloc(size);
if (offset + size > SIZE) {
slab = alloc(SIZE);
offset = 0;
}
var buf = slab.subarray(offset, offset += size);
var buf = slice.call(slab, offset, offset += size);
if (offset & 7) // align to 32 bit
offset = (offset | 7) + 1;
return buf;
Expand Down
2 changes: 2 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var util = exports;

var LongBits = util.LongBits = require("./longbits");

util.pool = require("./pool");

/**
* Whether running within node or not.
* @memberof util
Expand Down
4 changes: 2 additions & 2 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Writer.alloc = function alloc(size) {

// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = require("./pool")(Writer.alloc);
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice);

/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
Expand Down Expand Up @@ -563,7 +563,7 @@ function BufferWriter() {
BufferWriter.alloc = function alloc_buffer(size) {
BufferWriter.alloc = util.Buffer.allocUnsafe
? util.Buffer.allocUnsafe
: function alloc_buffer_new(size) { return new util.Buffer(size); };
: function allocUnsafe(size) { return new util.Buffer(size); };
return BufferWriter.alloc(size);
};

Expand Down
23 changes: 12 additions & 11 deletions types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* protobuf.js v6.0.2 TypeScript definitions
* Generated Tue, 06 Dec 2016 16:05:33 UTC
* Generated Tue, 06 Dec 2016 17:00:05 UTC
*/
declare module "protobufjs" {

Expand Down Expand Up @@ -888,16 +888,6 @@ declare module "protobufjs" {
*/
function parse(source: string, root?: Root): ParserResult;

/**
* A drop-in buffer pool for Uint8Array, just like node uses for buffers.
* @exports pool
* @param {function(number):Uint8Array} alloc Allocator
* @param {number} [size=8192] Slab size
* @returns {function(number):Uint8Array} Pooled allocator
*/
module pool {
}

/**
* Options passed to the {@link Prototype|prototype constructor}, modifying its behavior.
* @typedef PrototypeOptions
Expand Down Expand Up @@ -1648,6 +1638,17 @@ declare module "protobufjs" {

}

/**
* A drop-in buffer pool, similar in functionality to what node uses for buffers.
* @memberof util
* @function
* @param {function(number):Uint8Array} alloc Allocator
* @param {function(number, number):Uint8Array} slice Slicer
* @param {number} [size=8192] Slab size
* @returns {function(number):Uint8Array} Pooled allocator
*/
function pool(alloc: any, slice: any, size?: number): any;

/**
* Whether running within node or not.
* @memberof util
Expand Down

0 comments on commit 2a2f6dc

Please sign in to comment.