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

buffer: add Buffer compare by offset #5880

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Mar 23, 2016

Pull Request check-list

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to [CONTRIBUTING.md][0]?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?
  • Is a documentation update included (if this change modifies
    existing APIs, or introduces new ones)?

Affected core subsystem(s)

buffer

Description of change

Adds additional offset, length and thisOffset arguments
to Buffer.prototype.compare to allow comparison of sub-ranges
of two Buffers without requiring Buffer.prototype.slice()

Fixes: #521
/cc @trevnorris @rootslab @jorangreef

@jasnell jasnell added buffer Issues and PRs related to the buffer subsystem. semver-minor PRs that contain new features and should be released in the next minor version. labels Mar 23, 2016
@@ -670,9 +670,15 @@ console.log(buf.toString('ascii'));
// Prints: Node.js
```

### buf.compare(otherBuffer)
### buf.compare(otherBuffer[, offset[, length[, thisOffset]]])
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps the order should be tweaked a bit here and have length and thisOffset swapped to match more closely with buffer.copy() which also deals with two buffers?

I also think offset might be better renamed to something like otherOffset.

Copy link
Contributor

Choose a reason for hiding this comment

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

Possibly follow suit of copy() since it follows similar parameters.

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

@trevnorris @mscdex ... updated! PTAL

The signature has been changed to: buf.compare(other[, otherStart[, thisStart[, otherEnd[, thisEnd]]]])

@mscdex
Copy link
Contributor

mscdex commented Mar 24, 2016

Couldn't we just have one end value, thisEnd, or just keep length instead of the end parameters? If not, I would prefer seeing buf.compare(other[, otherStart[, otherEnd[, thisStart[, thisEnd]]]]).

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

Personally I'd prefer to keep length. I changed it to this to align with the signature for buf.copy()

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

That said, having it as just length limits you to always comparing equal length ranges which is not always desirable.

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

@mscdex ... updated ... I swapped the order of the arguments but kept separate otherEnd and thisEnd.

The signature has been changed to: buf.compare(other[, otherStart[, otherEnd[, thisStart[, thisEnd]]]])

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

Currently, thisEnd defaults to this.byteLength, however, we could make it so that if thisEnd is not specified, it defaults to Math.min(this.byteLength, thisStart + (otherEnd - otherStart)))

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

doing so causes surprising behavior tho:

const a = Buffer.from([1,2,3,4,5,6,7,8,9]);
const b = Buffer.from([5,6,7,8,9,1,2,3,4]);
console.log(a.compare(b, 5, 9));
  // Prints 0

@mscdex
Copy link
Contributor

mscdex commented Mar 24, 2016

I don't think it's that much of a surprising behavior considering that the default for sourceStart for .copy() is also 0.

@jasnell
Copy link
Member Author

jasnell commented Mar 24, 2016

the surprising behavior is that the subset of b is not compared to the full content of a. If I did Buffer.compare(a, b.slice(5, 9)) I would get a different result.

default = `otherBuffer.byteLength`.
* `thisStart` {Integer} The offset within `buf` at which to begin comparison.
Ignored when `otherStart` is undefined. default = `0`
* `thisEnd` {Integer} The offset within `buf` at which to end comparison.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use source/target to mirror copy()? Think it will have less mental impact.

Copy link
Member Author

Choose a reason for hiding this comment

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

we could but source/target make a lot more sense in copy than they do in compare.

@trevnorris
Copy link
Contributor

@jasnell One documentation comment, but implementation LGTM.

@jasnell jasnell force-pushed the buffer-compare-offset branch from 904cdf7 to 11f429f Compare March 26, 2016 04:07
@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2016

@jasnell
Copy link
Member Author

jasnell commented Mar 26, 2016

@mscdex ... any further thoughts on this one?


switch (conf.method) {
case 'slice':
bench.start();
Copy link
Contributor

Choose a reason for hiding this comment

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

What about moving the bench.start()/bench.end() to be closer to the actual loop in the respective functions?

@mscdex
Copy link
Contributor

mscdex commented Mar 26, 2016

@jasnell Just a few more comments/questions.

@jasnell jasnell force-pushed the buffer-compare-offset branch from 11f429f to cd114a2 Compare March 27, 2016 20:49
@jasnell
Copy link
Member Author

jasnell commented Mar 27, 2016

@trevnorris @mscdex ... updated and rebased. PTAL!

@jasnell
Copy link
Member Author

jasnell commented Apr 7, 2016

@trevnorris ... PTAL!

else if (source_start >= source_end)
return args.GetReturnValue().Set(-1);
else if (target_start >= target_end)
return args.GetReturnValue().Set(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

These should have been handled in JS. Instead can they all be turned into CHECKs? e.g.

CHECK(target_start >= target_end && source_start >= source_end);
CHECK_GE(source_start >= source_end);
CHECK_GE(target_start >= target_end);

Since if one of these is hit then it would indicate a bug on the JS side.

@trevnorris
Copy link
Contributor

One comment, but LGTM otherwise.

Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: nodejs#521
@jasnell jasnell force-pushed the buffer-compare-offset branch from 168f7ad to 1e6a4c4 Compare April 9, 2016 00:28
@jasnell
Copy link
Member Author

jasnell commented Apr 9, 2016

@trevnorris ... updated! squashed the commits. New CI: https://ci.nodejs.org/job/node-test-pull-request/2227/

@trevnorris
Copy link
Contributor

If CI is happy LGTM

@jasnell
Copy link
Member Author

jasnell commented Apr 9, 2016

Failures in CI are unrelated.

jasnell added a commit that referenced this pull request Apr 9, 2016
Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: #521
PR-URL: #5880
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@jasnell
Copy link
Member Author

jasnell commented Apr 9, 2016

Landed in a246689. Thank you!

@jasnell jasnell closed this Apr 9, 2016
@jorangreef
Copy link
Contributor

Thanks @jasnell and @trevnorris.

MylesBorins pushed a commit that referenced this pull request Apr 20, 2016
Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: #521
PR-URL: #5880
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 21, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) #5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) #6279
  * update ESLint to 2.7.0
    (silverwind) #6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) #6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) #6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) #6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behaviour was in node
    previously and was not removed intentionally.
    (Rich Trott) #6090

src:
  * add SIGINFO to supported signals
    (James Reggio) #6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) #6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) #6069
This was referenced Apr 21, 2016
MylesBorins pushed a commit that referenced this pull request Apr 21, 2016
Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: #521
PR-URL: #5880
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 21, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) #5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) #6279
  * update ESLint to 2.7.0
    (silverwind) #6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) #6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) #6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) #6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behavior was in node
    previously and was not removed intentionally.
    (Rich Trott) #6090

src:
  * add SIGINFO to supported signals
    (James Reggio) #6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) #6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) #6069
MylesBorins pushed a commit that referenced this pull request Apr 21, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) #5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) #6279
  * update ESLint to 2.7.0
    (silverwind) #6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) #6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) #6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) #6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behaviour was in node
    previously and was not removed intentionally.
    (Rich Trott) #6090

src:
  * add SIGINFO to supported signals
    (James Reggio) #6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) #6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) #6069

PR-URL: #6322
MylesBorins pushed a commit that referenced this pull request Apr 21, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) #5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) #6279
  * update ESLint to 2.7.0
    (silverwind) #6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) #6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) #6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) #6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behaviour was in node
    previously and was not removed intentionally.
    (Rich Trott) #6090

src:
  * add SIGINFO to supported signals
    (James Reggio) #6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) #6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) #6069

PR-URL: #6322
joelostrowski pushed a commit to joelostrowski/node that referenced this pull request Apr 25, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) nodejs#5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) nodejs#6279
  * update ESLint to 2.7.0
    (silverwind) nodejs#6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) nodejs#6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) nodejs#6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) nodejs#6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behaviour was in node
    previously and was not removed intentionally.
    (Rich Trott) nodejs#6090

src:
  * add SIGINFO to supported signals
    (James Reggio) nodejs#6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) nodejs#6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) nodejs#6069

PR-URL: nodejs#6322
jasnell added a commit that referenced this pull request Apr 26, 2016
Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: #521
PR-URL: #5880
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
jasnell pushed a commit that referenced this pull request Apr 26, 2016
Buffer:
  * Buffer.prototype.compare can now compare sub-ranges of two Buffers
   (James M Snell) #5880

deps:
  * update to http-parser 2.7.0
    (Fedor Indutny) #6279
  * update ESLint to 2.7.0
    (silverwind) #6132

net:
  * adds support for passing DNS lookup hints to createConnection()
    (Colin Ihrig) #6000

node:
  * Make the builtin libraries available for the --eval and --print
    CLI options
    (Anna Henningsen) #6207

npm:
  * upgrade npm to 3.8.6
    (Kat Marchán) #6153

repl:
  * Pressing enter in the repl will repeat the last command by default
    if no input has been received. This behaviour was in node
    previously and was not removed intentionally.
    (Rich Trott) #6090

src:
  * add SIGINFO to supported signals
    (James Reggio) #6093

streams:
  * Fix a regression that caused by net streams requesting multiple
    chunks synchronously when combined with cork/uncork
    (Matteo Collina) #6164

zlib:
  * The flushing flag is now configurable allowing for decompression
    of partial data
    (Anna Henningsen) #6069

PR-URL: #6322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Buffer - feature request: Buffer.compare with offsets.
4 participants