1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: optimise the initialisation of the common 1 byte element arrays []u8

This commit is contained in:
Delyan Angelov 2022-12-22 20:08:05 +02:00
parent 8f8a186158
commit f9043c84a7
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 47 additions and 22 deletions

View File

@ -56,12 +56,21 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
if val != 0 {
mut eptr := &u8(arr.data)
unsafe {
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
}
}
}
}
}
return arr
}
@ -75,12 +84,14 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, d
}
mut eptr := &u8(arr.data)
unsafe {
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,12 +105,14 @@ fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array
}
mut eptr := &u8(arr.data)
unsafe {
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,6 +204,7 @@ 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 {
if eptr != nil {
for _ in 0 .. count {
if depth > 0 {
ary_clone := a.clone_to_depth(depth)
@ -202,6 +216,7 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
}
}
}
}
return arr
}
@ -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 {

View File

@ -24,11 +24,21 @@ fn __new_array_with_default_noscan(mylen int, cap int, elm_size int, val voidptr
len: mylen
cap: cap_
}
if val != 0 {
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
}