From afbccf79f703c2e5e30d981db2352cc0f36e9e4d Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 21 Mar 2022 21:34:35 +0000 Subject: [PATCH] builtin: don't memdup element for `array.pop` (depends on how cgen works) (#13789) The voidptr returned is immediately dereferenced in cgen so the memory is copied before the array can be appended to: `*(int*)array_pop(&a)` --- vlib/builtin/array.v | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 6109d66178..e29e50d478 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -402,12 +402,13 @@ pub fn (mut a array) pop() voidptr { a.len = new_len // Note: a.cap is not changed here *on purpose*, so that // further << ops on that array will be more efficient. - return unsafe { memdup(last_elem, a.element_size) } + return last_elem } // delete_last efficiently deletes the last element of the array. // It does it simply by reducing the length of the array by 1. // If the array is empty, this will panic. +// See also: [trim](#array.trim) pub fn (mut a array) delete_last() { // copy pasting code for performance $if !no_bounds_checking ? {