psanford / wormhole-william

End-to-end encrypted file transfer. A magic wormhole CLI and API in Go (golang).
MIT License
1.07k stars 54 forks source link

cmd: optimize how text receives are printed #76

Closed Jacalz closed 2 years ago

Jacalz commented 2 years ago

Now that we know the length of the text, we can use ReadFull() with a pre-allocated buffer instead. ReadAll always allocates 512 bytes minimum and will append as it goes. We now just allocate just as much as needed and do so once only.

I did this for Rymdport as well and figured that it wouldn't hurt being a good open source citizen and improve it here also :)

Jacalz commented 2 years ago
BenchmarkReadAll-8                   7253271           156.3 ns/op       544 B/op          2 allocs/op
BenchmarkReadFull-8                 12244660            93.98 ns/op      160 B/op          2 allocs/op
psanford commented 2 years ago

Maybe we should just change this to:

_, err = io.Copy(os.Stdout, msg)
Jacalz commented 2 years ago

That seems like a wonderful idea. I hadn’t thought about that at all. Will get this updated tomorrow.

Jacalz commented 2 years ago

I should probably have said something sooner, but this should be ready for a re-review.

Jacalz commented 2 years ago

I'm just going through and trying to clean up my list of open PRs. Any chance to get this re-reviewed?