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)`
pull/13800/head
Nick Treleaven 2022-03-21 21:34:35 +00:00 committed by GitHub
parent f66d2f5d43
commit afbccf79f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 1 deletions

View File

@ -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 ? {