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

cgen: speed up c_name/1 by using a map lookup, instead of a linear search in []string{}

This commit is contained in:
Delyan Angelov 2021-05-03 12:58:40 +03:00
parent 9d4783a2dd
commit d3f2d6f6df
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -14,19 +14,27 @@ import v.depgraph
const ( const (
// NB: some of the words in c_reserved, are not reserved in C, // NB: some of the words in c_reserved, are not reserved in C,
// but are in C++, or have special meaning in V, thus need escaping too. // but are in C++, or have special meaning in V, thus need escaping too.
c_reserved = ['auto', 'break', 'calloc', 'case', 'char', 'class', 'const', 'continue', 'default', c_reserved = ['auto', 'break', 'calloc', 'case', 'char', 'class', 'const', 'continue',
'delete', 'do', 'double', 'else', 'enum', 'error', 'exit', 'export', 'extern', 'float', 'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit', 'export', 'extern',
'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc', 'namespace', 'new', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc', 'namespace',
'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'struct', 'new', 'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static',
'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned', 'void', 'volatile', 'while', 'struct', 'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned', 'void', 'volatile',
'template', 'while', 'template']
] c_reserved_map = string_array_to_map(c_reserved)
// same order as in token.Kind // same order as in token.Kind
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
// when operands are switched // when operands are switched
cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge'] cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge']
) )
fn string_array_to_map(a []string) map[string]bool {
mut res := map[string]bool{}
for x in a {
res[x] = true
}
return res
}
struct Gen { struct Gen {
pref &pref.Preferences pref &pref.Preferences
module_built string module_built string
@ -5858,7 +5866,7 @@ fn op_to_fn_name(name string) string {
[inline] [inline]
fn c_name(name_ string) string { fn c_name(name_ string) string {
name := util.no_dots(name_) name := util.no_dots(name_)
if name in c.c_reserved { if name in c.c_reserved_map {
return 'v_$name' return 'v_$name'
} }
return name return name