Permalink
Browse files

Generalized buffer pool and moved it to util

  • Loading branch information...
dcodeIO committed Dec 6, 2016
1 parent 7c28483 commit 2a2f6dcab5beaaa98e55a005b3d02643c45504d6

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +12 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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
@@ -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;
@@ -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
@@ -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;
@@ -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);
};
@@ -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" {
@@ -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
@@ -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

0 comments on commit 2a2f6dc

Please sign in to comment.