Skip to content

Commit

Permalink
Restore the "chunked" methodology in getMeta
Browse files Browse the repository at this point in the history
* Rename `aChunks` to `aBufs` to match *node* docs and indicate that is what we are expecting
* `.concat` is vague on `totalLength` parameter but in tests with *node* it is `String` length e.g. `1` returns one character currently ... added Watchpoint here
* Remove the debug messages for now
* Tested on ~112.9KiB User Script... took ~10ms-11ms versus 7ms in debug mode on full Buffers conversion... slower **however** less memory intensive in some use cases especially with larger scripts

Applies to OpenUserJS#678

Refs:
* https://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength
  • Loading branch information
Martii committed Oct 6, 2015
1 parent 5d0788e commit 9a09467
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,55 +326,53 @@ function parseMeta(aParser, aString) {
}
exports.parseMeta = parseMeta;

exports.getMeta = function (aChunks, aCallback) {
exports.getMeta = function (aBufs, aCallback) {
// We need to convert the array of buffers to a string to
// parse the blocks. But strings are memory inefficient compared
// to buffers so we only convert the least number of chunks to
// get the metadata blocks.
var i = 0;
var len = 0;
var str = null;
var parser = null;
var rHeaderContent = null;
var headerContent = null;
var hasUserScriptHeaderContent = false;
var blocksContent = {};
var blocks = {};

if (isDbg) {
console.log('> getMeta() > aChunks.length');
console.log(aChunks.length);
}
for (; i < aBufs.length; ++i) {
// Convert the current Buffer to a `String` and accumulate it's `String` totalLength
len += aBufs[i].toString('utf8').length; // NOTE: Watchpoint

var buf = Buffer.concat(aChunks);
var str = buf.toString('utf8');
// Read from the start of the Buffers to the `String` length end-point
// See also #678
str = Buffer.concat(aBufs, len).toString('utf8');

if (isDbg) {
console.log('> getMeta() > str');
console.log(str);
}
for (parser in parsers) {
rHeaderContent = new RegExp(
'^(?:\\uFEFF)?\/\/ ==' + parser + '==([\\s\\S]*?)^\/\/ ==\/'+ parser + '==', 'm'
);
headerContent = rHeaderContent.exec(str);
if (headerContent && headerContent[1]) {
if (parser === 'UserScript') {
hasUserScriptHeaderContent = true;
}

for (parser in parsers) {
rHeaderContent = new RegExp(
'^(?:\\uFEFF)?\/\/ ==' + parser + '==([\\s\\S]*?)^\/\/ ==\/'+ parser + '==', 'm'
);
headerContent = rHeaderContent.exec(str);
if (headerContent && headerContent[1]) {
if (parser === 'UserScript') {
hasUserScriptHeaderContent = true;
blocksContent[parser] = headerContent[1];
}

blocksContent[parser] = headerContent[1];
}
}

if (hasUserScriptHeaderContent) {
for (parser in parsers) {
if (blocksContent[parser]) {
blocks[parser] = parseMeta(parsers[parser], blocksContent[parser]);
if (hasUserScriptHeaderContent) {
for (parser in parsers) {
if (blocksContent[parser]) {
blocks[parser] = parseMeta(parsers[parser], blocksContent[parser]);
}
}
return aCallback(blocks);
}
return aCallback(blocks);
}


aCallback(null);
};

Expand Down

0 comments on commit 9a09467

Please sign in to comment.