gdext icon indicating copy to clipboard operation
gdext copied to clipboard

[Performance Issue] Array methods duplicate Variant param or Callable take ownership when it could not.

Open Ughuuu opened this issue 7 months ago • 3 comments

From Performance Investigation

  1. Extra clones Calling set and many other methods of Array with Variant arguments clones the Variant. Example:
let arr = Array::new();
arr.set(0, Variant::nil());

The set will call internally:

    pub fn set(&mut self, index: usize, value: T) {
         let ptr_mut = self.ptr_mut(index);

         // SAFETY: `ptr_mut` just checked that the index is not out of bounds.
         unsafe {
             value.to_variant().move_into_var_ptr(ptr_mut);
         }
     }

which will do to_variant(), even though the argument is already variant. There are multiple such cases throughout the Array class.

  1. Ownership

Calling callv:

    pub fn callv(&self, arguments: VariantArray) -> Variant {
        self.as_inner().callv(arguments)
    }

Takes ownership of VariantArray. It might seem small thing, but for my use cases, i make about 10000 or more calls using callv. Allocating the array every time takes about 100-150ms of a 4s timeslice. I would like to be able to reuse the array. Eg. send it to the callv method, and after the method is called be able to reuse my array:

    pub fn callv(&self, arguments: &VariantArray) -> Variant {
        self.as_inner().callv(arguments)
    }

Ughuuu avatar Jul 05 '24 20:07 Ughuuu