镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Duplex seems to create unnecessary copies of Buffer data #162

Description

@jamestalmage

The assertion below fails in iojs >= 3.0.0, and Node >= 4.0.0. It passes in every earlier version I checked (0.8.28 to 2.5.0).

var stream = require('readable-stream');

var duplex = new stream.Duplex({
    write: function(chunk, enc, cb) {
        this.push(chunk);
        cb();
    },
    read: function() {}
});

var bufA = new Buffer('a');

var writable = new stream.Writable({
    write: function (chunk, enc, cb) {
        assert.strictEqual(chunk, bufA, 'should be the same instance');
    }
});

duplex.pipe(writable);

duplex.write(bufA);

It can be made to pass by setting readableObjectMode: true, but it is unclear to me why that would be necessary (or the implications it would have).

Activity

  1. yoshuawuyts commented on Oct 9, 2015

    @yoshuawuyts

    @jamestalmage did you check if this is also the case for readable streams?

  2. calvinmetcalf commented on Oct 9, 2015

    @calvinmetcalf
    Contributor

    there is no guarantee of buffer equality for streams not in object mode, the buffers can be combined and they can be split. If it's just the possible perf issues then we should look to see where the copy is happening.

  3. jamestalmage commented on Oct 9, 2015

    @jamestalmage
    Author

    This does not happen with readable streams.

    My main concern here is the performance impact.

    objectMode:true seems undesirable since it seems it will have implications on buffering/backpressure.

    there is no guarantee of buffer equality for streams not in object mode, the buffers can be combined and they can be split

    Just for my understanding: Why is that ever necessary in the base streams implementation? It seems to me that the goal would be to never copy data unnecessarily, and I am having a hard time thinking of a reason why a base stream implementation would ever need to.

  4. calvinmetcalf commented on Oct 9, 2015

    @calvinmetcalf
    Contributor

    figured out the reason for this nodejs/node#3300

  5. jamestalmage commented on Oct 9, 2015

    @jamestalmage
    Author

    This does not happen with readable streams.

    Oops! I obviously recalled incorrectly (probably got confused with my testing on node 12.X). Sorry. And thanks for the fix!

  6. calvinmetcalf commented on Nov 1, 2015

    @calvinmetcalf
    Contributor

    fixed and is in both node and readable-streams

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions