builtin: use == 0 instead of isnil()

Do not rely on C inlining that may not happen for a *very* frequently
called fn in lower level code.
pull/10597/head
Delyan Angelov 2021-06-28 10:52:28 +03:00
parent 830cf4645c
commit 8650ec6916
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 30 additions and 31 deletions

View File

@ -376,7 +376,7 @@ pub fn (a &array) clone_to_depth(depth int) array {
}
return arr
} else {
if !isnil(a.data) {
if a.data != 0 {
unsafe { C.memcpy(&byte(arr.data), a.data, a.cap * a.element_size) }
}
return arr
@ -409,7 +409,7 @@ fn (mut a array) push(val voidptr) {
// `val` is array.data and user facing usage is `a << [1,2,3]`
[unsafe]
pub fn (mut a3 array) push_many(val voidptr, size int) {
if a3.data == val && !isnil(a3.data) {
if a3.data == val && val != 0 {
// handle `arr << arr`
copy := a3.clone()
a3.ensure_cap(a3.len + size)
@ -419,7 +419,7 @@ pub fn (mut a3 array) push_many(val voidptr, size int) {
}
} else {
a3.ensure_cap(a3.len + size)
if !isnil(a3.data) && !isnil(val) {
if a3.data != 0 && val != 0 {
unsafe { C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
}
}

View File

@ -204,7 +204,7 @@ fn (a &array) clone_to_depth_noscan(depth int) array {
}
return arr
} else {
if !isnil(a.data) {
if a.data != 0 {
unsafe { C.memcpy(&byte(arr.data), a.data, a.cap * a.element_size) }
}
return arr
@ -221,7 +221,7 @@ fn (mut a array) push_noscan(val voidptr) {
// `val` is array.data and user facing usage is `a << [1,2,3]`
[unsafe]
fn (mut a3 array) push_many_noscan(val voidptr, size int) {
if a3.data == val && !isnil(a3.data) {
if a3.data == val && val != 0 {
// handle `arr << arr`
copy := a3.clone()
a3.ensure_cap_noscan(a3.len + size)
@ -231,7 +231,7 @@ fn (mut a3 array) push_many_noscan(val voidptr, size int) {
}
} else {
a3.ensure_cap_noscan(a3.len + size)
if !isnil(a3.data) && !isnil(val) {
if a3.data != 0 && val != 0 {
unsafe { C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size) }
}
}

View File

@ -110,7 +110,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
cmd := 'addr2line -e $executable $addr'
// taken from os, to avoid depending on the os module inside builtin.v
f := C.popen(&char(cmd.str), c'r')
if isnil(f) {
if f == 0 {
eprintln(sframe)
continue
}

View File

@ -73,7 +73,7 @@ fn (mut m SortedMap) set(key string, value voidptr) {
mut parent := &mapnode(0)
for {
if node.len == max_len {
if isnil(parent) {
if parent != 0 {
parent = new_node()
m.root = parent
}
@ -100,7 +100,7 @@ fn (mut m SortedMap) set(key string, value voidptr) {
}
return
}
if isnil(node.children) {
if node.children == 0 {
mut j := node.len - 1
for j >= 0 && key < node.keys[j] {
node.keys[j + 1] = node.keys[j]
@ -130,7 +130,7 @@ fn (mut n mapnode) split_child(child_index int, mut y mapnode) {
z.keys[j] = y.keys[j + degree]
z.values[j] = y.values[j + degree]
}
if !isnil(y.children) {
if y.children != 0 {
z.children = unsafe { &voidptr(malloc(int(children_bytes))) }
for jj := degree - 1; jj >= 0; jj-- {
unsafe {
@ -139,7 +139,7 @@ fn (mut n mapnode) split_child(child_index int, mut y mapnode) {
}
}
unsafe {
if isnil(n.children) {
if n.children == 0 {
n.children = &voidptr(malloc(int(children_bytes)))
}
n.children[n.len + 1] = n.children[n.len]
@ -173,7 +173,7 @@ fn (m SortedMap) get(key string, out voidptr) bool {
}
return true
}
if isnil(node.children) {
if node.children == 0 {
break
}
node = unsafe { &mapnode(node.children[i + 1]) }
@ -182,7 +182,7 @@ fn (m SortedMap) get(key string, out voidptr) bool {
}
fn (m SortedMap) exists(key string) bool {
if isnil(m.root) { // TODO: find out why root can be nil
if m.root == 0 { // TODO: find out why root can be nil
return false
}
mut node := m.root
@ -194,7 +194,7 @@ fn (m SortedMap) exists(key string) bool {
if i != -1 && key == node.keys[i] {
return true
}
if isnil(node.children) {
if node.children == 0 {
break
}
node = unsafe { &mapnode(node.children[i + 1]) }
@ -213,14 +213,14 @@ fn (n &mapnode) find_key(k string) int {
fn (mut n mapnode) remove_key(k string) bool {
idx := n.find_key(k)
if idx < n.len && n.keys[idx] == k {
if isnil(n.children) {
if n.children == 0 {
n.remove_from_leaf(idx)
} else {
n.remove_from_non_leaf(idx)
}
return true
} else {
if isnil(n.children) {
if n.children == 0 {
return false
}
flag := if idx == n.len { true } else { false }
@ -250,7 +250,7 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) {
k := n.keys[idx]
if unsafe { &mapnode(n.children[idx]) }.len >= degree {
mut current := unsafe { &mapnode(n.children[idx]) }
for !isnil(current.children) {
for current.children != 0 {
current = unsafe { &mapnode(current.children[current.len]) }
}
predecessor := current.keys[current.len - 1]
@ -260,7 +260,7 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) {
node.remove_key(predecessor)
} else if unsafe { &mapnode(n.children[idx + 1]) }.len >= degree {
mut current := unsafe { &mapnode(n.children[idx + 1]) }
for !isnil(current.children) {
for current.children != 0 {
current = unsafe { &mapnode(current.children[0]) }
}
successor := current.keys[0]
@ -294,7 +294,7 @@ fn (mut n mapnode) borrow_from_prev(idx int) {
child.keys[i + 1] = child.keys[i]
child.values[i + 1] = child.values[i]
}
if !isnil(child.children) {
if child.children != 0 {
for i := child.len; i >= 0; i-- {
unsafe {
child.children[i + 1] = child.children[i]
@ -303,7 +303,7 @@ fn (mut n mapnode) borrow_from_prev(idx int) {
}
child.keys[0] = n.keys[idx - 1]
child.values[0] = n.values[idx - 1]
if !isnil(child.children) {
if child.children != 0 {
unsafe {
child.children[0] = sibling.children[sibling.len]
}
@ -319,7 +319,7 @@ fn (mut n mapnode) borrow_from_next(idx int) {
mut sibling := unsafe { &mapnode(n.children[idx + 1]) }
child.keys[child.len] = n.keys[idx]
child.values[child.len] = n.values[idx]
if !isnil(child.children) {
if child.children != 0 {
unsafe {
child.children[child.len + 1] = sibling.children[0]
}
@ -330,7 +330,7 @@ fn (mut n mapnode) borrow_from_next(idx int) {
sibling.keys[i - 1] = sibling.keys[i]
sibling.values[i - 1] = sibling.values[i]
}
if !isnil(sibling.children) {
if sibling.children != 0 {
for i := 1; i <= sibling.len; i++ {
unsafe {
sibling.children[i - 1] = sibling.children[i]
@ -350,7 +350,7 @@ fn (mut n mapnode) merge(idx int) {
child.keys[i + degree] = sibling.keys[i]
child.values[i + degree] = sibling.values[i]
}
if !isnil(child.children) {
if child.children != 0 {
for i := 0; i <= sibling.len; i++ {
unsafe {
child.children[i + degree] = sibling.children[i]
@ -383,11 +383,10 @@ pub fn (mut m SortedMap) delete(key string) {
if m.root.len == 0 {
// tmp := t.root
if isnil(m.root.children) {
if m.root.children == 0 {
return
} else {
m.root = unsafe { &mapnode(m.root.children[0]) }
}
m.root = unsafe { &mapnode(m.root.children[0]) }
// free(tmp)
}
}
@ -396,7 +395,7 @@ pub fn (mut m SortedMap) delete(key string) {
// starting at `at`. Keys are inserted in order.
fn (n &mapnode) subkeys(mut keys []string, at int) int {
mut position := at
if !isnil(n.children) {
if n.children != 0 {
// Traverse children and insert
// keys inbetween children
for i in 0 .. n.len {
@ -421,7 +420,7 @@ fn (n &mapnode) subkeys(mut keys []string, at int) int {
pub fn (m &SortedMap) keys() []string {
mut keys := []string{len: m.len}
if isnil(m.root) || m.root.len == 0 {
if m.root == 0 || m.root.len == 0 {
return keys
}
m.root.subkeys(mut keys, 0)
@ -433,7 +432,7 @@ fn (mut n mapnode) free() {
}
pub fn (mut m SortedMap) free() {
if isnil(m.root) {
if m.root == 0 {
return
}
m.root.free()

View File

@ -38,7 +38,7 @@ pub fn new_scope(parent &Scope, start_pos int) &Scope {
*/
fn (s &Scope) dont_lookup_parent() bool {
return isnil(s.parent) || s.detached_from_parent
return s.parent == 0 || s.detached_from_parent
}
pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject, &Scope) {

View File

@ -2842,7 +2842,7 @@ fn (mut g Gen) trace_autofree(line string) {
// fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, end_pos int) {
fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int, line_nr int, free_parent_scopes bool, stop_pos int) {
if isnil(scope) {
if scope == 0 {
return
}
for _, obj in scope.objects {