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

builtin: reduce the number of array allocations for consts in all V programs (#18889)

This commit is contained in:
Artem Yurchenko 2023-07-19 12:06:41 +02:00 committed by GitHub
parent 44d2f2b302
commit 1ed0cd9a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1317 additions and 644 deletions

View File

@ -7,11 +7,11 @@ const (
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
de_bruijn32 = u32(0x077CB531)
de_bruijn32tab = [u8(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13,
23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]!
de_bruijn64 = u64(0x03f79d71b4ca8b09)
de_bruijn64tab = [u8(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 62, 47, 59,
36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 63, 55, 48, 27, 60, 41, 37, 16, 46,
35, 44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6]
35, 44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6]!
)
const (

View File

@ -21,7 +21,7 @@ const (
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01,
0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00,
0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02,
0x00, 0x01, 0x00]
0x00, 0x01, 0x00]!
pop_8_tab = [u8(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02,
0x03, 0x03, 0x04, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04,
0x03, 0x04, 0x04, 0x05, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03,
@ -39,7 +39,7 @@ const (
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06,
0x07, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06,
0x06, 0x07, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06,
0x07, 0x07, 0x08]
0x07, 0x07, 0x08]!
rev_8_tab = [u8(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30,
0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8,
0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54,
@ -57,7 +57,7 @@ const (
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b,
0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7,
0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f,
0xbf, 0x7f, 0xff]
0xbf, 0x7f, 0xff]!
len_8_tab = [u8(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
@ -75,5 +75,5 @@ const (
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08]
0x08, 0x08, 0x08]!
)

View File

@ -236,7 +236,7 @@ const (
u64(0x7d987706b0213d0a), u64(0x7dce94c85c298c4c), u64(0x7e031cfd3999f7b0),
u64(0x7e37e43c8800759c), u64(0x7e6ddd4baa009303), u64(0x7ea2aa4f4a405be2),
u64(0x7ed754e31cd072da), u64(0x7f0d2a1be4048f90), u64(0x7f423a516e82d9ba),
u64(0x7f76c8e5ca239029), u64(0x7fac7b1f3cac7433), u64(0x7fe1ccf385ebc8a0)]
u64(0x7f76c8e5ca239029), u64(0x7fac7b1f3cac7433), u64(0x7fe1ccf385ebc8a0)]!
// negative exp of 10 binary form
neg_exp = [u64(0x3ff0000000000000), u64(0x3fb999999999999a), u64(0x3f847ae147ae147b),
u64(0x3f50624dd2f1a9fc), u64(0x3f1a36e2eb1c432d), u64(0x3ee4f8b588e368f1),
@ -345,5 +345,5 @@ const (
u64(0x0000002f201d49fb), u64(0x00000004b6695433), u64(0x0000000078a42205),
u64(0x000000000c1069cd), u64(0x000000000134d761), u64(0x00000000001ee257),
u64(0x00000000000316a2), u64(0x0000000000004f10), u64(0x00000000000007e8),
u64(0x00000000000000ca), u64(0x0000000000000014), u64(0x0000000000000002)]
u64(0x00000000000000ca), u64(0x0000000000000014), u64(0x0000000000000002)]!
)

View File

@ -35,7 +35,7 @@ const (
u32(1000000000),
u32(10000000000),
u32(100000000000),
]
]!
)
//=============================================================================

View File

@ -187,7 +187,7 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
k := pow5_inv_num_bits_64 + pow5_bits(int(q)) - 1
i := -e2 + int(q) + k
mul := pow5_inv_split_64[q]
mul := *(&Uint128(&pow5_inv_split_64_x[q * 2]))
vr = mul_shift_64(u64(4) * m2, mul, i)
vp = mul_shift_64(u64(4) * m2 + u64(2), mul, i)
vm = mul_shift_64(u64(4) * m2 - u64(1) - mm_shift, mul, i)
@ -215,7 +215,7 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
i := -e2 - int(q)
k := pow5_bits(i) - pow5_num_bits_64
j := int(q) - k
mul := pow5_split_64[i]
mul := *(&Uint128(&pow5_split_64_x[i * 2]))
vr = mul_shift_64(u64(4) * m2, mul, j)
vp = mul_shift_64(u64(4) * m2 + u64(2), mul, j)
vm = mul_shift_64(u64(4) * m2 - u64(1) - mm_shift, mul, j)

View File

@ -23,7 +23,7 @@ const (
u64(100000000000000000),
u64(1000000000000000000),
u64(10000000000000000000),
]
]!
)
//=============================================================================

View File

@ -43,7 +43,7 @@ const (
0.000000000000000005,
0.0000000000000000005,
0.00000000000000000005,
]
]!
)
/*

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,8 @@ fn test_interpreter() {
tests << InterpTest{'println(3-4)', '-1'}
tests << InterpTest{'println(3*3)', '9'}
tests << InterpTest{'a := 3\nprintln(a*3)', '9'}
tests << InterpTest{'arr := [10,20,30]\nprintln(arr[1])', '20'}
tests << InterpTest{'fa := [10,20,30]!\nprintln(fa[2])', '30'}
for test in tests {
interp_test(test.input, test.output)!
assert true

View File

@ -21,6 +21,7 @@ fn (o Object) as_i64() !i64 {
}
pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
// eprintln('>>>>>>> expr: ${typeof(expr)}')
match expr {
ast.CallExpr {
// println(expr.is_method)
@ -434,6 +435,16 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
}
}
}
FixedArray {
match expr.field_name {
'len' {
return Int{exp.val.len, 32}
}
else {
e.error('unknown selector to fixed array[${exp.val.len}]: ${expr.field_name}')
}
}
}
else {
e.error('unknown selector expression: ${exp.type_name()}')
}
@ -460,7 +471,14 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
}
}
if expr.is_fixed || expr.has_val {
e.error('fixed arrays are not supported')
mut res := FixedArray{
val: []Object{cap: expr.exprs.len}
}
elem_type := if expr.exprs.len > 0 { expr.expr_types[0] } else { 0 }
for exp in expr.exprs {
res.val << e.expr(exp, elem_type)
}
return res
}
mut res := Array{
val: []Object{cap: expr.exprs.len}
@ -532,12 +550,31 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
ast.UnsafeExpr {
return e.expr(expr.expr, expecting)
}
ast.IndexExpr {
left := e.expr(expr.left, expr.left_type)
index := e.expr(expr.index, ast.int_type_idx)
if expr.is_farray {
if left is FixedArray {
if index is i64 {
return left.val[index]
}
}
}
if expr.is_array {
if left is Array {
if index is i64 {
return left.val[index]
}
}
}
e.error('unhandled index expression ${left}[ ${index} ]')
}
ast.AnonFn, ast.ArrayDecompose, ast.AsCast, ast.Assoc, ast.AtExpr, ast.CTempVar,
ast.ChanInit, ast.Comment, ast.ComptimeCall, ast.ComptimeSelector, ast.ComptimeType,
ast.ConcatExpr, ast.DumpExpr, ast.EmptyExpr, ast.EnumVal, ast.GoExpr, ast.SpawnExpr,
ast.IfGuardExpr, ast.IndexExpr, ast.IsRefType, ast.Likely, ast.LockExpr, ast.MapInit,
ast.MatchExpr, ast.Nil, ast.NodeError, ast.None, ast.OffsetOf, ast.OrExpr, ast.RangeExpr,
ast.SelectExpr, ast.SqlExpr, ast.TypeNode, ast.TypeOf {
ast.IfGuardExpr, ast.IsRefType, ast.Likely, ast.LockExpr, ast.MapInit, ast.MatchExpr,
ast.Nil, ast.NodeError, ast.None, ast.OffsetOf, ast.OrExpr, ast.RangeExpr, ast.SelectExpr,
ast.SqlExpr, ast.TypeNode, ast.TypeOf {
e.error('unhandled expression ${typeof(expr).name}')
}
}

View File

@ -4,6 +4,7 @@ const empty = Void{}
// Note: i64 is an int_literal, NOT an i64 (same with f64)
type Object = Array
| FixedArray
| Float
| Int
| Ptr
@ -78,6 +79,16 @@ pub fn (o Object) string() string {
}
return res + ']'
}
FixedArray {
mut res := '['
for i, val in o.val {
res += val.string()
if i + 1 != o.val.len {
res += ', '
}
}
return res + ']!'
}
Ptr {
return o.val.str()
}
@ -135,6 +146,11 @@ pub mut:
size i8 // 8/16/32/64
}
pub struct FixedArray {
pub mut:
val []Object
}
pub struct Array {
pub mut:
val []Object

View File

@ -5162,7 +5162,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
field_expr := field.expr
match field.expr {
ast.ArrayInit {
if field.expr.is_fixed {
if field.expr.is_fixed && g.pref.build_mode != .build_module {
styp := g.typ(field.expr.typ)
val := g.expr_string(field.expr)
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
@ -5221,7 +5221,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
g.const_decl_simple_define(field.mod, field.name, g.expr_string(field_expr))
} else if field.expr is ast.CastExpr {
if field.expr.expr is ast.ArrayInit {
if field.expr.expr.is_fixed {
if field.expr.expr.is_fixed && g.pref.build_mode != .build_module {
styp := g.typ(field.expr.typ)
val := g.expr_string(field.expr.expr)
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{