-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: compressor didn't chunkify big payload #3
Changes from 5 commits
0757db6
6e22a09
ab41c6d
ce0a908
5c0a678
d6c2786
0e7e5ef
21c7b4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
/** | ||
* As per the snappy framing format for streams, the size of any uncompressed chunk can be | ||
* no longer than 65536 bytes. | ||
* | ||
* From: https://github.com/google/snappy/blob/main/framing_format.txt#L90:L92 | ||
*/ | ||
const UNCOMPRESSED_CHUNK_SIZE = 65536; | ||
|
||
var Transform = require('stream').Transform | ||
, util = require('util') | ||
|
||
|
@@ -52,18 +60,25 @@ CompressStream.prototype._uncompressed = function (chunk) { | |
|
||
CompressStream.prototype._transform = function (chunk, enc, callback) { | ||
var self = this | ||
async function compressChunks() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why async function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the API of transform is to call callback on the completion of transformation of the chunk, so don't want to holdup transform There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But there is no await in this body, and function compressChunks() { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the callback can error and cause unhandled rejection. Maybe the implementation can look like: new Promise(() => {
...
callback(...);
...
}).catch((e) => logUnexpectedError(e); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, just console logged as nothing fancy available in lib |
||
try { | ||
for (let startFrom = 0; startFrom < chunk.length; startFrom += UNCOMPRESSED_CHUNK_SIZE) { | ||
const endAt = startFrom + Math.min(chunk.length - startFrom, UNCOMPRESSED_CHUNK_SIZE); | ||
const bytesChunk = chunk.slice(startFrom, endAt); | ||
const compressed = snappy.compressSync(bytesChunk) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why switch to the sync version? If we want to do that we should do it in another PR as study in depth the implications There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dapplion the async version where I chunk bytes from outside (like call compressStream.write(chunk)) which uses the original flow, compared to this where chunking happen in the transform using this way is 2X (i.e. compressSync based solution is coming out ahead): If I use async version inside with I think for now this is our best bet 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay sounds good to me. Can you commit a benchmark tho? To inform us of the cost of compressing an uncompressing objects of 10Kb, 100Kb, 1000Kb |
||
if (compressed.length < bytesChunk.length) | ||
self._compressed(bytesChunk, compressed) | ||
else | ||
self._uncompressed(bytesChunk) | ||
|
||
} | ||
callback(); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
} | ||
|
||
snappy.compress(chunk, function (err, compressed) { | ||
if (err) | ||
return callback(err) | ||
|
||
if (compressed.length < chunk.length) | ||
self._compressed(chunk, compressed) | ||
else | ||
self._uncompressed(chunk) | ||
|
||
callback() | ||
}) | ||
compressChunks(); | ||
} | ||
|
||
module.exports = CompressStream | ||
module.exports = CompressStream |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we don't need self anymore now