[Libguestfs] [PATCH libnbd] generator/Go.ml: Simplify copy_uint32_array

Richard W.M. Jones rjones at redhat.com
Sun Feb 6 19:12:33 UTC 2022


On Sun, Feb 06, 2022 at 07:45:20PM +0200, Nir Soffer wrote:
> Create a slice backed up by the entries pointer, and copy the data with
> builtin copy(). This can be 3x times faster but I did not measure it.
> 
> Eric posted a similar patch[1] last year, but the patch seems to be
> stuck with unrelated incomplete work.
> 
> [1] https://listman.redhat.com/archives/libguestfs/2021-December/msg00065.html
> 
> Signed-off-by: Nir Soffer <nsoffer at redhat.com>
> ---
>  generator/GoLang.ml | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/generator/GoLang.ml b/generator/GoLang.ml
> index eb3aa263..73838199 100644
> --- a/generator/GoLang.ml
> +++ b/generator/GoLang.ml
> @@ -1,13 +1,13 @@
>  (* hey emacs, this is OCaml code: -*- tuareg -*- *)
>  (* nbd client library in userspace: generator
> - * Copyright (C) 2013-2020 Red Hat Inc.
> + * Copyright (C) 2013-2022 Red Hat Inc.
>   *
>   * This library is free software; you can redistribute it and/or
>   * modify it under the terms of the GNU Lesser General Public
>   * License as published by the Free Software Foundation; either
>   * version 2 of the License, or (at your option) any later version.
>   *
>   * This library is distributed in the hope that it will be useful,
>   * but WITHOUT ANY WARRANTY; without even the implied warranty of
>   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>   * Lesser General Public License for more details.
> @@ -508,24 +508,24 @@ package libnbd
>  #include \"wrappers.h\"
>  */
>  import \"C\"
>  
>  import \"unsafe\"
>  
>  /* Closures. */
>  
>  func copy_uint32_array (entries *C.uint32_t, count C.size_t) []uint32 {
>      ret := make([]uint32, int (count))
> -    for i := 0; i < int (count); i++ {
> -       entry := (*C.uint32_t) (unsafe.Pointer(uintptr(unsafe.Pointer(entries)) + (unsafe.Sizeof(*entries) * uintptr(i))))
> -       ret[i] = uint32 (*entry)
> -    }
> +    // See https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
> +    // TODO: Use unsafe.Slice() when we require Go 1.17.
> +    s := (*[1<<30]uint32)(unsafe.Pointer(entries))[:count:count]
> +    copy(ret, s)
>      return ret
>  }
>  ";
>  
>    List.iter (
>      fun { cbname; cbargs } ->
>        let uname = camel_case cbname in
>        pr "type %sCallback func (" uname;
>        let comma = ref false in
>        List.iter (

ACK

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org




More information about the Libguestfs mailing list