builtin: improve performance for common array ops, based on VTune analysis
parent
3e40cd5baa
commit
57db261538
|
@ -171,9 +171,9 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
|
||||||
for i in 0 .. count {
|
for i in 0 .. count {
|
||||||
if depth > 0 {
|
if depth > 0 {
|
||||||
ary_clone := unsafe { a.clone_to_depth(depth) }
|
ary_clone := unsafe { a.clone_to_depth(depth) }
|
||||||
unsafe { vmemcpy(arr.get_unsafe(i * a.len), &byte(ary_clone.data), a.len * a.element_size) }
|
unsafe { vmemcpy( &byte(arr.data) + (i*arr.len) * arr.element_size, &byte(ary_clone.data), a.len * a.element_size) }
|
||||||
} else {
|
} else {
|
||||||
unsafe { vmemcpy(arr.get_unsafe(i * a.len), &byte(a.data), a.len * a.element_size) }
|
unsafe { vmemcpy( &byte(arr.data) + (i*arr.len) * arr.element_size, &byte(a.data), a.len * a.element_size) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -537,17 +537,18 @@ pub fn (a &array) clone_to_depth(depth int) array {
|
||||||
len: a.len
|
len: a.len
|
||||||
cap: a.cap
|
cap: a.cap
|
||||||
}
|
}
|
||||||
|
isizea := int(sizeof(array))
|
||||||
// Recursively clone-generated elements if array element is array type
|
// Recursively clone-generated elements if array element is array type
|
||||||
if depth > 0 && a.element_size == sizeof(array) && a.len >= 0 && a.cap >= a.len {
|
if depth > 0 && a.element_size == isizea && a.len >= 0 && a.cap >= a.len {
|
||||||
for i in 0 .. a.len {
|
for i in 0 .. a.len {
|
||||||
ar := array{}
|
ar := array{}
|
||||||
unsafe { vmemcpy(&ar, a.get_unsafe(i), int(sizeof(array))) }
|
unsafe { vmemcpy(&ar, &byte(a.data) + i * a.element_size, isizea) }
|
||||||
ar_clone := unsafe { ar.clone_to_depth(depth - 1) }
|
ar_clone := unsafe { ar.clone_to_depth(depth - 1) }
|
||||||
unsafe { arr.set_unsafe(i, &ar_clone) }
|
unsafe { vmemcpy(&byte(arr.data) + arr.element_size * i, &ar_clone, isizea) }
|
||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
} else {
|
} else {
|
||||||
if !isnil(a.data) {
|
if a.data != 0 {
|
||||||
unsafe { vmemcpy(&byte(arr.data), a.data, a.cap * a.element_size) }
|
unsafe { vmemcpy(&byte(arr.data), a.data, a.cap * a.element_size) }
|
||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
|
@ -571,31 +572,33 @@ fn (mut a array) set(i int, val voidptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut a array) push(val voidptr) {
|
fn (mut a array) push(val voidptr) {
|
||||||
if a.len >= a.cap {
|
olen := a.len
|
||||||
a.ensure_cap(a.len + 1)
|
nlen := olen + 1
|
||||||
|
if olen >= a.cap {
|
||||||
|
a.ensure_cap(nlen)
|
||||||
}
|
}
|
||||||
unsafe { vmemmove(&byte(a.data) + a.element_size * a.len, val, a.element_size) }
|
unsafe { vmemmove(&byte(a.data) + a.element_size * olen, val, a.element_size) }
|
||||||
a.len++
|
a.len = nlen
|
||||||
}
|
}
|
||||||
|
|
||||||
// push_many implements the functionality for pushing another array.
|
// push_many implements the functionality for pushing another array.
|
||||||
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
||||||
[unsafe]
|
[unsafe]
|
||||||
pub fn (mut a3 array) push_many(val voidptr, size int) {
|
pub fn (mut a3 array) push_many(val voidptr, size int) {
|
||||||
a3.ensure_cap(a3.len + size)
|
nlen := a3.len + size
|
||||||
if a3.data == val && !isnil(a3.data) {
|
a3.ensure_cap(nlen)
|
||||||
|
if a3.data == val && a3.data != 0 {
|
||||||
// handle `arr << arr`
|
// handle `arr << arr`
|
||||||
copy := a3.clone()
|
copy := a3.clone()
|
||||||
unsafe {
|
unsafe {
|
||||||
// vmemcpy(a.data, copy.data, copy.element_size * copy.len)
|
vmemcpy(&byte(a3.data) + a3.len * a3.element_size, copy.data, a3.element_size * size)
|
||||||
vmemcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !isnil(a3.data) && !isnil(val) {
|
if a3.data != 0 && val != 0 {
|
||||||
unsafe { vmemcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
|
unsafe { vmemcpy(&byte(a3.data) + a3.len * a3.element_size, val, a3.element_size * size) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
a3.len += size
|
a3.len = nlen
|
||||||
}
|
}
|
||||||
|
|
||||||
// reverse_in_place reverses existing array data, modifying original array.
|
// reverse_in_place reverses existing array data, modifying original array.
|
||||||
|
|
Loading…
Reference in New Issue