Skip to content

Commit

Permalink
channel: Use append to allocate an output buffer.
Browse files Browse the repository at this point in the history
This improves the performance and decreases allocations for the split framings
appreciably.

 BENCHMARK                      BEFORE AFTER SPEEDUP B/op (B) B/op (A)
 BenchmarkFramingCost/Line-12   2547   2121  16.7%   4096     2048
 BenchmarkFramingCost/NUL-12    2564   2091  18.4%   4096     2048
  • Loading branch information
creachadair committed Sep 14, 2021
1 parent 9864d5a commit 5cf8851
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions channel/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ func (c split) Send(msg []byte) error {
if bytes.IndexByte(msg, c.split) >= 0 {
return errors.New("message contains split byte")
}
out := make([]byte, len(msg)+1)
copy(out, msg)
out[len(msg)] = c.split
out := append(msg, c.split)
_, err := c.wc.Write(out)
return err
}
Expand Down

0 comments on commit 5cf8851

Please sign in to comment.