Skip to content
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

modernize lib/rarity-map.js #1485

Merged
merged 4 commits into from Aug 27, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

modernize lib/rarity-map.jsj

  • Loading branch information
jimmywarting committed Aug 26, 2018
commit 40ca1578a7703448264a4f22832520d23b3118e4
@@ -1,121 +1,113 @@
module.exports = RarityMap

/**
* Mapping of torrent pieces to their respective availability in the torrent swarm. Used
* by the torrent manager for implementing the rarest piece first selection strategy.
*/
function RarityMap (torrent) {
var self = this
class RarityMap {
constructor (torrent) {
const self = this

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

We should stop using self


self._torrent = torrent
self._numPieces = torrent.pieces.length
self._pieces = []
self._torrent = torrent
self._numPieces = torrent.pieces.length
self._pieces = new Array(self._numPieces).fill(0)

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Any benefits in doing this?

This comment has been minimized.

Copy link
@jimmywarting

jimmywarting Aug 26, 2018

Author Contributor

You probably don't need the initial .fill(0) in new Array(self._numPieces).fill(0) maybe just new Array(self._numPieces) will do

Reason why i did this was b/c new Array(n) is faster then writing to a literal array []
And using .fill(0) is faster then writing to each index

skarmavbild 2018-08-26 kl 22 01 14

also b/c this looks cleaner then

recalculate () {	
  this._pieces.fill(0)
}

then

recalculate () {	
  for (let i = 0; i < this._numPieces; ++i) {	
    this._pieces[i] = 0	
  }
}

self._onWire = function (wire) {
self.recalculate()
self._initWire(wire)
}
self._onWireHave = function (index) {
self._pieces[index] += 1
}
self._onWireBitfield = function () {
self._onWire = wire => {
self.recalculate()
self._initWire(wire)
}
self._onWireHave = index => {
self._pieces[index] += 1
}
self._onWireBitfield = () => {
self.recalculate()
}

self._torrent.wires.forEach(wire => {
self._initWire(wire)
})
self._torrent.on('wire', self._onWire)
self.recalculate()
}

self._torrent.wires.forEach(function (wire) {
self._initWire(wire)
})
self._torrent.on('wire', self._onWire)
self.recalculate()
}

/**
* Get the index of the rarest piece. Optionally, pass a filter function to exclude
* certain pieces (for instance, those that we already have).
*
* @param {function} pieceFilterFunc
* @return {number} index of rarest piece, or -1
*/
RarityMap.prototype.getRarestPiece = function (pieceFilterFunc) {
if (!pieceFilterFunc) pieceFilterFunc = trueFn

var candidates = []
var min = Infinity

for (var i = 0; i < this._numPieces; ++i) {
if (!pieceFilterFunc(i)) continue
/**
* Get the index of the rarest piece. Optionally, pass a filter function to exclude
* certain pieces (for instance, those that we already have).
*
* @param {function} pieceFilterFunc
* @return {number} index of rarest piece, or -1
*/
getRarestPiece (pieceFilterFunc) {
let candidates = []
let min = Infinity

for (let i = 0; i < this._numPieces; ++i) {
if (pieceFilterFunc && !pieceFilterFunc(i)) continue

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Much better removing that trueFn

This comment has been minimized.

Copy link
@jimmywarting

jimmywarting Aug 26, 2018

Author Contributor

thanks


const availability = this._pieces[i]
if (availability === min) {
candidates.push(i)
} else if (availability < min) {
candidates = [ i ]
min = availability
}
}

var availability = this._pieces[i]
if (availability === min) {
candidates.push(i)
} else if (availability < min) {
candidates = [ i ]
min = availability
if (candidates.length) {
// if there are multiple pieces with the same availability, choose one randomly
return candidates[Math.random() * candidates.length | 0]
} else {
return -1
}
}

if (candidates.length > 0) {
// if there are multiple pieces with the same availability, choose one randomly
return candidates[Math.random() * candidates.length | 0]
} else {
return -1
destroy () {
const self = this

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Same here, no more self

self._torrent.removeListener('wire', self._onWire)
self._torrent.wires.forEach(wire => {
self._cleanupWireEvents(wire)
})
self._torrent = null
self._pieces = null

self._onWire = null
self._onWireHave = null
self._onWireBitfield = null
}
}

RarityMap.prototype.destroy = function () {
var self = this
self._torrent.removeListener('wire', self._onWire)
self._torrent.wires.forEach(function (wire) {
self._cleanupWireEvents(wire)
})
self._torrent = null
self._pieces = null

self._onWire = null
self._onWireHave = null
self._onWireBitfield = null
}

RarityMap.prototype._initWire = function (wire) {
var self = this
_initWire (wire) {
const self = this

wire._onClose = function () {
self._cleanupWireEvents(wire)
for (var i = 0; i < this._numPieces; ++i) {
self._pieces[i] -= wire.peerPieces.get(i)
wire._onClose = function () {
self._cleanupWireEvents(wire)
for (let i = 0; i < this._numPieces; ++i) {
self._pieces[i] -= wire.peerPieces.get(i)
}
}
}

wire.on('have', self._onWireHave)
wire.on('bitfield', self._onWireBitfield)
wire.once('close', wire._onClose)
}

/**
* Recalculates piece availability across all peers in the torrent.
*/
RarityMap.prototype.recalculate = function () {
var i
for (i = 0; i < this._numPieces; ++i) {
this._pieces[i] = 0
wire.on('have', self._onWireHave)
wire.on('bitfield', self._onWireBitfield)
wire.once('close', wire._onClose)
}

var numWires = this._torrent.wires.length
for (i = 0; i < numWires; ++i) {
var wire = this._torrent.wires[i]
for (var j = 0; j < this._numPieces; ++j) {
this._pieces[j] += wire.peerPieces.get(j)
/**
* Recalculates piece availability across all peers in the torrent.
*/
recalculate () {
this._pieces.fill(0)

for (const wire of this._torrent.wires) {
for (let i = 0; i < this._numPieces; ++i) {
this._pieces[i] += wire.peerPieces.get(i)
}
}
}
}

RarityMap.prototype._cleanupWireEvents = function (wire) {
wire.removeListener('have', this._onWireHave)
wire.removeListener('bitfield', this._onWireBitfield)
if (wire._onClose) wire.removeListener('close', wire._onClose)
wire._onClose = null
_cleanupWireEvents (wire) {
wire.removeListener('have', this._onWireHave)
wire.removeListener('bitfield', this._onWireBitfield)
if (wire._onClose) wire.removeListener('close', wire._onClose)
wire._onClose = null
}
}

function trueFn () {
return true
}
module.exports = RarityMap
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.