Using Slice() we can use builtin copy() instead of a manual loop, which
is 4.6 times faster with a typical 256k buffer:
Before:
BenchmarkFromBytes-12 9806 111474 ns/op
After:
BenchmarkFromBytes-12 48193 24106 ns/op
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
golang/aio_buffer.go | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/golang/aio_buffer.go b/golang/aio_buffer.go
index 008d9ae0..52ea54de 100644
--- a/golang/aio_buffer.go
+++ b/golang/aio_buffer.go
@@ -47,25 +47,22 @@ func MakeAioBuffer(size uint) AioBuffer {
// MakeAioBuffer makes a new buffer backed by a C allocated array. The
// underlying buffer is set to zero.
func MakeAioBufferZero(size uint) AioBuffer {
return AioBuffer{C.calloc(C.ulong(1), C.ulong(size)), size}
}
// FromBytes makes a new buffer backed by a C allocated array, initialized by
// copying the given Go slice.
func FromBytes(buf []byte) AioBuffer {
- size := len(buf)
- ret := MakeAioBuffer(uint(size))
- for i := 0; i < len(buf); i++ {
- *ret.Get(uint(i)) = buf[i]
- }
+ ret := MakeAioBuffer(uint(len(buf)))
+ copy(ret.Slice(), buf)
return ret
}
// Free deallocates the underlying C allocated array. Using the buffer after
// Free() will panic.
func (b *AioBuffer) Free() {
if b.P != nil {
C.free(b.P)
b.P = nil
}
--
2.34.1