Skip to content

Commit 9ed44dd

Browse files
authored
small wants compat mode (#763)
* auto align want reply * compat mode for old peers * lint
1 parent 93f8504 commit 9ed44dd

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/bitfield.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ module.exports = class Bitfield {
495495
}
496496

497497
*want(start, length) {
498+
if (start & 31) {
499+
const d = start & 31
500+
start -= d
501+
length += d
502+
}
503+
498504
let j = start & (BITS_PER_SEGMENT - 1)
499505
let i = (start - j) / BITS_PER_SEGMENT
500506

lib/wants.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const BATCH_UINTS = 128
1+
const COMPAT = true // should be flipped once this is widely deployed
2+
const BATCH_UINTS = COMPAT ? 65536 : 128
23
const BATCH_BYTES = BATCH_UINTS * 4
34
const BATCH = BATCH_BYTES * 8 // in bits
45
const MAX_REMOTE_BATCHES = 4
@@ -25,6 +26,14 @@ class LocalWants {
2526
if (this.destroyed) return null
2627
if (this.any === null) this.any = []
2728

29+
if (COMPAT) {
30+
let r = start & (BATCH - 1)
31+
start -= r
32+
length += r
33+
r = length & (BATCH - 1)
34+
if (r) length = length - r + BATCH
35+
}
36+
2837
for (let i = 0; i < this.any.length; i++) {
2938
const w = this.any[i]
3039
if (w.start === start && w.length === length) {
@@ -38,7 +47,7 @@ class LocalWants {
3847
this.any.push(w)
3948
handle.addWant(w)
4049

41-
return { start, length, any: true }
50+
return { start, length, any: !COMPAT }
4251
}
4352

4453
removeAnyRange(start, length, handle) {
@@ -54,7 +63,7 @@ class LocalWants {
5463

5564
const head = this.any.pop()
5665
if (head !== w) this.any[i] = head
57-
return { start, length, any: true }
66+
return { start, length, any: !COMPAT }
5867
}
5968

6069
return null

0 commit comments

Comments
 (0)