-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
KBucket.distance #29
Comments
JFYI, buffer-xor uses value, not 255 |
I don't understand. Could you provide a code example of what outcome you expect and what you're getting instead? |
id1 = [ 0x04 ] |
Ah, I understand now. The short answer is that it is a convention I picked 😃 . The long answer is that when I was exploring the references (ones from README), they all made an assumption that the node ids would be of equal length. At the time I was creating
Since we are doing ops at level of bytes, then that would result in using This test case informally documents this convention:
In addition what I mentioned above, the other distance choice I made is that the shorter buffer is assumed to be the most significant bits (and the filling happens in the least significant part). This was done to align with use cases where node id's are named using namespaces like:
This way, it "makes sense" to compare the distance between
|
Sorry, can't understand your truth table, how bit can not exists in a middle? I understand that usually length of ids is equal, so this question almost doesn't make sense, but I still think that we should take value from longest id, not 255. For example, if we have 3 ids: xor(a, b) should not be equal to xor(a, c), right? but with current implementation we receive that distance is equal.. your example with modified distance: var xor = require('buffer-xor')
function distance (firstId, secondId) {
var buffer = xor(firstId, secondId)
return buffer.reduce(function (d, v) { return d * 256 + v }, 0)
}
distance(new Buffer(".com.mystuff"), new Buffer(".com.mystuff.detailed"))
// => 855784543728809300000
distance(new Buffer(".com"), new Buffer(".com.mystuff.detailed"))
// => 1.5798505339527512e+40 |
With the example you provided:
I think Consider the following example:
In this case, |
Why?
no, distance is different: var xor = require('buffer-xor')
function distance (firstId, secondId) {
var buffer = xor(firstId, secondId)
return buffer.reduce(function (d, v) { return d * 256 + v }, 0)
}
distance(new Buffer(".com"), new Buffer(".com.em"))
// => 3040621
distance(new Buffer(".com"), new Buffer(".com.emem"))
// => 199270163821 |
You're right. Looks like hierarchical namespacing use case works only if each namespace in the hierarchy is of equal length.
The answer to the why is the truth table. I defined XOR when The fact that
I don't think the definition of |
Yes, you're correct.
Agreed. Then maybe you consider using KBucket.distance = function (firstId, secondId) {
return xor(firstId, secondId).reduce(function (d, v) { return d * 256 + v }, 0)
} |
I benchmarked: 'use strict'
var KBucket = require('../index')
var xor = require('buffer-xor');
var _0000000100100100 = new Buffer('0124', 'hex')
var _0100000000100100 = new Buffer('4024', 'hex')
var kBucketDistance = KBucket.distance;
var bufferXorDistance = function (firstId, secondId) {
return xor(firstId, secondId).reduce(function (d, v) { return d * 256 + v }, 0)
};
var hrtime = process.hrtime()
for (var i = 0; i < 1e7; i++) {
kBucketDistance(_0000000100100100, _0100000000100100)
}
var kBucketDistanceDiff = process.hrtime(hrtime)
// console.log(diff[0] * 1e9 + diff[1])
hrtime = process.hrtime()
for (var i = 0; i < 1e7; i++) {
kBucketDistance(_0000000100100100, _0100000000100100)
}
kBucketDistanceDiff = process.hrtime(hrtime)
hrtime = process.hrtime()
for (var i = 0; i < 1e7; i++) {
bufferXorDistance(_0000000100100100, _0100000000100100)
}
var bufferXorDistanceDiff = process.hrtime(hrtime)
hrtime = process.hrtime()
for (var i = 0; i < 1e7; i++) {
bufferXorDistance(_0000000100100100, _0100000000100100)
}
bufferXorDistanceDiff = process.hrtime(hrtime)
console.log("KBucket.distance : ", kBucketDistanceDiff[0] * 1e9 + kBucketDistanceDiff[1])
console.log("buffer-xor distance: ", bufferXorDistanceDiff[0] * 1e9 + bufferXorDistanceDiff[1]) Results on my machine:
|
it was expected, |
Should not be here index.js#L98 value from largest buffer, not 255?
The text was updated successfully, but these errors were encountered: