Fix logic bug in optimizer, and fix up DEBUG_DEPS compile.
[ccan] / ccan / tdb / tools / replay_trace.c
index f9552b92637e1f73a2a10e29730940f00a393b9e..db5a07be1cf7c7dd4b3cd7ae18a5770ca1d5ca63 100644 (file)
@@ -502,7 +502,8 @@ static void do_pre(char *filename[], unsigned int file, int pre_fd,
 
 #if DEBUG_DEPS
                printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
-                      dep->needs_op, dep->satisfies_file, dep->satisfies_op+1);
+                      dep->needs_opnum+1, filename[dep->satisfies_file],
+                      dep->satisfies_opnum+1);
                fflush(stdout);
 #endif
                /* This could be any op, not just this one. */
@@ -1165,8 +1166,10 @@ static void add_dependency(void *ctx,
        unsigned int needs_start, sat_start;
 
        /* We don't depend on ourselves. */
-       if (needs_file == satisfies_file)
+       if (needs_file == satisfies_file) {
+               assert(satisfies_opnum < needs_opnum);
                return;
+       }
 
 #if DEBUG_DEPS
        printf("%s:%u: depends on %s:%u\n",
@@ -1319,6 +1322,29 @@ static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
 {
        unsigned int i, j;
 
+       /* There can only be one real dependency on each file */
+       for (i = 0; i < num; i++) {
+               for (j = 1; j < num_ops[i]; j++) {
+                       struct depend *dep, *next;
+                       struct depend *prev[num];
+
+                       memset(prev, 0, sizeof(prev));
+
+                       list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
+                               if (!prev[dep->satisfies_file]) {
+                                       prev[dep->satisfies_file] = dep;
+                                       continue;
+                               }
+                               if (prev[dep->satisfies_file]->satisfies_opnum
+                                   < dep->satisfies_opnum) {
+                                       talloc_free(prev[dep->satisfies_file]);
+                                       prev[dep->satisfies_file] = dep;
+                               } else
+                                       talloc_free(dep);
+                       }
+               }
+       }
+
        for (i = 0; i < num; i++) {
                int deps[num];