net, config: optimize read and write packets #382
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: close #381
Problem Summary:
WritePacket
andReadPacket
become the hot path when there are many rows returned, so this code should be optimized:WriteOnePacket
calls 2NewReader
and that allocates memory twice.ReadPacket
grows slice indata = append(data, buf)
, which is unncessary.var header [4]byte
escapes to the heap becauseheader
is passed as a parameter to other functions.read
/write
8 times.io.ReadFull
contains unnecessary boundary checks, function calls, and interface conversion.What is changed and how it works:
Optimization:
io.Copy
withreadWriter.Write
to removeNewReader
.data = append(data, buf)
for the first packet.header
intoPacketIO
/compressedReadWriter
as a member.conn-buffer-size
configurable and set to 32K by default. It can't be too large in case there are tens of thousands of idle connections.io.ReadFull
with customizedReadFull
.Others:
DirectWrite
tocompressedReadWriter
because it was missed, but it doesn't matter actually.Check List
Tests
Benchmark Result
Before this PR:
BenchmarkWritePacket-8 2721517 607.5 ns/op 328 B/op 6 allocs/op
BenchmarkReadPacket-8 2829 413287 ns/op 2097727 B/op 6 allocs/op
After this PR:
BenchmarkWritePacket-8 10362842 116.3 ns/op 112 B/op 1 allocs/op
BenchmarkReadPacket-8 5344 232485 ns/op 1049217 B/op 1 allocs/op
Sysbench Result
Before this PR:
After this PR with
conn-buffer-size=131072
:Flame Graph
Before this PR:
After this PR:
Notable changes
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.