2
0
mirror of synced 2025-02-24 14:48:27 +00:00

Make Piece a concrete type

There was a lot of allocations occuring because it was an interface.
This commit is contained in:
Matt Joiner 2015-10-19 01:31:34 +11:00
parent 4b539b1f11
commit 1a4fbed1ac

View File

@ -61,34 +61,28 @@ func (me *Info) NumPieces() int {
return len(me.Pieces) / 20 return len(me.Pieces) / 20
} }
type Piece interface { type Piece struct {
Hash() []byte
Length() int64
Offset() int64
}
type piece struct {
Info *Info Info *Info
i int i int
} }
func (me piece) Length() int64 { func (me Piece) Length() int64 {
if me.i == me.Info.NumPieces()-1 { if me.i == me.Info.NumPieces()-1 {
return me.Info.TotalLength() - int64(me.i)*me.Info.PieceLength return me.Info.TotalLength() - int64(me.i)*me.Info.PieceLength
} }
return me.Info.PieceLength return me.Info.PieceLength
} }
func (me piece) Offset() int64 { func (me Piece) Offset() int64 {
return int64(me.i) * me.Info.PieceLength return int64(me.i) * me.Info.PieceLength
} }
func (me piece) Hash() []byte { func (me Piece) Hash() []byte {
return me.Info.Pieces[me.i*20 : (me.i+1)*20] return me.Info.Pieces[me.i*20 : (me.i+1)*20]
} }
func (me *Info) Piece(i int) piece { func (me *Info) Piece(i int) Piece {
return piece{me, i} return Piece{me, i}
} }
func (i *Info) IsDir() bool { func (i *Info) IsDir() bool {