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,9 +56,18 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
if val != 0 { if val != 0 {
mut eptr := &u8(arr.data) mut eptr := &u8(arr.data)
unsafe { unsafe {
for _ in 0 .. arr.len { if eptr != nil {
vmemcpy(eptr, val, arr.element_size) if arr.element_size == 1 {
eptr += arr.element_size 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) mut eptr := &u8(arr.data)
unsafe { unsafe {
for _ in 0 .. arr.len { if eptr != nil {
val_clone := val.clone_to_depth(depth) for _ in 0 .. arr.len {
vmemcpy(eptr, &val_clone, arr.element_size) val_clone := val.clone_to_depth(depth)
eptr += arr.element_size vmemcpy(eptr, &val_clone, arr.element_size)
eptr += arr.element_size
}
} }
} }
return arr 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) mut eptr := &u8(arr.data)
unsafe { unsafe {
for _ in 0 .. arr.len { if eptr != nil {
val_clone := val.clone() for _ in 0 .. arr.len {
vmemcpy(eptr, &val_clone, arr.element_size) val_clone := val.clone()
eptr += arr.element_size vmemcpy(eptr, &val_clone, arr.element_size)
eptr += arr.element_size
}
} }
} }
return arr 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) arr_step_size := u64(a.len) * u64(arr.element_size)
mut eptr := &u8(arr.data) mut eptr := &u8(arr.data)
unsafe { unsafe {
for _ in 0 .. count { if eptr != nil {
if depth > 0 { for _ in 0 .. count {
ary_clone := a.clone_to_depth(depth) if depth > 0 {
vmemcpy(eptr, &u8(ary_clone.data), a_total_size) ary_clone := a.clone_to_depth(depth)
} else { vmemcpy(eptr, &u8(ary_clone.data), a_total_size)
vmemcpy(eptr, &u8(a.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. // reverse_in_place reverses existing array data, modifying original array.
pub fn (mut a array) reverse_in_place() { pub fn (mut a array) reverse_in_place() {
if a.len < 2 { if a.len < 2 || a.element_size == 0 {
return return
} }
unsafe { unsafe {

View File

@ -24,9 +24,19 @@ fn __new_array_with_default_noscan(mylen int, cap int, elm_size int, val voidptr
len: mylen len: mylen
cap: cap_ cap: cap_
} }
if val != 0 { if val != 0 && arr.data != unsafe { nil } {
for i in 0 .. arr.len { if elm_size == 1 {
unsafe { arr.set_unsafe(i, val) } 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 return arr