shinesolutions/jazz

Inefficient complete checking

Closed this issue · 4 comments

Suor commented

Instead of checking if all chunks is ready, check it only when one of them is ready - add
if (__jazz_expected == __jazz_seen) __jazz_cb(__jazz_output.join(''));
to
__jazz_emitter.addListener('data', function(pos, chunk) { ... })

What's the use-case here? I'm not sure I see the utility of the callback being passed a position and a chunk, short of a little more flexibility. I mean, what are you going to do with the pos & chunk, short of dumping it all into an array and calling Array.join()?

If we could deliver chunks to the callback in-order, that would be useful.

Suor commented

It has nothing to do with flexibility or anything else except efficiency. I was trying to say that we don't need to check if all chunks is ready on every tick. All parts could be ready only when one of them is ready ('data' event occurs). So, this code:
code += "function __jazz_check_complete() {\n";
code += " if (__jazz_expected == __jazz_seen) __jazz_cb(__jazz_output.join(''));\n";
code += " else process.nextTick(__jazz_check_complete);\n";
code += "}\n";

code += "__jazz_emitter.addListener('data', function(pos, chunk) {\n";
code += "  __jazz_output[pos] = chunk;\n";
code += "  __jazz_seen++;\n";
code += "});\n";

should be replaced with:
code += "var __jazz_ready = false;\n";
code += "function __jazz_check_complete() {\n";
code += " if (__jazz_ready && __jazz_expected == __jazz_seen) __jazz_cb(__jazz_output.join(''));\n";
code += "}\n";

code += "__jazz_emitter.addListener('data', function(pos, chunk) {\n";
code += "  __jazz_output[pos] = chunk;\n";
code += "  __jazz_seen++;\n";
code += "  __jazz_check_complete()\n";
code += "});\n";

And in epilogue instead of
code += "__jazz_check_complete();\n";
should be
code += "__jazz_ready = true;\n";
code += "__jazz_check_complete()\n";

Ah I understand now, great idea. Shall give it a shot when I get a moment.

Thanks for the suggestion!

Implemented in 16563dc...

Thanks Suor!