authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-21 16:46:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-21 16:46:33-04:00
logc1642355f0b05f182c0b6d81d294d12be79ad0a8
tree350987257f118c809dc3bca09c02331c537504c8
parenta1af7cbf007f61e8fea06e1497b93c05bd989e74

parse-c: improve performance

previously we did linear search to find existing global declarations; now we index using a hash map. building tetris went from taking 5.3 sec to 0.76 sec

1 files changed, 17 insertions(+), 20 deletions(-)

src/parsec.cpp+17-20
......@@ -41,6 +41,7 @@ struct Context {
4141 AstNode *root;
4242 HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
4343 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
44 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> global_table;
4445 SourceManager *source_manager;
4546 ZigList<Alias> aliases;
4647 ZigList<MacroSymbol> macro_symbols;
......@@ -296,20 +297,10 @@ static AstNode *trans_create_node_unwrap_null(Context *c, AstNode *child) {
296297}
297298
298299static AstNode *get_global(Context *c, Buf *name) {
299 for (size_t i = 0; i < c->root->data.root.top_level_decls.length; i += 1) {
300 AstNode *decl_node = c->root->data.root.top_level_decls.items[i];
301 if (decl_node->type == NodeTypeVariableDeclaration) {
302 if (buf_eql_buf(decl_node->data.variable_declaration.symbol, name)) {
303 return decl_node;
304 }
305 } else if (decl_node->type == NodeTypeFnDef) {
306 if (buf_eql_buf(decl_node->data.fn_def.fn_proto->data.fn_proto.name, name)) {
307 return decl_node;
308 }
309 } else if (decl_node->type == NodeTypeFnProto) {
310 if (buf_eql_buf(decl_node->data.fn_proto.name, name)) {
311 return decl_node;
312 }
300 {
301 auto entry = c->global_table.maybe_get(name);
302 if (entry) {
303 return entry->value;
313304 }
314305 }
315306 {
......@@ -320,11 +311,16 @@ static AstNode *get_global(Context *c, Buf *name) {
320311 return nullptr;
321312}
322313
314static void add_top_level_decl(Context *c, Buf *name, AstNode *node) {
315 c->global_table.put(name, node);
316 c->root->data.root.top_level_decls.append(node);
317}
318
323319static AstNode *add_global_var(Context *c, Buf *var_name, AstNode *value_node) {
324320 bool is_const = true;
325321 AstNode *type_node = nullptr;
326322 AstNode *node = trans_create_node_var_decl_global(c, is_const, var_name, type_node, value_node);
327 c->root->data.root.top_level_decls.append(node);
323 add_top_level_decl(c, var_name, node);
328324 return node;
329325}
330326
......@@ -2519,7 +2515,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
25192515
25202516 if (!fn_decl->hasBody()) {
25212517 // just a prototype
2522 c->root->data.root.top_level_decls.append(proto_node);
2518 add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node);
25232519 return;
25242520 }
25252521
......@@ -2563,7 +2559,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
25632559 fn_def_node->data.fn_def.body = body_node_with_param_inits;
25642560
25652561 proto_node->data.fn_proto.fn_def_node = fn_def_node;
2566 c->root->data.root.top_level_decls.append(fn_def_node);
2562 add_top_level_decl(c, fn_def_node->data.fn_def.fn_proto->data.fn_proto.name, fn_def_node);
25672563}
25682564
25692565static AstNode *resolve_typdef_as_builtin(Context *c, const TypedefNameDecl *typedef_decl, const char *primitive_name) {
......@@ -2911,14 +2907,14 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
29112907 }
29122908
29132909 AstNode *var_node = trans_create_node_var_decl_global(c, is_const, name, var_type, init_node);
2914 c->root->data.root.top_level_decls.append(var_node);
2910 add_top_level_decl(c, name, var_node);
29152911 return;
29162912 }
29172913
29182914 if (is_extern) {
29192915 AstNode *var_node = trans_create_node_var_decl_global(c, is_const, name, var_type, nullptr);
29202916 var_node->data.variable_declaration.is_extern = true;
2921 c->root->data.root.top_level_decls.append(var_node);
2917 add_top_level_decl(c, name, var_node);
29222918 return;
29232919 }
29242920
......@@ -2976,7 +2972,7 @@ static void render_macros(Context *c) {
29762972
29772973 AstNode *value_node = entry->value;
29782974 if (value_node->type == NodeTypeFnDef) {
2979 c->root->data.root.top_level_decls.append(value_node);
2975 add_top_level_decl(c, value_node->data.fn_def.fn_proto->data.fn_proto.name, value_node);
29802976 } else {
29812977 add_global_var(c, entry->key, value_node);
29822978 }
......@@ -3183,6 +3179,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
31833179 }
31843180 c->decl_table.init(8);
31853181 c->macro_table.init(8);
3182 c->global_table.init(8);
31863183 c->ptr_params.init(8);
31873184 c->codegen = codegen;
31883185 c->source_node = source_node;