Skip to content
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

Review #6

Closed
wants to merge 2 commits into from
Closed

Review #6

wants to merge 2 commits into from

Conversation

Yone-e
Copy link

@Yone-e Yone-e commented Jun 2, 2018

No description provided.

* implementing 'Writable' which has an Array of Buffers as an internal
storage structure
…ich has an Buffer as an internal storage structure
Copy link
Member

@SemenchenkoVitaliy SemenchenkoVitaliy left a comment

Choose a reason for hiding this comment

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

Use eslint


let ret =
state.length < state.highWaterMark &&
state.bufferOffset + Buffer.from(chunk).length < state.internalBuffer.length;
Copy link
Member

@SemenchenkoVitaliy SemenchenkoVitaliy Aug 4, 2018

Choose a reason for hiding this comment

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

  • make separate variable for Buffer.from(chunk) to avoid unnecessary memory allocation and copying
  • use state.highWaterMark instead of state.internalBuffer.length because they have the same value and in the 1st case you have less nesting

@tshemsedinov
Copy link
Member

@SemenchenkoVitaliy please compare with @machendos implementation and choose better one

@tshemsedinov tshemsedinov reopened this Aug 4, 2018
@SemenchenkoVitaliy
Copy link
Member

If @Yone-e will fix everything that I said and add timer, i would consider it as a better one. @machendos implements stream.Writable interface only partially

@tshemsedinov
Copy link
Member

Also note that CI fails: https://travis-ci.org/metarhia/metastreams/jobs/412174568

/home/travis/build/metarhia/metastreams/lib/array-based/writable.js
   19:33  error  'onwrite' was used before it was defined           no-use-before-define
   48:5   error  'writeAfterEnd' was used before it was defined     no-use-before-define
   54:12  error  'writeOrBuffer' was used before it was defined     no-use-before-define
   81:5   error  'endWritable' was used before it was defined       no-use-before-define
  105:7   error  'clearBuffer' was used before it was defined       no-use-before-define
  139:2   error  Unnecessary semicolon                              no-extra-semi
  143:22  error  'decodeChunk' was used before it was defined       no-use-before-define
  155:7   error  'ret' is never reassigned. Use 'const' instead     prefer-const
  162:1   error  Line 162 exceeds the maximum line length of 80     max-len
  179:5   error  'doWrite' was used before it was defined           no-use-before-define
  214:5   error  'onwriteError' was used before it was defined      no-use-before-define
  216:22  error  'needFinish' was used before it was defined        no-use-before-define
  226:7   error  'clearBuffer' was used before it was defined       no-use-before-define
  230:24  error  'afterWrite' was used before it was defined        no-use-before-define
  232:7   error  'afterWrite' was used before it was defined        no-use-before-define
  242:22  error  'finishMaybe' was used before it was defined       no-use-before-define
  249:5   error  'finishMaybe' was used before it was defined       no-use-before-define
  258:2   error  Unnecessary semicolon                              no-extra-semi
  262:5   error  'onwriteDrain' was used before it was defined      no-use-before-define
  267:3   error  'finishMaybe' was used before it was defined       no-use-before-define
  280:1   error  Line 280 exceeds the maximum line length of 80     max-len
  312:8   error  Unnecessary semicolon                              no-extra-semi
  338:5   error  'finishMaybe' was used before it was defined       no-use-before-define
  376:5   error  Closing curly brace does not appear on the same line
as the subsequent block brace-style
/home/travis/build/metarhia/metastreams/lib/buffer-based/writable.js
   16:1   error  Line 16 exceeds the maximum line length of 80      max-len
   20:33  error  'onwrite' was used before it was defined           no-use-before-define
   50:5   error  'writeAfterEnd' was used before it was defined     no-use-before-define
   56:12  error  'writeOrBuffer' was used before it was defined     no-use-before-define
   83:5   error  'endWritable' was used before it was defined       no-use-before-define
  105:7   error  'clearBuffer' was used before it was defined       no-use-before-define
  132:1   error  Line 132 exceeds the maximum line length of 80     max-len
  135:1   error  Line 135 exceeds the maximum line length of 80     max-len
  144:2   error  Unnecessary semicolon                              no-extra-semi
  148:22  error  'decodeChunk' was used before it was defined       no-use-before-define
  160:7   error  'ret' is never reassigned. Use 'const' instead     prefer-const
  162:1   error  Line 162 exceeds the maximum line length of 80     max-len
  171:9   error  'doWrite' was used before it was defined           no-use-before-define
  187:7   error  'doWrite' was used before it was defined           no-use-before-define
  189:1   error  Line 189 exceeds the maximum line length of 80     max-len
  190:7   error  'doWrite' was used before it was defined           no-use-before-define
  214:5   error  'doWrite' was used before it was defined           no-use-before-define
  249:5   error  'onwriteError' was used before it was defined      no-use-before-define
  251:22  error  'needFinish' was used before it was defined        no-use-before-define
  259:7   error  'clearBuffer' was used before it was defined       no-use-before-define
  263:24  error  'afterWrite' was used before it was defined        no-use-before-define
  265:7   error  'afterWrite' was used before it was defined        no-use-before-define
  275:22  error  'finishMaybe' was used before it was defined       no-use-before-define
  282:5   error  'finishMaybe' was used before it was defined       no-use-before-define
  291:2   error  Unnecessary semicolon                              no-extra-semi
  295:5   error  'onwriteDrain' was used before it was defined      no-use-before-define
  300:3   error  'finishMaybe' was used before it was defined       no-use-before-define
  354:5   error  'finishMaybe' was used before it was defined       no-use-before-define
  392:5   error  Closing curly brace does not appear on the same line
as the subsequent block brace-style
✖ 53 problems (53 errors, 0 warnings)
  9 errors, 0 warnings potentially fixable with the `--fix` option.

Copy link
Member

@machendos machendos left a comment

Choose a reason for hiding this comment

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

Check the code with eslint

this._writableState.bufferedArray = null;
this._writableState.bufferedSize = 0;

this._writableState.onwrite = onwrite.bind(undefined, this);
Copy link
Member

Choose a reason for hiding this comment

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

would be better onwrite.bind(null, this);

state.sync = false;
};

const onwrite = (stream, error) => {
Copy link
Member

Choose a reason for hiding this comment

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

It is necessary to change all lambda functions to a functional declaration, or to raise lambdas to the top, since they are called above

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer reorder and use lambdas in all cases where this not needed

const length = state.objectMode ? 1 : chunk.length;
state.length += length;

let ret = state.length < state.highWaterMark;
Copy link
Member

Choose a reason for hiding this comment

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

const instead of let

delete this._writableState.lastBufferedRequest;
delete this._writableState.corkedRequestsFree;

this._writableState.internalBuffer = Buffer.alloc(this._writableState.highWaterMark);
Copy link
Member

Choose a reason for hiding this comment

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

We can use here Buffer.allocUnsafe

const callback = state.writecb;

if (typeof callback !== 'function') {
throw new Error('Callback called multiple times');
Copy link
Member

Choose a reason for hiding this comment

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

@tshemsedinov Maybe we should not throw mistakes like this. For example, we can take all the error messages in constants, or at least create variables with them somewhere, and then turn to them? Or is it normal?

Copy link
Member

Choose a reason for hiding this comment

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

For this certain purpose we have callback = common.once(callback) in https://github.com/metarhia/common I do not think we should throw in such cases

@lundibundi
Copy link
Member

fyi @machendos @SemenchenkoVitaliy this implementation is a somewhat reworked nodejs Writable stream implementation https://github.com/nodejs/node/blob/master/lib/_stream_writable.js.

I believe it's not a good idea to inherit all of that, there is a lot of legacy and even nodejs members want to implement a new stream api that will be much more performant and clean (see links).
On the other hand are there any benchmarks of this, is it event important (will it bring any benefits) to have this in our repo instead of just using plain Writable?

Links:
https://github.com/nodejs/node/projects/7
openjs-foundation/summit#82
https://github.com/Fishrock123/bob

@SemenchenkoVitaliy
Copy link
Member

Now that I've run speed tests, I see that implementation of @Yone-e is about 1000% slower, maybe due to V8 optimizations of internal streams.

@machendos
Copy link
Member

The goal of this repository, as I understand it, was to rewrite the usual Writable streams to something faster working, and eventually offer our work in the node. But for now, I dont like this decision very much. I agree with @lundibundi that it's just a little overwritten Writable stream from the node. And I also agree with that we shouldnt inherit this decision. Perhaps someone should address this issue in more detail

@lundibundi
Copy link
Member

Closing for now due to inactivity. Also refs #6 (comment).

@lundibundi lundibundi closed this Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants