FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

stream: add map method to Readable: by benjamingr · Pull Request #40815 · nodejs/node · GitHub

/ node Public

stream: add map method to Readable: - #40815

Closed
benjamingr wants to merge 19 commits into
nodejs:masterfrom
benjamingr:add-map-to-readable-stream
Closed

stream: add map method to Readable:#40815
benjamingr wants to merge 19 commits into
nodejs:masterfrom
benjamingr:add-map-to-readable-stream

Conversation

Copy link
Copy Markdown
Member

This is very much work in progress when meeting @ronag at NodeTLV.

This is based on the ongoing standards work in https://github.com/tc39/proposal-iterator-helpers in order to make Node.js more compatible with the language in the future.

In addition, people often really just want to map a stream and this provides a simpler API than compose or pipeline for simple cases.

This is still missing docs, I want to bikeshed the API and see we have consensus first.

nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Nov 15, 2021

Copy link
Copy Markdown
Member Author

This is still missing:

  • docs (I want this to be experimental, but this bit is straightforward).
  • consensus regarding whether it should be an instance methods (I like better) or static (may be more compatible possibly).
  • consensus regarding its inclusion.

In terms of API - I think this is what people are usually looking for when transforming a stream.

Also cc @mcollina @nodejs/streams to make sure this isn't "under the radar".

Copy link
Copy Markdown
Member

I think it's better to lift the implementation of mapIterator in https://github.com/mcollina/hwp/blob/main/index.js where also @ronag contributed.

Copy link
Copy Markdown
Member Author

I don't understand the need for the double queueing here but I am happy to adjust the implementation. The actual API sounds good?

mcollina commented Nov 15, 2021
edited
Loading

Copy link
Copy Markdown
Member

I don't understand the need for the double queueing here but I am happy to adjust the implementation. The actual API sounds good?

The implementation in this PR processes chunks one by one, however this is problematic given it's an async function. If we do not support some level of parallel processing it's not going to be really useful because it would be too slow.

ronag commented Nov 15, 2021

Copy link
Copy Markdown
Member

Should we use hwp map?

Copy link
Copy Markdown
Member Author

The implementation in this PR processes chunks one by one, however this is problematic given it's an async function. If we do not support some level of parallel processing it's not going to be really useful because it would be too slow.

That sounds reasonable. So we agree on the API itself but it would be better if it mapd concurrently.

I'm fine with hwp map as that implementation (can adjust the PR + add tests) :)

I think we should slightly adjust the behavior:

  • Return a stream and not an async iterator (or results array)
  • Name the hwm parameter something more indicative than n (like concurrency or maxConcurrency)?

WDYT?

Comment thread lib/internal/streams/operators.js Outdated

jasnell commented Nov 16, 2021

Copy link
Copy Markdown
Member

That sounds reasonable. So we agree on the API itself but it would be better if it mapd concurrently.

I think concurrency should be an option, not necessarily the default given that it would introduce non-deterministic ordering of the results.

Options that would appear to make immediate sense here include:

  • concurrency
  • signal (AbortSignal)
const ac = new AbortController();
Readable.from([1, 2, 3, 4, 5]).map((x) => x + x, { concurrency: 5, signal: ac.signal });

Copy link
Copy Markdown
Member

I think concurrency should be an option, not necessarily the default given that it would introduce non-deterministic ordering of the results.

That is incorrect. The implementation I recommended keep the ordering.

ronag commented Nov 17, 2021

Copy link
Copy Markdown
Member

@benjamingr I did some work on the PR. I hope you don't mind.

  • Added concurrency and highWaterMark options
  • Output is in order
  • Bypasses a lot of intermediate buffering by creating a Readable directly

ronag force-pushed the add-map-to-readable-stream branch 4 times, most recently from 19e5c4b to da16246 Compare November 17, 2021 08:12
Comment thread lib/internal/streams/operators.js Outdated
Comment thread lib/internal/streams/operators.js Outdated
Comment thread lib/internal/streams/operators.js Outdated
Comment thread lib/internal/streams/operators.js Outdated
Comment thread lib/internal/streams/operators.js Outdated

Copy link
Copy Markdown
Member Author

@ronag I don't, feel free, I am still in the conference aftermath here ^^

I'll try and push some docs :)

ronag force-pushed the add-map-to-readable-stream branch 2 times, most recently from ee87587 to db002da Compare November 17, 2021 08:28
Comment thread lib/internal/streams/operators.js Outdated

ronag Nov 17, 2021
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
}
}
stream.destroy(err);

Should destroy back propagate to source stream?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@devsnek does .return propagate in proposal-iterator-helpers?

That is, in that proposal:

const iter = getIterator();
const mapped = iter.map(x => x * 2);
mapped.return(); // is `.return` called on iter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@ronag did you resolve because you checked with that? Because I wanna make sure we align

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I resolved it because I added a destroy. It was before I saw your comment.

ronag force-pushed the add-map-to-readable-stream branch 4 times, most recently from debd387 to 27999bf Compare November 17, 2021 08:41
Comment thread lib/internal/streams/operators.js Outdated
ronag and others added 4 commits December 27, 2021 08:59
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

Copy link
Copy Markdown
Collaborator

mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM with a question

Comment thread lib/stream.js Outdated
Comment thread lib/stream.js Outdated

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

lgtm

ronag commented Dec 29, 2021

Copy link
Copy Markdown
Member

Landed in b97b81d

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.


Back | FazBrowse Home | New Git URL