From f9043c84a7afee524650fca9c93d9940fc9f4d8c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 22 Dec 2022 20:08:05 +0200 Subject: [PATCH] builtin: optimise the initialisation of the common 1 byte element arrays `[]u8` --- vlib/builtin/array.v | 53 +++++++++++++++++++----------- vlib/builtin/array_d_gcboehm_opt.v | 16 +++++++-- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 7d9deb2c58..9a006aaecf 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -56,9 +56,18 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array if val != 0 { mut eptr := &u8(arr.data) unsafe { - for _ in 0 .. arr.len { - vmemcpy(eptr, val, arr.element_size) - eptr += arr.element_size + if eptr != nil { + if arr.element_size == 1 { + byte_value := *(&u8(val)) + for i in 0 .. arr.len { + eptr[i] = byte_value + } + } else { + for _ in 0 .. arr.len { + vmemcpy(eptr, val, arr.element_size) + eptr += arr.element_size + } + } } } } @@ -75,10 +84,12 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, d } mut eptr := &u8(arr.data) unsafe { - for _ in 0 .. arr.len { - val_clone := val.clone_to_depth(depth) - vmemcpy(eptr, &val_clone, arr.element_size) - eptr += arr.element_size + if eptr != nil { + for _ in 0 .. arr.len { + val_clone := val.clone_to_depth(depth) + vmemcpy(eptr, &val_clone, arr.element_size) + eptr += arr.element_size + } } } return arr @@ -94,10 +105,12 @@ fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array } mut eptr := &u8(arr.data) unsafe { - for _ in 0 .. arr.len { - val_clone := val.clone() - vmemcpy(eptr, &val_clone, arr.element_size) - eptr += arr.element_size + if eptr != nil { + for _ in 0 .. arr.len { + val_clone := val.clone() + vmemcpy(eptr, &val_clone, arr.element_size) + eptr += arr.element_size + } } } return arr @@ -191,14 +204,16 @@ pub fn (a array) repeat_to_depth(count int, depth int) array { arr_step_size := u64(a.len) * u64(arr.element_size) mut eptr := &u8(arr.data) unsafe { - for _ in 0 .. count { - if depth > 0 { - ary_clone := a.clone_to_depth(depth) - vmemcpy(eptr, &u8(ary_clone.data), a_total_size) - } else { - vmemcpy(eptr, &u8(a.data), a_total_size) + if eptr != nil { + for _ in 0 .. count { + if depth > 0 { + ary_clone := a.clone_to_depth(depth) + vmemcpy(eptr, &u8(ary_clone.data), a_total_size) + } else { + vmemcpy(eptr, &u8(a.data), a_total_size) + } + eptr += arr_step_size } - eptr += arr_step_size } } } @@ -661,7 +676,7 @@ pub fn (mut a3 array) push_many(val voidptr, size int) { // reverse_in_place reverses existing array data, modifying original array. pub fn (mut a array) reverse_in_place() { - if a.len < 2 { + if a.len < 2 || a.element_size == 0 { return } unsafe { diff --git a/vlib/builtin/array_d_gcboehm_opt.v b/vlib/builtin/array_d_gcboehm_opt.v index 6522fa269f..70f24ab7ba 100644 --- a/vlib/builtin/array_d_gcboehm_opt.v +++ b/vlib/builtin/array_d_gcboehm_opt.v @@ -24,9 +24,19 @@ fn __new_array_with_default_noscan(mylen int, cap int, elm_size int, val voidptr len: mylen cap: cap_ } - if val != 0 { - for i in 0 .. arr.len { - unsafe { arr.set_unsafe(i, val) } + if val != 0 && arr.data != unsafe { nil } { + if elm_size == 1 { + byte_value := *(&u8(val)) + dptr := &u8(arr.data) + for i in 0 .. arr.len { + unsafe { + dptr[i] = byte_value + } + } + } else { + for i in 0 .. arr.len { + unsafe { arr.set_unsafe(i, val) } + } } } return arr