Improve generate_entity_dependency_graph: Calculate edges for graph M - Part 2

This commit is contained in:
gingerBill
2020-11-15 22:46:07 +00:00
parent 3a229397e4
commit 5fafb17d81
2 changed files with 58 additions and 25 deletions
+42 -9
View File
@@ -1729,7 +1729,10 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
void generate_minimum_dependency_set(Checker *c, Entity *start) { 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()); ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator());
String required_runtime_entities[] = { String required_runtime_entities[] = {
@@ -1893,19 +1896,17 @@ void add_entity_dependency_from_procedure_parameters(Map<EntityGraphNode *> *M,
} }
Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) { Array<EntityGraphNode *> 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) #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<EntityGraphNode *> M = {}; // Key: Entity * Map<EntityGraphNode *> M = {}; // Key: Entity *
map_init(&M, a, info->entities.count); map_init(&M, allocator, info->entities.count);
defer (map_destroy(&M)); defer (map_destroy(&M));
for_array(i, info->entities) { for_array(i, info->entities) {
Entity *e = info->entities[i]; Entity *e = info->entities[i];
DeclInfo *d = e->decl_info; DeclInfo *d = e->decl_info;
if (is_entity_a_dependency(e)) { if (is_entity_a_dependency(e)) {
EntityGraphNode *n = gb_alloc_item(a, EntityGraphNode); EntityGraphNode *n = gb_alloc_item(allocator, EntityGraphNode);
n->entity = e; n->entity = e;
map_set(&M, hash_pointer(e), n); map_set(&M, hash_pointer(e), n);
} }
@@ -1940,7 +1941,7 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
// This means that the entity graph node set will have to be thread safe // 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"); TIME_SECTION("generate_entity_dependency_graph: Calculate edges for graph M - Part 2");
auto G = array_make<EntityGraphNode *>(a, 0, M.entries.count); auto G = array_make<EntityGraphNode *>(allocator, 0, M.entries.count);
for_array(i, M.entries) { for_array(i, M.entries) {
auto *entry = &M.entries[i]; auto *entry = &M.entries[i];
@@ -1961,17 +1962,27 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
EntityGraphNode *s = n->succ.entries[k].ptr; EntityGraphNode *s = n->succ.entries[k].ptr;
// Ignore self-cycles // Ignore self-cycles
if (s != n) { 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(&p->succ, s);
entity_graph_node_set_add(&s->pred, p); entity_graph_node_set_add(&s->pred, p);
// Remove edge to 'n' // Remove edge to 'n'
entity_graph_node_set_remove(&s->pred, n); entity_graph_node_set_remove(&s->pred, n);
} }
} }
// Remove edge to 'n' // Remove edge to 'n'
entity_graph_node_set_remove(&p->succ, n); entity_graph_node_set_remove(&p->succ, n);
} }
} }
} else { } else if (e->kind == Entity_Variable) {
array_add(&G, n); array_add(&G, n);
} }
} }
@@ -1984,6 +1995,28 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
GB_ASSERT(n->dep_count >= 0); 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; return G;
#undef TIME_SECTION #undef TIME_SECTION
@@ -4174,7 +4207,7 @@ void calculate_global_init_order(Checker *c) {
CheckerInfo *info = &c->info; CheckerInfo *info = &c->info;
TIME_SECTION("calculate_global_init_order: generate entity dependency graph"); TIME_SECTION("calculate_global_init_order: generate entity dependency graph");
Array<EntityGraphNode *> dep_graph = generate_entity_dependency_graph(info); Array<EntityGraphNode *> dep_graph = generate_entity_dependency_graph(info, heap_allocator());
defer ({ defer ({
for_array(i, dep_graph) { for_array(i, dep_graph) {
entity_graph_node_destroy(dep_graph[i], heap_allocator()); entity_graph_node_destroy(dep_graph[i], heap_allocator());
+16 -16
View File
@@ -82,13 +82,13 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr; u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
u64 n = cast(u64)s->hashes.count; u64 n = cast(u64)s->hashes.count;
fr.hash_index = cast(PtrSetIndex)(hash & (n-1)); 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) { 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; return fr;
} }
fr.entry_prev = fr.entry_index; 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; return fr;
@@ -113,10 +113,10 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
array_resize(&ns.hashes, new_count); array_resize(&ns.hashes, new_count);
array_reserve(&ns.entries, s->entries.count); array_reserve(&ns.entries, s->entries.count);
for (i = 0; i < new_count; i++) { 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++) { for (i = 0; i < s->entries.count; i++) {
PtrSetEntry<T> *e = &s->entries[i]; PtrSetEntry<T> *e = &s->entries.data[i];
PtrSetFindResult fr; PtrSetFindResult fr;
if (ns.hashes.count == 0) { if (ns.hashes.count == 0) {
ptr_set_grow(&ns); ptr_set_grow(&ns);
@@ -124,11 +124,11 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
fr = ptr_set__find(&ns, e->ptr); fr = ptr_set__find(&ns, e->ptr);
j = ptr_set__add_entry(&ns, e->ptr); j = ptr_set__add_entry(&ns, e->ptr);
if (fr.entry_prev == PTR_SET_SENTINEL) { if (fr.entry_prev == PTR_SET_SENTINEL) {
ns.hashes[fr.hash_index] = j; ns.hashes.data[fr.hash_index] = j;
} else { } 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)) { if (ptr_set__full(&ns)) {
ptr_set_grow(&ns); ptr_set_grow(&ns);
} }
@@ -157,9 +157,9 @@ T ptr_set_add(PtrSet<T> *s, T ptr) {
} else { } else {
index = ptr_set__add_entry(s, ptr); index = ptr_set__add_entry(s, ptr);
if (fr.entry_prev != PTR_SET_SENTINEL) { if (fr.entry_prev != PTR_SET_SENTINEL) {
s->entries[fr.entry_prev].next = index; s->entries.data[fr.entry_prev].next = index;
} else { } else {
s->hashes[fr.hash_index] = index; s->hashes.data[fr.hash_index] = index;
} }
} }
if (ptr_set__full(s)) { if (ptr_set__full(s)) {
@@ -173,20 +173,20 @@ template <typename T>
void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) { void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
PtrSetFindResult last; PtrSetFindResult last;
if (fr.entry_prev == PTR_SET_SENTINEL) { 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 { } 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) { if (fr.entry_index == s->entries.count-1) {
array_pop(&s->entries); array_pop(&s->entries);
return; return;
} }
s->entries[fr.entry_index] = s->entries[s->entries.count-1]; s->entries.data[fr.entry_index] = s->entries.data[s->entries.count-1];
last = ptr_set__find(s, s->entries[fr.entry_index].ptr); last = ptr_set__find(s, s->entries.data[fr.entry_index].ptr);
if (last.entry_prev != PTR_SET_SENTINEL) { 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 { } else {
s->hashes[last.hash_index] = fr.entry_index; s->hashes.data[last.hash_index] = fr.entry_index;
} }
} }