mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: in [] optimization
This commit is contained in:
parent
010f3efcf0
commit
b7a036945d
@ -42,7 +42,7 @@ pub fn parse_rfc2822(s string) ?Time {
|
|||||||
mm := pos / 3 + 1
|
mm := pos / 3 + 1
|
||||||
mut tmstr := byteptr(0)
|
mut tmstr := byteptr(0)
|
||||||
unsafe { tmstr = malloc(s.len * 2) }
|
unsafe { tmstr = malloc(s.len * 2) }
|
||||||
count := C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm,
|
count := C.sprintf(charptr(tmstr), '%s-%02d-%s %s', fields[3].str, mm,
|
||||||
fields[1].str, fields[4].str)
|
fields[1].str, fields[4].str)
|
||||||
|
|
||||||
t := parse(tos(tmstr, count)) or {
|
t := parse(tos(tmstr, count)) or {
|
||||||
|
@ -944,6 +944,16 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
|
|||||||
else if node.op == .key_in {
|
else if node.op == .key_in {
|
||||||
right_sym := g.table.get_type_symbol(node.right_type)
|
right_sym := g.table.get_type_symbol(node.right_type)
|
||||||
if right_sym.kind == .array {
|
if right_sym.kind == .array {
|
||||||
|
match node.right {
|
||||||
|
ast.ArrayInit {
|
||||||
|
// `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3`
|
||||||
|
// avoids an allocation
|
||||||
|
// g.write('/*in opt*/')
|
||||||
|
g.in_optimization(node.left, it)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
styp := g.typ(node.left_type)
|
styp := g.typ(node.left_type)
|
||||||
g.write('_IN($styp, ')
|
g.write('_IN($styp, ')
|
||||||
g.expr(node.left)
|
g.expr(node.left)
|
||||||
@ -1634,6 +1644,30 @@ fn (g mut Gen) gen_filter(node ast.MethodCallExpr) {
|
|||||||
g.write(tmp)
|
g.write(tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
|
||||||
|
fn (g mut Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
|
||||||
|
is_str := right.elem_type == table.string_type
|
||||||
|
for i, array_expr in right.exprs {
|
||||||
|
if is_str {
|
||||||
|
g.write('string_eq(')
|
||||||
|
}
|
||||||
|
g.expr(left)
|
||||||
|
if is_str {
|
||||||
|
g.write(', ')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
g.write(' == ')
|
||||||
|
}
|
||||||
|
g.expr(array_expr)
|
||||||
|
if is_str {
|
||||||
|
g.write(')')
|
||||||
|
}
|
||||||
|
if i < right.exprs.len - 1 {
|
||||||
|
g.write(' || ')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn op_to_fn_name(name string) string {
|
fn op_to_fn_name(name string) string {
|
||||||
return match name {
|
return match name {
|
||||||
'+'{
|
'+'{
|
||||||
|
@ -40,6 +40,8 @@ const (
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define V_COMMIT_HASH "TODO"
|
||||||
|
|
||||||
'
|
'
|
||||||
c_headers = '
|
c_headers = '
|
||||||
|
|
||||||
|
@ -33,6 +33,10 @@ void User_foo(User* u) {
|
|||||||
array_string zzz = array_repeat(new_array_from_c_array(1, 1, sizeof(string), (string[]){
|
array_string zzz = array_repeat(new_array_from_c_array(1, 1, sizeof(string), (string[]){
|
||||||
tos3(""),
|
tos3(""),
|
||||||
}), u->age);
|
}), u->age);
|
||||||
|
int a = 10;
|
||||||
|
if (a == 10 || a == 20 || a == 30) {
|
||||||
|
int b = 10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void println(string s) {
|
void println(string s) {
|
||||||
|
@ -26,6 +26,10 @@ fn get_user_opt() ?User {
|
|||||||
fn (u &User) foo() {
|
fn (u &User) foo() {
|
||||||
age := u.age
|
age := u.age
|
||||||
zzz := [''].repeat(u.age)
|
zzz := [''].repeat(u.age)
|
||||||
|
a := 10
|
||||||
|
if a in [10, 20, 30] {
|
||||||
|
b := 10
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn println(s string) {}
|
fn println(s string) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user