1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

transformer: only enable array optimisation with -prod (#12833)

This commit is contained in:
Thomas Mangin 2021-12-14 11:05:54 +00:00 committed by GitHub
parent 5365984ef5
commit 99f14a7ead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -25,7 +25,8 @@ fn access(line string) {
}
fn test_array_optimisation() {
mut args := []string{cap: 3}
mut args := []string{cap: 4}
args << '-prod'
args << test
args << '-o'
args << '-'

View File

@ -93,6 +93,8 @@ fn check_for_c_init_1(a []byte) {
for access_it := a[34]; a[34] == 0; {
direct(a[34])
access(a[35])
// work around https://github.com/vlang/v/issues/12832
println(access_it)
return
}
}

View File

@ -156,6 +156,9 @@ pub fn (mut t Transformer) transform(mut ast_file ast.File) {
}
pub fn (mut t Transformer) find_new_array_len(node ast.AssignStmt) {
if !t.pref.is_prod {
return
}
// looking for, array := []type{len:int}
mut right := node.right[0]
if mut right is ast.ArrayInit {
@ -185,6 +188,9 @@ pub fn (mut t Transformer) find_new_array_len(node ast.AssignStmt) {
}
pub fn (mut t Transformer) find_new_range(node ast.AssignStmt) {
if !t.pref.is_prod {
return
}
// looking for, array := []type{len:int}
mut right := node.right[0]
if mut right is ast.IndexExpr {
@ -214,10 +220,16 @@ pub fn (mut t Transformer) find_new_range(node ast.AssignStmt) {
}
pub fn (mut t Transformer) find_mut_self_assign(node ast.AssignStmt) {
if !t.pref.is_prod {
return
}
// even if mutable we can be sure than `a[1] = a[2] is safe
}
pub fn (mut t Transformer) find_assert_len(node ast.InfixExpr) {
if !t.pref.is_prod {
return
}
right := node.right
match right {
ast.IntegerLiteral {
@ -265,6 +277,9 @@ pub fn (mut t Transformer) find_assert_len(node ast.InfixExpr) {
}
pub fn (mut t Transformer) check_safe_array(mut node ast.IndexExpr) {
if !t.pref.is_prod {
return
}
if !node.is_array {
return
}