Skip to content

Commit 2a2f6dc

Browse files
committed
Generalized buffer pool and moved it to util
1 parent 7c28483 commit 2a2f6dc

File tree

10 files changed

+137
-128
lines changed

10 files changed

+137
-128
lines changed

dist/protobuf.js

Lines changed: 108 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

12 Bytes
Binary file not shown.

dist/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ protobuf.inherits = require("./inherits");
5252
// Utility
5353
protobuf.types = require("./types");
5454
protobuf.common = require("./common");
55-
protobuf.pool = require("./pool");
5655
protobuf.util = require("./util");
5756

5857
// Be nice to AMD

src/pool.js renamed to src/util/pool.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
"use strict";
2+
module.exports = pool;
23

34
/**
4-
* A drop-in buffer pool for Uint8Array, just like node uses for buffers.
5-
* @exports pool
5+
* A drop-in buffer pool, similar in functionality to what node uses for buffers.
6+
* @memberof util
7+
* @function
68
* @param {function(number):Uint8Array} alloc Allocator
9+
* @param {function(number, number):Uint8Array} slice Slicer
710
* @param {number} [size=8192] Slab size
811
* @returns {function(number):Uint8Array} Pooled allocator
912
*/
10-
module.exports = function create_pool(alloc, size) {
13+
function pool(alloc, slice, size) {
1114
var SIZE = size || 8192;
1215
var MAX = SIZE >>> 1;
1316
var slab = null;
1417
var offset = SIZE;
15-
return function alloc_from_pool(size) {
18+
return function pool_alloc(size) {
1619
if (size > MAX)
1720
return alloc(size);
1821
if (offset + size > SIZE) {
1922
slab = alloc(SIZE);
2023
offset = 0;
2124
}
22-
var buf = slab.subarray(offset, offset += size);
25+
var buf = slice.call(slab, offset, offset += size);
2326
if (offset & 7) // align to 32 bit
2427
offset = (offset | 7) + 1;
2528
return buf;

src/util/runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var util = exports;
44

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

7+
util.pool = require("./pool");
8+
79
/**
810
* Whether running within node or not.
911
* @memberof util

src/writer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Writer.alloc = function alloc(size) {
146146

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

151151
/** @alias Writer.prototype */
152152
var WriterPrototype = Writer.prototype;
@@ -563,7 +563,7 @@ function BufferWriter() {
563563
BufferWriter.alloc = function alloc_buffer(size) {
564564
BufferWriter.alloc = util.Buffer.allocUnsafe
565565
? util.Buffer.allocUnsafe
566-
: function alloc_buffer_new(size) { return new util.Buffer(size); };
566+
: function allocUnsafe(size) { return new util.Buffer(size); };
567567
return BufferWriter.alloc(size);
568568
};
569569

types/protobuf.js.d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/*
55
* protobuf.js v6.0.2 TypeScript definitions
6-
* Generated Tue, 06 Dec 2016 16:05:33 UTC
6+
* Generated Tue, 06 Dec 2016 17:00:05 UTC
77
*/
88
declare module "protobufjs" {
99

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

891-
/**
892-
* A drop-in buffer pool for Uint8Array, just like node uses for buffers.
893-
* @exports pool
894-
* @param {function(number):Uint8Array} alloc Allocator
895-
* @param {number} [size=8192] Slab size
896-
* @returns {function(number):Uint8Array} Pooled allocator
897-
*/
898-
module pool {
899-
}
900-
901891
/**
902892
* Options passed to the {@link Prototype|prototype constructor}, modifying its behavior.
903893
* @typedef PrototypeOptions
@@ -1648,6 +1638,17 @@ declare module "protobufjs" {
16481638

16491639
}
16501640

1641+
/**
1642+
* A drop-in buffer pool, similar in functionality to what node uses for buffers.
1643+
* @memberof util
1644+
* @function
1645+
* @param {function(number):Uint8Array} alloc Allocator
1646+
* @param {function(number, number):Uint8Array} slice Slicer
1647+
* @param {number} [size=8192] Slab size
1648+
* @returns {function(number):Uint8Array} Pooled allocator
1649+
*/
1650+
function pool(alloc: any, slice: any, size?: number): any;
1651+
16511652
/**
16521653
* Whether running within node or not.
16531654
* @memberof util

0 commit comments

Comments
 (0)