Skip to content

Suggestion: stream.read(buffer, offset, length, callback) #3403

Closed

Description

@jorangreef

I am not sure if this is possible with the design of libuv and Node, but it would be great to be able to do:

stream.read(buffer, offset, length, callback)

buffer is the buffer that the data will be written to.

offset is the offset in the buffer to start writing at.

length is an integer specifying the number of bytes to read.

Especially for net and tls sockets.

I am not sure if Node itself currently manages and re-uses the underlying buffers for net sockets? But if not, then this would make it possible to do static memory allocation in a server. In the past, streams did not support pull mode at all, now they support pull mode, but the user still has no control over the allocation of the underlying buffers, so this creates more allocations and more work for the GC.

I am working on a client-side program at the moment and am finding that V8's GC is just too lazy. Things work and eventually get collected but memory usage climbs and climbs. I would like to have more control and rather statically allocate buffers and re-use them. This would also have a positive side-effect of controlling queue sizes and workloads in various parts of the program more explicitly.

Activity

  1. added
    streamIssues and PRs related to Node.js streams.
    feature requestIssues requesting new Node.js features.
    on Oct 16, 2015
  2. Fishrock123 commented on Oct 20, 2015

    @Fishrock123
    Contributor

    cc @nodejs/streams

  3. chrisdickinson commented on Oct 20, 2015

    @chrisdickinson
    Contributor

    Something like this would be cool. WHATWG currently has "getByobReader", which implements something similar. I would advise we watch how that pans out closely to see if there are any rough edges we should know about before implementing such a thing ourselves.

    On Oct 20, 2015, at 6:08 AM, Jeremiah Senkpiel notifications@github.com wrote:

    cc @nodejs/streams

    —
    Reply to this email directly or view it on GitHub.

  4. bnoordhuis commented on Oct 20, 2015

    @bnoordhuis
    Member

    Apropos nothing:

    I am not sure if this is possible with the design of libuv

    Request-driven reading is not the model that libuv currently uses but it can be modeled reasonably efficiently by selectively calling uv_read_start() and uv_read_stop().

    That's what libuv's socks5-proxy example does in fact and it's the most natural model for most applications. The plan is to implement it natively in libuv v2.x.

  5. Trott commented on May 26, 2016

    @Trott
    Member

    Anything new to add to this issue in the past 7 months?

  6. jhamhader commented on May 26, 2016

    @jhamhader
    Contributor

    Looking into it.
    Need to think on how this API makes sense in the case of objectMode or stringMode (encoding set) on the readable stream.

  7. vkurchatkin commented on May 26, 2016

    @vkurchatkin
    Contributor
  8. Trott commented on Jul 7, 2017

    @Trott
    Member

    This issue has been inactive for sufficiently long that it seems like perhaps it should be closed. Feel free to re-open (or leave a comment requesting that it be re-opened) if you disagree. I'm just tidying up and not acting on a super-strong opinion or anything like that.

  9. jorangreef commented on Jul 21, 2017

    @jorangreef
    ContributorAuthor

    This issue is blocked on libuv v2.x and nodejs/node-eps#27 which may take some time.

    Can we re-open?

    We definitely need a way to do more static allocation in Node specifically when working with TCP.

  10. added
    blockedPRs that are blocked by other issues or PRs.
    on Jul 21, 2017
  11. refack commented on Nov 11, 2018

    @refack
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedPRs that are blocked by other issues or PRs.feature requestIssues requesting new Node.js features.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions