Skip to content

Cache verified remote contiguous length - #810

Open
marcus-pousette-hp wants to merge 4 commits into
mainfrom
perf/replicator-request-range
Open

marcus-pousette-hp wants to merge 4 commits into
mainfrom
perf/replicator-request-range

Conversation

@marcus-pousette-hp

@marcus-pousette-hp marcus-pousette-hp commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Before:

read remoteContiguousLength
-> scan remote bitfield from stale cached position
-> return higher value
-> cache stays stale
-> next read scans same area again

After:

read remoteContiguousLength
-> scan remote bitfield once
-> cache the verified higher value
-> next read starts from the higher value

Local bench test from before and after patch.

10k reads over a 50M-bit verified contiguous extent
before: ~51.7 ms
after:  ~2.26 ms

PR was Co-authored with AI.

@marcus-pousette-hp
marcus-pousette-hp requested a review from a team May 14, 2026 12:50
Comment thread lib/replicator.js Outdated
const length = this.remoteBitfield.findFirst(false, this._remoteContiguousLength)
// Cache the verified extent so repeated reads do not rescan the same bitfield run.
if (length > this._remoteContiguousLength) this._remoteContiguousLength = length
return length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Having a getter with side effects is tenuous as getters imply only reading values.

Can we apply these checks when the remote bitfield is mutated (onbitfield & onrange)? This will also ensure the value is updated when we have more info from the remote. Otherwise accessing _remoteContiguousLength directly will have less information than if calling .remoteContiguousLength. We also could explore replacing _remoteContiguousLength to be .remoteContiguousLength, though maybe should be a separate PR to keep scope simple and incremental.

Regarding that, could you add a test for a scenario where onbitfield updates _remoteContigousLength and later a broadcastRange() is called for the same peer but is skipped via the check here:

if (this._remoteContiguousLength >= start + length) return

Comment thread test/remote-bitfield.js Outdated

const peer = getPeer(b, a)

peer.remoteFork = b.core.state.fork

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Setting the fork isn't necessary as they both start at 0.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants