Skip to content

Commit

Permalink
fileget: allocate a slice with enough capacity
Browse files Browse the repository at this point in the history
so a copy is not needed in MarshalBinary and sendPacket.

Here are some profiling results while downloading a file,

before this patch:

1254.24MB 55.18% 55.18%  1254.24MB 55.18%  github.com/pkg/sftp.sshFxpDataPacket.MarshalBinary
  991.81MB 43.63% 98.81%   991.81MB 43.63%  github.com/pkg/sftp.fileget
       1MB 0.044% 98.86%  1255.24MB 55.22%  github.com/pkg/sftp.(*packetManager).maybeSendPackets
    0.50MB 0.022% 98.88%  1260.24MB 55.44%  github.com/pkg/sftp.(*packetManager).controller
         0     0% 98.88%   991.81MB 43.63%  github.com/pkg/sftp.(*Request).call
         0     0% 98.88%   993.31MB 43.70%  github.com/pkg/sftp.(*RequestServer).Serve.func1.1
         0     0% 98.88%   993.31MB 43.70%  github.com/pkg/sftp.(*RequestServer).packetWorker
         0     0% 98.88%  1254.24MB 55.18%  github.com/pkg/sftp.(*conn).sendPacket
         0     0% 98.88%  1254.24MB 55.18%  github.com/pkg/sftp.sendPacket

with this patch:

1209.48MB 98.46% 98.46%  1209.48MB 98.46%  github.com/pkg/sftp.fileget
       2MB  0.16% 98.63%     7.50MB  0.61%  github.com/pkg/sftp.recvPacket
         0     0% 98.63%        8MB  0.65%  github.com/drakkan/sftpgo/sftpd.Configuration.handleSftpConnection
         0     0% 98.63%  1209.48MB 98.46%  github.com/pkg/sftp.(*Request).call
         0     0% 98.63%        8MB  0.65%  github.com/pkg/sftp.(*RequestServer).Serve
         0     0% 98.63%  1209.98MB 98.50%  github.com/pkg/sftp.(*RequestServer).Serve.func1.1
         0     0% 98.63%  1209.98MB 98.50%  github.com/pkg/sftp.(*RequestServer).packetWorker
         0     0% 98.63%     7.50MB  0.61%  github.com/pkg/sftp.(*conn).recvPacket (inline)
  • Loading branch information
drakkan committed Mar 9, 2020
1 parent f1f62a1 commit 22ac97d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,11 @@ type sshFxpDataPacket struct {
}

func (p sshFxpDataPacket) MarshalBinary() ([]byte, error) {
b := []byte{sshFxpData}
b = marshalUint32(b, p.ID)
b = marshalUint32(b, p.Length)
b = append(b, p.Data[:p.Length]...)
b := append(p.Data, make([]byte, 9)...)
copy(b[9:], p.Data[:p.Length])
b[0] = sshFxpData
binary.BigEndian.PutUint32(b[1:5], p.ID)
binary.BigEndian.PutUint32(b[5:9], p.Length)
return b, nil
}

Expand Down
5 changes: 4 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ func fileget(h FileReader, r *Request, pkt requestPacket) responsePacket {
}

_, offset, length := packetData(pkt)
data := make([]byte, clamp(length, maxTxPacket))
dataLen := clamp(length, maxTxPacket)
// we allocate a slice with a bigger capacity so we avoid a new allocation in MarshalBinary and in sendPacket
// we need 9 bytes in MarshalBinary and 4 bytes in sendPacket
data := make([]byte, dataLen, dataLen+13)
n, err := reader.ReadAt(data, offset)
// only return EOF erro if no data left to read
if err != nil && (err != io.EOF || n == 0) {
Expand Down

0 comments on commit 22ac97d

Please sign in to comment.