mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: save a call to array.ensure_cap in array.insert, array.push, array.push_many in most cases
This commit is contained in:
parent
c0c07db7cc
commit
9344c27021
@ -204,7 +204,9 @@ pub fn (mut a array) insert(i int, val voidptr) {
|
|||||||
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
|
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if a.len >= a.cap {
|
||||||
a.ensure_cap(a.len + 1)
|
a.ensure_cap(a.len + 1)
|
||||||
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
vmemmove(a.get_unsafe(i + 1), a.get_unsafe(i), (a.len - i) * a.element_size)
|
vmemmove(a.get_unsafe(i + 1), a.get_unsafe(i), (a.len - i) * a.element_size)
|
||||||
a.set_unsafe(i, val)
|
a.set_unsafe(i, val)
|
||||||
@ -569,7 +571,9 @@ 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 {
|
||||||
a.ensure_cap(a.len + 1)
|
a.ensure_cap(a.len + 1)
|
||||||
|
}
|
||||||
unsafe { vmemmove(&byte(a.data) + a.element_size * a.len, val, a.element_size) }
|
unsafe { vmemmove(&byte(a.data) + a.element_size * a.len, val, a.element_size) }
|
||||||
a.len++
|
a.len++
|
||||||
}
|
}
|
||||||
@ -578,16 +582,15 @@ fn (mut a array) push(val voidptr) {
|
|||||||
// `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)
|
||||||
if a3.data == val && !isnil(a3.data) {
|
if a3.data == val && !isnil(a3.data) {
|
||||||
// handle `arr << arr`
|
// handle `arr << arr`
|
||||||
copy := a3.clone()
|
copy := a3.clone()
|
||||||
a3.ensure_cap(a3.len + size)
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// vmemcpy(a.data, copy.data, copy.element_size * copy.len)
|
// vmemcpy(a.data, copy.data, copy.element_size * copy.len)
|
||||||
vmemcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
|
vmemcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
a3.ensure_cap(a3.len + size)
|
|
||||||
if !isnil(a3.data) && !isnil(val) {
|
if !isnil(a3.data) && !isnil(val) {
|
||||||
unsafe { vmemcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
|
unsafe { vmemcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user