On Tue, Feb 1, 2022 at 3:12 PM Eric Blake <eblake(a)redhat.com> wrote:
On Sun, Jan 30, 2022 at 01:33:33AM +0200, Nir Soffer wrote:
> AioBuffer.Bytes() cannot be used for coping images from NBD to other
copying
> APis because it copies the entire image. Add a new Slice() function,
> creating a slice backed by the underling buffer.
underlying
>
> Using Slice() is efficient, but less safe, like Get(). The returned
> slice must be used only before calling Free(). This should not be an
> issue with typical code.
>
> Testing show that Slice() is much faster than Bytes() for typical 256k
> buffer:
>
> BenchmarkAioBufferBytes-12 86616 16529 ns/op
> BenchmarkAioBufferSlice-12 1000000000 0.4630 ns/op
>
> I modified the aio_copy example to use AioBuffer and complied 2
compiled
> +++ b/golang/libnbd_620_aio_buffer_test.go
> @@ -44,20 +44,35 @@ func TestAioBuffer(t *testing.T) {
> /* Modifying returned slice does not modify the buffer. */
> for i := 0; i < len(b); i++ {
> b[i] = 42
> }
>
> /* Bytes() still returns zeroes. */
> if !bytes.Equal(buf.Bytes(), zeroes) {
> t.Fatalf("Expected %v, got %v", zeroes, buf.Bytes())
> }
>
> + /* Creating a slice without copying the underlhing buffer. */
underlying
> + s := buf.Slice()
> + if !bytes.Equal(s, zeroes) {
> + t.Fatalf("Expected %v, got %v", zeroes, s)
> + }
> +
> + /* Modifing the slice modifies the underlying buffer. */
Modifying
> }
> +
> +// Benchmark creating a slice without copying the underling buffer.
underlying
> +func BenchmarkAioBufferSlice(b *testing.B) {
> + buf := MakeAioBuffer(bufferSize)
> + defer buf.Free()
> + var r int
> +
> + b.ResetTimer()
> + for i := 0; i < b.N; i++ {
> + r += len(buf.Slice())
> + }
> +}
> --
> 2.34.1
Will fix in v2