Skip to content

Commit

Permalink
only iterate the known segments ish (#456)
Browse files Browse the repository at this point in the history
* only iterate the known segments ish

* missing some
  • Loading branch information
mafintosh authored Nov 9, 2023
1 parent 688ed54 commit 62fc0d4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/remote-bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ module.exports = class RemoteBitfield {
constructor () {
this._pages = new BigSparseArray()
this._segments = new BigSparseArray()
this._maxSegments = 0
}

getBitfield (index) {
Expand Down Expand Up @@ -177,6 +178,7 @@ module.exports = class RemoteBitfield {
if (!p && val) {
const k = Math.floor(i / PAGES_PER_SEGMENT)
const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
if (this._maxSegments <= k) this._maxSegments = k + 1

p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
}
Expand All @@ -194,6 +196,7 @@ module.exports = class RemoteBitfield {
if (!p && val) {
const k = Math.floor(i / PAGES_PER_SEGMENT)
const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
if (this._maxSegments <= k) this._maxSegments = k + 1

p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
}
Expand All @@ -213,7 +216,7 @@ module.exports = class RemoteBitfield {
let j = position & (BITS_PER_SEGMENT - 1)
let i = (position - j) / BITS_PER_SEGMENT

while (i < this._segments.maxLength) {
while (i < this._maxSegments) {
const s = this._segments.get(i)

let index = -1
Expand Down Expand Up @@ -281,6 +284,7 @@ module.exports = class RemoteBitfield {
if (!p) {
const k = Math.floor(i / PAGES_PER_SEGMENT)
const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
if (this._maxSegments <= k) this._maxSegments = k + 1

p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
}
Expand Down Expand Up @@ -314,6 +318,7 @@ module.exports = class RemoteBitfield {
if (!p) {
const k = Math.floor(i / PAGES_PER_SEGMENT)
const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
if (this._maxSegments <= k) this._maxSegments = k + 1

p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
}
Expand Down

0 comments on commit 62fc0d4

Please sign in to comment.