Skip to content

Commit

Permalink
allocate memory only once
Browse files Browse the repository at this point in the history
The best_patterns is only an array of arrays and hence does not need
that much memory. Overestimate the size by assuming we have no duplicate
columns and allocate memory only once. This makes the code more readable
and probably faster too (since reallocating takes time too).
  • Loading branch information
agners committed Jan 4, 2020
1 parent 2fa089e commit 6debfd2
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/cutting_stock.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ double **cutting_stock_compute_best_patterns(order **orders, int order_count, in
columns_matrix = columns_matrix_compute(orders, order_count, max_width);
column_size = order_count;
columns_matrix_number = order_count;
best_patterns = NULL;
temp_best_patterns_number = 0;

while (true) {
dual_column = cutting_stock_compute_dual(orders, order_count, columns_matrix, columns_matrix_number);
Expand Down Expand Up @@ -164,18 +162,14 @@ double **cutting_stock_compute_best_patterns(order **orders, int order_count, in
return NULL;
}

best_patterns = (double **)malloc(columns_matrix_number * sizeof(double *));
temp_best_patterns_number = 0;
for (i = 0; i < columns_matrix_number; i++) {
if (best_patterns) {
if (!double_matrix_contains_array(best_patterns, temp_best_patterns_number, columns_matrix[i], column_size)) {
best_patterns = (double **)realloc(best_patterns, (temp_best_patterns_number + 1) * sizeof(double *));
best_patterns[temp_best_patterns_number] = columns_matrix[i];
temp_best_patterns_number++;
}
} else {
best_patterns = (double **)malloc(sizeof(double *));
best_patterns[0] = columns_matrix[i];
temp_best_patterns_number++;
}
if (double_matrix_contains_array(best_patterns, temp_best_patterns_number, columns_matrix[i], column_size))
continue;

best_patterns[temp_best_patterns_number] = columns_matrix[i];
temp_best_patterns_number++;
}

*best_patterns_number = temp_best_patterns_number;
Expand Down

0 comments on commit 6debfd2

Please sign in to comment.