Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

zlib: add sync versions for convenience methods #6987

Closed
wants to merge 6 commits into from
Closed

zlib: add sync versions for convenience methods #6987

wants to merge 6 commits into from

Conversation

seishun
Copy link

@seishun seishun commented Jan 28, 2014

The user should have an option to run CPU-bound functions synchronously. Zlib is the only case where they don't.

This patch should be considered as a draft or a proof-of-concept. Any directions on how it could be improved will be greatly appreciated.

@Nodejs-Jenkins
Copy link

Thank you for contributing this pull request! Here are a few pointers to make sure your submission will be considered for inclusion.

The following commiters were not found in the CLA:

  • Nikolai Vavilov

You can fix all these things without opening another issue.

Please see CONTRIBUTING.md for more information

Context::Scope context_scope(env->context());

// Acceptable error states depend on the type of zlib stream.
switch (ctx->err_) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to de-dupe it, perhaps a template version of AfterWork could handle it.

@indutny
Copy link
Member

indutny commented Jan 28, 2014

Generally, looks good to me. But needs documentation fixes too.

@seishun
Copy link
Author

seishun commented Jan 29, 2014

Done.

// normal statuses, not fatal
break;
case Z_NEED_DICT:
if (ctx->dictionary_ == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you copied from below, but please remove braces { }

@indutny
Copy link
Member

indutny commented Jan 29, 2014

Yay, almost ready! :) Thank you very much. Please correct last nits and let me know

@seishun
Copy link
Author

seishun commented Jan 29, 2014

Done again!

};

function zlibBuffer(engine, buffer, callback) {
var buffers = [];
var nread = 0;

if (!util.isFunction(callback)) {
if (util.isString(buffer))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but one more thing. Please move it to separate function as well.

@thomseddon
Copy link

Question: fs provides synchronous API's via the *Sync() functions whereas here, synchronicity is implied by omitting callback.

Shouldn't we choose one and unify the API accordingly? And as fs is considered stable, zlib should fall in line by only providing explicit *Sync() function alternatives?

@seishun
Copy link
Author

seishun commented Jan 29, 2014

@thomseddon On the other hand, crypto provides synchronous functions where you omit the callback. Since crypto is CPU-bound and fs is I/O-bound, the former falls in the same category as zlib so that's what I think we should be consistent with.

@indutny
Copy link
Member

indutny commented Jan 29, 2014

@thomseddon I agree with @seishun

@thomseddon
Copy link

@seishun Good point well made :)

@seishun
Copy link
Author

seishun commented Jan 30, 2014

Okay I know this is crazy but I couldn't think of a better way to de-dupe this.

@indutny
Copy link
Member

indutny commented Jan 30, 2014

What?

@seishun
Copy link
Author

seishun commented Jan 30, 2014

I'm not sure I understand the question. I was referring to my last commit. I didn't know how the core devs would feel about the callback approach in de-duping the recursive write but the idea seemed interesting to me so I did it anyway. Also it's 2:30 AM here so I'll probably refrain from writing anything else for today.

@indutny
Copy link
Member

indutny commented Jan 30, 2014

Ah, you was referring to your last commit

});

engine._processChunk(buffer, function(binding, args, callback) {
callback.apply(null, binding.writeSync.apply(binding, args));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good approach! But let's try to avoid calling apply, where possible.

And additional advice, you could pass true as an argument for async write and false for a synchronous write and call writeSync/write in the _processChunk.


engine._processChunk(buffer, function(binding, args, callback) {
callback.apply(null, binding.writeSync.apply(binding, args));
}, function(out) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As another idea, you could use passed flag to determine the desirable behavior instead of passing a callbacks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that you should not pass callback at all.

@indutny
Copy link
Member

indutny commented Jan 30, 2014

I like where it is going! So far no more comments, will continue after you'll fix it!

@seishun
Copy link
Author

seishun commented Jan 31, 2014

Done! I figured that a loop would be a better fit for the synchronous version than recursion.

@@ -480,7 +531,12 @@ Zlib.prototype._transform = function(chunk, encoding, cb) {
var out = self._buffer.slice(self._offset, self._offset + have);
self._offset += have;
// serve some output to the consumer.
self.push(out);
if (async)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces here, when else has braces - if should have them too.

@indutny
Copy link
Member

indutny commented Jan 31, 2014

Actually, I think I could fix all that stuff myself... please wait a minute

@indutny
Copy link
Member

indutny commented Jan 31, 2014

LGTM

@indutny
Copy link
Member

indutny commented Jan 31, 2014

Thank you very much for all your efforts, landed in 9b37b83

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants