From ec0402e63bb2993ad8eb13101b46563dc6946d5c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 15 Jul 2023 09:30:05 +0300 Subject: [PATCH] ast,cgen: small speedup for the 1m_statements_in_1_fn.v --- vlib/v/ast/types.v | 43 +++++++++++++++++++++++----------- vlib/v/gen/c/auto_eq_methods.v | 12 +++++----- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 7e00250f8e..feba690036 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -272,10 +272,12 @@ pub fn (t Type) atomic_typename() string { } } +[inline] pub fn sharetype_from_flags(is_shared bool, is_atomic bool) ShareType { return unsafe { ShareType(int(u32(is_atomic) << 1) | int(is_shared)) } } +[inline] pub fn (t Type) share() ShareType { return sharetype_from_flags(t.has_flag(.shared_f), t.has_flag(.atomic_f)) } @@ -283,7 +285,7 @@ pub fn (t Type) share() ShareType { // return TypeSymbol idx for `t` [inline] pub fn (t Type) idx() int { - return u16(t) & 0xffff + return int(t) & 0xffff } [inline] @@ -307,13 +309,14 @@ pub fn (t Type) nr_muls() int { pub fn (t Type) is_ptr() bool { // any normal pointer, i.e. &Type, &&Type etc; // Note: voidptr, charptr and byteptr are NOT included! - return (int(t) >> 16) & 0xff > 0 + return (int(t) >> 16) & 0xff != 0 } [inline] pub fn (typ Type) is_pointer() bool { // builtin pointer types (voidptr, byteptr, charptr) - return typ.idx() in ast.pointer_type_idxs + return typ.idx() in [ast.voidptr_type_idx, ast.byteptr_type_idx, ast.charptr_type_idx, + ast.nil_type_idx] } [inline] @@ -323,7 +326,8 @@ pub fn (typ Type) is_voidptr() bool { [inline] pub fn (t Type) is_any_kind_of_pointer() bool { - return (int(t) >> 16) & 0xff > 0 || (u16(t) & 0xffff) in ast.pointer_type_idxs + return (int(t) >> 16) & 0xff != 0 + || u16(t) in [ast.voidptr_type_idx, ast.byteptr_type_idx, ast.charptr_type_idx, ast.nil_type_idx] } // set nr_muls on `t` and return it @@ -384,7 +388,7 @@ pub fn (t Type) clear_flags(flags ...TypeFlag) Type { // return true if `flag` is set on `t` [inline] pub fn (t Type) has_flag(flag TypeFlag) bool { - return int(t) & (1 << (int(flag) + 24)) > 0 + return int(t) & (1 << (int(flag) + 24)) != 0 } // debug returns a verbose representation of the information in ts, useful for tracing/debugging @@ -484,42 +488,50 @@ pub fn new_type_ptr(idx int, nr_muls int) Type { [inline] pub fn (typ Type) is_float() bool { - return !typ.is_ptr() && typ.idx() in ast.float_type_idxs + return !typ.is_ptr() + && typ.idx() in [ast.f32_type_idx, ast.f64_type_idx, ast.float_literal_type_idx] } [inline] pub fn (typ Type) is_int() bool { - return !typ.is_ptr() && typ.idx() in ast.integer_type_idxs + return !typ.is_ptr() + && typ.idx() in [ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, ast.i64_type_idx, ast.u8_type_idx, ast.u16_type_idx, ast.u32_type_idx, ast.u64_type_idx, ast.isize_type_idx, ast.usize_type_idx, ast.int_literal_type_idx, ast.rune_type_idx] } [inline] pub fn (typ Type) is_int_valptr() bool { - return typ.is_ptr() && typ.idx() in ast.integer_type_idxs + return typ.is_ptr() + && typ.idx() in [ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, ast.i64_type_idx, ast.u8_type_idx, ast.u16_type_idx, ast.u32_type_idx, ast.u64_type_idx, ast.isize_type_idx, ast.usize_type_idx, ast.int_literal_type_idx, ast.rune_type_idx] } [inline] pub fn (typ Type) is_float_valptr() bool { - return typ.is_ptr() && typ.idx() in ast.float_type_idxs + return typ.is_ptr() + && typ.idx() in [ast.f32_type_idx, ast.f64_type_idx, ast.float_literal_type_idx] } [inline] pub fn (typ Type) is_pure_int() bool { - return int(typ) in ast.integer_type_idxs + return int(typ) in [ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, ast.i64_type_idx, + ast.u8_type_idx, ast.u16_type_idx, ast.u32_type_idx, ast.u64_type_idx, ast.isize_type_idx, + ast.usize_type_idx, ast.int_literal_type_idx, ast.rune_type_idx] } [inline] pub fn (typ Type) is_pure_float() bool { - return int(typ) in ast.float_type_idxs + return int(typ) in [ast.f32_type_idx, ast.f64_type_idx, ast.float_literal_type_idx] } [inline] pub fn (typ Type) is_signed() bool { - return typ.idx() in ast.signed_integer_type_idxs + return typ.idx() in [ast.char_type_idx, ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, + ast.i64_type_idx, ast.isize_type_idx] } [inline] pub fn (typ Type) is_unsigned() bool { - return typ.idx() in ast.unsigned_integer_type_idxs + return typ.idx() in [ast.u8_type_idx, ast.u16_type_idx, ast.u32_type_idx, ast.u64_type_idx, + ast.usize_type_idx] } pub fn (typ Type) flip_signedness() Type { @@ -545,7 +557,10 @@ pub fn (typ Type) is_int_literal() bool { [inline] pub fn (typ Type) is_number() bool { - return typ.clear_flags() in ast.number_type_idxs + return (int(typ) & 0xffffff) in [ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, + ast.i64_type_idx, ast.u8_type_idx, ast.char_type_idx, ast.u16_type_idx, ast.u32_type_idx, + ast.u64_type_idx, ast.isize_type_idx, ast.usize_type_idx, ast.f32_type_idx, ast.f64_type_idx, + ast.int_literal_type_idx, ast.float_literal_type_idx, ast.rune_type_idx] } [inline] diff --git a/vlib/v/gen/c/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v index bcb619eb11..35d3fdc483 100644 --- a/vlib/v/gen/c/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -225,10 +225,10 @@ fn (mut g Gen) gen_alias_equality_fn(left_type ast.Type) string { } g.generated_eq_fns << left_type info := left.sym.info as ast.Alias - g.definitions.writeln('static bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b); // auto') + g.definitions.writeln('static inline bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b); // auto') mut fn_builder := strings.new_builder(512) - fn_builder.writeln('static bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b) {') + fn_builder.writeln('static inline bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b) {') is_option := left.typ.has_flag(.option) @@ -283,10 +283,10 @@ fn (mut g Gen) gen_array_equality_fn(left_type ast.Type) string { g.generated_eq_fns << left_type elem := g.unwrap(left.sym.array_info().elem_type) ptr_elem_styp := g.typ(elem.typ) - g.definitions.writeln('static bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b); // auto') + g.definitions.writeln('static inline bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b); // auto') mut fn_builder := strings.new_builder(512) - fn_builder.writeln('static bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b) {') + fn_builder.writeln('static inline bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b) {') left_len := g.read_field(left_type, 'len', 'a') right_len := g.read_field(left_type, 'len', 'b') @@ -353,13 +353,13 @@ fn (mut g Gen) gen_fixed_array_equality_fn(left_type ast.Type) string { elem_info := left_typ.sym.array_fixed_info() elem := g.unwrap(elem_info.elem_type) size := elem_info.size - g.definitions.writeln('static bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b); // auto') + g.definitions.writeln('static inline bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b); // auto') left := if left_type.has_flag(.option) { 'a.data' } else { 'a' } right := if left_type.has_flag(.option) { 'b.data' } else { 'b' } mut fn_builder := strings.new_builder(512) - fn_builder.writeln('static bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b) {') + fn_builder.writeln('static inline bool ${ptr_styp}_arr_eq(${ptr_styp} a, ${ptr_styp} b) {') fn_builder.writeln('\tfor (int i = 0; i < ${size}; ++i) {') // compare every pair of elements of the two fixed arrays if elem.sym.kind == .string {