From 5fafb17d81c2bfb07402e04d34c717a7abf42f84 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 15 Nov 2020 22:46:07 +0000 Subject: [PATCH] Improve generate_entity_dependency_graph: Calculate edges for graph M - Part 2 --- src/checker.cpp | 51 ++++++++++++++++++++++++++++++++++++++++--------- src/ptr_set.cpp | 32 +++++++++++++++---------------- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/src/checker.cpp b/src/checker.cpp index de1d8091d..ef8e39ed9 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1729,7 +1729,10 @@ void add_dependency_to_set(Checker *c, Entity *entity) { void generate_minimum_dependency_set(Checker *c, Entity *start) { - ptr_set_init(&c->info.minimum_dependency_set, heap_allocator()); + isize entity_count = c->info.entities.count; + isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor + + ptr_set_init(&c->info.minimum_dependency_set, heap_allocator(), min_dep_set_cap); ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator()); String required_runtime_entities[] = { @@ -1893,19 +1896,17 @@ void add_entity_dependency_from_procedure_parameters(Map *M, } -Array generate_entity_dependency_graph(CheckerInfo *info) { +Array generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) { #define TIME_SECTION(str) do { if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0) - gbAllocator a = heap_allocator(); - Map M = {}; // Key: Entity * - map_init(&M, a, info->entities.count); + map_init(&M, allocator, info->entities.count); defer (map_destroy(&M)); for_array(i, info->entities) { Entity *e = info->entities[i]; DeclInfo *d = e->decl_info; if (is_entity_a_dependency(e)) { - EntityGraphNode *n = gb_alloc_item(a, EntityGraphNode); + EntityGraphNode *n = gb_alloc_item(allocator, EntityGraphNode); n->entity = e; map_set(&M, hash_pointer(e), n); } @@ -1940,7 +1941,7 @@ Array generate_entity_dependency_graph(CheckerInfo *info) { // This means that the entity graph node set will have to be thread safe TIME_SECTION("generate_entity_dependency_graph: Calculate edges for graph M - Part 2"); - auto G = array_make(a, 0, M.entries.count); + auto G = array_make(allocator, 0, M.entries.count); for_array(i, M.entries) { auto *entry = &M.entries[i]; @@ -1961,17 +1962,27 @@ Array generate_entity_dependency_graph(CheckerInfo *info) { EntityGraphNode *s = n->succ.entries[k].ptr; // Ignore self-cycles if (s != n) { + if (p->entity->kind == Entity_Procedure && + s->entity->kind == Entity_Procedure) { + // NOTE(bill, 2020-11-15): Only care about variable initialization ordering + // TODO(bill): This is probably wrong!!!! + continue; + } + // IMPORTANT NOTE/TODO(bill, 2020-11-15): These three calls take the majority of the + // the time to process + entity_graph_node_set_add(&p->succ, s); entity_graph_node_set_add(&s->pred, p); // Remove edge to 'n' entity_graph_node_set_remove(&s->pred, n); } } + // Remove edge to 'n' entity_graph_node_set_remove(&p->succ, n); } } - } else { + } else if (e->kind == Entity_Variable) { array_add(&G, n); } } @@ -1984,6 +1995,28 @@ Array generate_entity_dependency_graph(CheckerInfo *info) { GB_ASSERT(n->dep_count >= 0); } + // f64 succ_count = 0.0; + // f64 pred_count = 0.0; + // f64 succ_capacity = 0.0; + // f64 pred_capacity = 0.0; + // f64 succ_max = 0.0; + // f64 pred_max = 0.0; + // for_array(i, G) { + // EntityGraphNode *n = G[i]; + // succ_count += n->succ.entries.count; + // pred_count += n->pred.entries.count; + // succ_capacity += n->succ.entries.capacity; + // pred_capacity += n->pred.entries.capacity; + + // succ_max = gb_max(succ_max, n->succ.entries.capacity); + // pred_max = gb_max(pred_max, n->pred.entries.capacity); + + // } + // f64 count = cast(f64)G.count; + // gb_printf_err(">>>count pred: %f succ: %f\n", pred_count/count, succ_count/count); + // gb_printf_err(">>>capacity pred: %f succ: %f\n", pred_capacity/count, succ_capacity/count); + // gb_printf_err(">>>max pred: %f succ: %f\n", pred_max, succ_max); + return G; #undef TIME_SECTION @@ -4174,7 +4207,7 @@ void calculate_global_init_order(Checker *c) { CheckerInfo *info = &c->info; TIME_SECTION("calculate_global_init_order: generate entity dependency graph"); - Array dep_graph = generate_entity_dependency_graph(info); + Array dep_graph = generate_entity_dependency_graph(info, heap_allocator()); defer ({ for_array(i, dep_graph) { entity_graph_node_destroy(dep_graph[i], heap_allocator()); diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp index 44bc1eca7..e75202663 100644 --- a/src/ptr_set.cpp +++ b/src/ptr_set.cpp @@ -82,13 +82,13 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet *s, T ptr) { u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr; u64 n = cast(u64)s->hashes.count; fr.hash_index = cast(PtrSetIndex)(hash & (n-1)); - fr.entry_index = s->hashes[fr.hash_index]; + fr.entry_index = s->hashes.data[fr.hash_index]; while (fr.entry_index != PTR_SET_SENTINEL) { - if (s->entries[fr.entry_index].ptr == ptr) { + if (s->entries.data[fr.entry_index].ptr == ptr) { return fr; } fr.entry_prev = fr.entry_index; - fr.entry_index = s->entries[fr.entry_index].next; + fr.entry_index = s->entries.data[fr.entry_index].next; } } return fr; @@ -113,10 +113,10 @@ void ptr_set_rehash(PtrSet *s, isize new_count) { array_resize(&ns.hashes, new_count); array_reserve(&ns.entries, s->entries.count); for (i = 0; i < new_count; i++) { - ns.hashes[i] = PTR_SET_SENTINEL; + ns.hashes.data[i] = PTR_SET_SENTINEL; } for (i = 0; i < s->entries.count; i++) { - PtrSetEntry *e = &s->entries[i]; + PtrSetEntry *e = &s->entries.data[i]; PtrSetFindResult fr; if (ns.hashes.count == 0) { ptr_set_grow(&ns); @@ -124,11 +124,11 @@ void ptr_set_rehash(PtrSet *s, isize new_count) { fr = ptr_set__find(&ns, e->ptr); j = ptr_set__add_entry(&ns, e->ptr); if (fr.entry_prev == PTR_SET_SENTINEL) { - ns.hashes[fr.hash_index] = j; + ns.hashes.data[fr.hash_index] = j; } else { - ns.entries[fr.entry_prev].next = j; + ns.entries.data[fr.entry_prev].next = j; } - ns.entries[j].next = fr.entry_index; + ns.entries.data[j].next = fr.entry_index; if (ptr_set__full(&ns)) { ptr_set_grow(&ns); } @@ -157,9 +157,9 @@ T ptr_set_add(PtrSet *s, T ptr) { } else { index = ptr_set__add_entry(s, ptr); if (fr.entry_prev != PTR_SET_SENTINEL) { - s->entries[fr.entry_prev].next = index; + s->entries.data[fr.entry_prev].next = index; } else { - s->hashes[fr.hash_index] = index; + s->hashes.data[fr.hash_index] = index; } } if (ptr_set__full(s)) { @@ -173,20 +173,20 @@ template void ptr_set__erase(PtrSet *s, PtrSetFindResult fr) { PtrSetFindResult last; if (fr.entry_prev == PTR_SET_SENTINEL) { - s->hashes[fr.hash_index] = s->entries[fr.entry_index].next; + s->hashes.data[fr.hash_index] = s->entries.data[fr.entry_index].next; } else { - s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next; + s->entries.data[fr.entry_prev].next = s->entries.data[fr.entry_index].next; } if (fr.entry_index == s->entries.count-1) { array_pop(&s->entries); return; } - s->entries[fr.entry_index] = s->entries[s->entries.count-1]; - last = ptr_set__find(s, s->entries[fr.entry_index].ptr); + s->entries.data[fr.entry_index] = s->entries.data[s->entries.count-1]; + last = ptr_set__find(s, s->entries.data[fr.entry_index].ptr); if (last.entry_prev != PTR_SET_SENTINEL) { - s->entries[last.entry_prev].next = fr.entry_index; + s->entries.data[last.entry_prev].next = fr.entry_index; } else { - s->hashes[last.hash_index] = fr.entry_index; + s->hashes.data[last.hash_index] = fr.entry_index; } }