-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const v8 = require('v8'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['offset', 'slice'], | ||
size: [16, 512, 1024, 4096, 16386], | ||
millions: [1] | ||
}); | ||
|
||
function compareUsingSlice(b0, b1, len, iter) { | ||
|
||
// Force optimization before starting the benchmark | ||
Buffer.compare(b0.slice(1, len), b1.slice(1, len)); | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(Buffer.compare)'); | ||
eval('%OptimizeFunctionOnNextCall(b0.slice)'); | ||
eval('%OptimizeFunctionOnNextCall(b1.slice)'); | ||
Buffer.compare(b0.slice(1, len), b1.slice(1, len)); | ||
doCompareUsingSlice(b0, b1, len, iter); | ||
} | ||
|
||
function doCompareUsingSlice(b0, b1, len, iter) { | ||
var i; | ||
bench.start(); | ||
for (i = 0; i < iter; i++) | ||
Buffer.compare(b0.slice(1, len), b1.slice(1, len)); | ||
bench.end(iter / 1e6); | ||
} | ||
|
||
function compareUsingOffset(b0, b1, len, iter) { | ||
len = len + 1; | ||
// Force optimization before starting the benchmark | ||
b0.compare(b1, 1, len, 1, len); | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(b0.compare)'); | ||
b0.compare(b1, 1, len, 1, len); | ||
doCompareUsingOffset(b0, b1, len, iter); | ||
} | ||
|
||
function doCompareUsingOffset(b0, b1, len, iter) { | ||
var i; | ||
bench.start(); | ||
for (i = 0; i < iter; i++) | ||
b0.compare(b1, 1, len, 1, len); | ||
bench.end(iter / 1e6); | ||
} | ||
|
||
function main(conf) { | ||
const iter = (conf.millions >>> 0) * 1e6; | ||
const size = (conf.size >>> 0); | ||
const method = conf.method === 'slice' ? | ||
compareUsingSlice : compareUsingOffset; | ||
method(Buffer.alloc(size, 'a'), | ||
Buffer.alloc(size, 'b'), | ||
size >> 1, | ||
iter); | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -870,6 +870,62 @@ void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) { | |
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length()); | ||
} | ||
|
||
// Normalize val to be an integer in the range of [1, -1] since | ||
// implementations of memcmp() can vary by platform. | ||
static int normalizeCompareVal(int val, size_t a_length, size_t b_length) { | ||
if (val == 0) { | ||
if (a_length > b_length) | ||
return 1; | ||
else if (a_length < b_length) | ||
return -1; | ||
} else { | ||
if (val > 0) | ||
return 1; | ||
else | ||
return -1; | ||
} | ||
return val; | ||
} | ||
|
||
void CompareOffset(const FunctionCallbackInfo<Value> &args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]); | ||
THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]); | ||
SPREAD_ARG(args[0], ts_obj); | ||
SPREAD_ARG(args[1], target); | ||
|
||
size_t target_start; | ||
size_t source_start; | ||
size_t source_end; | ||
size_t target_end; | ||
|
||
CHECK_NOT_OOB(ParseArrayIndex(args[2], 0, &target_start)); | ||
CHECK_NOT_OOB(ParseArrayIndex(args[3], 0, &source_start)); | ||
CHECK_NOT_OOB(ParseArrayIndex(args[4], target_length, &target_end)); | ||
CHECK_NOT_OOB(ParseArrayIndex(args[5], ts_obj_length, &source_end)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curse me for using |
||
|
||
if (source_start > ts_obj_length) | ||
return env->ThrowRangeError("out of range index"); | ||
if (target_start > target_length) | ||
return env->ThrowRangeError("out of range index"); | ||
|
||
CHECK_LE(source_start, source_end); | ||
CHECK_LE(target_start, target_end); | ||
|
||
size_t to_cmp = MIN(MIN(source_end - source_start, | ||
target_end - target_start), | ||
ts_obj_length - source_start); | ||
|
||
int val = normalizeCompareVal(to_cmp > 0 ? | ||
memcmp(ts_obj_data + source_start, | ||
target_data + target_start, | ||
to_cmp) : 0, | ||
source_end - source_start, | ||
target_end - target_start); | ||
|
||
args.GetReturnValue().Set(val); | ||
} | ||
|
||
void Compare(const FunctionCallbackInfo<Value> &args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
@@ -881,22 +937,9 @@ void Compare(const FunctionCallbackInfo<Value> &args) { | |
|
||
size_t cmp_length = MIN(obj_a_length, obj_b_length); | ||
|
||
int val = cmp_length > 0 ? memcmp(obj_a_data, obj_b_data, cmp_length) : 0; | ||
|
||
// Normalize val to be an integer in the range of [1, -1] since | ||
// implementations of memcmp() can vary by platform. | ||
if (val == 0) { | ||
if (obj_a_length > obj_b_length) | ||
val = 1; | ||
else if (obj_a_length < obj_b_length) | ||
val = -1; | ||
} else { | ||
if (val > 0) | ||
val = 1; | ||
else | ||
val = -1; | ||
} | ||
|
||
int val = normalizeCompareVal(cmp_length > 0 ? | ||
memcmp(obj_a_data, obj_b_data, cmp_length) : 0, | ||
obj_a_length, obj_b_length); | ||
args.GetReturnValue().Set(val); | ||
} | ||
|
||
|
@@ -1172,6 +1215,7 @@ void Initialize(Local<Object> target, | |
|
||
env->SetMethod(target, "byteLengthUtf8", ByteLengthUtf8); | ||
env->SetMethod(target, "compare", Compare); | ||
env->SetMethod(target, "compareOffset", CompareOffset); | ||
env->SetMethod(target, "fill", Fill); | ||
env->SetMethod(target, "indexOfBuffer", IndexOfBuffer); | ||
env->SetMethod(target, "indexOfNumber", IndexOfNumber); | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); | ||
const b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]); | ||
|
||
assert.equal(-1, a.compare(b)); | ||
|
||
// Equivalent to a.compare(b). | ||
assert.equal(-1, a.compare(b, 0)); | ||
assert.equal(-1, a.compare(b, '0')); | ||
|
||
// Equivalent to a.compare(b). | ||
assert.equal(-1, a.compare(b, 0, undefined, 0)); | ||
|
||
// Zero-length targer, return 1 | ||
assert.equal(1, a.compare(b, 0, 0, 0)); | ||
assert.equal(1, a.compare(b, '0', '0', '0')); | ||
|
||
// Equivalent to Buffer.compare(a, b.slice(6, 10)) | ||
assert.equal(1, a.compare(b, 6, 10)); | ||
|
||
// Zero-length source, return -1 | ||
assert.equal(-1, a.compare(b, 6, 10, 0, 0)); | ||
|
||
// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5)) | ||
assert.equal(1, a.compare(b, 0, 5, 4)); | ||
|
||
// Equivalent to Buffer.compare(a.slice(1), b.slice(5)) | ||
assert.equal(1, a.compare(b, 5, undefined, 1)); | ||
|
||
// Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4)) | ||
assert.equal(-1, a.compare(b, 2, 4, 2)); | ||
|
||
// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7)) | ||
assert.equal(-1, a.compare(b, 0, 7, 4)); | ||
|
||
// Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7)); | ||
assert.equal(-1, a.compare(b, 0, 7, 4, 6)); | ||
|
||
// zero length target | ||
assert.equal(1, a.compare(b, 0, null)); | ||
|
||
// coerces to targetEnd == 5 | ||
assert.equal(-1, a.compare(b, 0, {valueOf: () => 5})); | ||
|
||
// zero length target | ||
assert.equal(1, a.compare(b, Infinity, -Infinity)); | ||
|
||
// zero length target because default for targetEnd <= targetSource | ||
assert.equal(1, a.compare(b, '0xff')); | ||
|
||
const oor = /out of range index/; | ||
|
||
assert.throws(() => a.compare(b, 0, 100, 0), oor); | ||
assert.throws(() => a.compare(b, 0, 1, 0, 100), oor); | ||
assert.throws(() => a.compare(b, -1), oor); | ||
assert.throws(() => a.compare(b, 0, '0xff'), oor); | ||
assert.throws(() => a.compare(b, 0, Infinity), oor); | ||
assert.throws(() => a.compare(b, -Infinity, Infinity), oor); | ||
assert.throws(() => a.compare(), /Argument must be a Buffer/); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should document whether this will throw if, say,
sourceEnd > buf.length
, or if it will simply return-1
.