Open ethan256 opened 2 months ago
@magiconair Can you help review it?
Is there a technical reason to not go all the way and replace the buffer allocations in all the write functions also?
func (b *Stream) WriteUint16(n uint16) {
d := make([]byte, 2)
binary.LittleEndian.PutUint16(d, n)
b.Write(d)
}
could become
func (b *Stream) WriteUint16(n uint16) {
b.buf = binary.LittleEndian.AppendUint16(b.buf, n)
}
Just a thought.
Is there a technical reason to not go all the way and replace the buffer allocations in all the write functions also?
func (b *Stream) WriteUint16(n uint16) { d := make([]byte, 2) binary.LittleEndian.PutUint16(d, n) b.Write(d) }
could become
func (b *Stream) WriteUint16(n uint16) { b.buf = binary.LittleEndian.AppendUint16(b.buf, n) }
Just a thought.
I think it's a good idea, I'll give it a try
commit 855932c benchstats results as follow:
goos: linux
goarch: amd64
pkg: github.com/gopcua/opcua/uasc
cpu: AMD Ryzen 7 5800H with Radeon Graphics
│ old.txt │ codec.txt │
│ sec/op │ sec/op vs base │
EncodeMessage-6 12.710µ ± 2% 3.613µ ± 1% -71.57% (p=0.000 n=10)
│ old.txt │ codec.txt │
│ B/op │ B/op vs base │
EncodeMessage-6 4.375Ki ± 0% 1.539Ki ± 0% -64.82% (p=0.000 n=10)
│ old.txt │ codec.txt │
│ allocs/op │ allocs/op vs base │
EncodeMessage-6 169.00 ± 0% 39.00 ± 0% -76.92% (p=0.000 n=10)
Using stream reduces memory allocation during message encoding and improves performance. benchstat: